从jdbc查询的结果集中的数据放入到String数组中

------解决方案--------------------------------------------------------
public List query(String sql) {
Vector content = new Vector();
Connection conn = null;
Statement stmt = null;
try {
logger.debug("查询语句:"+sql);
//System.out.println("查询语句:"+sql);
conn = this.getConnection();
stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
ResultSetMetaData rsMeta = rs.getMetaData();
while (rs.next()) {
int columnNum = rsMeta.getColumnCount();
String[] field = new String[columnNum];
String fieldValue = null;
for (int i = 1; i <= columnNum; i++) {
//fieldValue = rs.getString(rsMeta.getColumnName(i));
fieldValue = rs.getString(i);
if (fieldValue == null) {
fieldValue = "";
}
field[i - 1] = fieldValue;
}
content.add(field);
}

} catch ( SQLException  e) {
logger.error(e.toString());
e.printStackTrace();
} finally {
try {
if (stmt != null) {
stmt.close();
}
this.closeConnection(conn);
} catch (SQLException e1) {
logger.error(e1.toString());
e1.printStackTrace();
}
}
return content;
}

你可能感兴趣的:(Java)