日期:2014-05-20 浏览次数:20775 次
public java.util.List<String> query(String sql) throws SQLException {
java.util.List<String> list = new java.util.ArrayList<String>();
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery(sql);
//String[] COLNAMES = dealSQL(sql, rs);
list = getResultSet(rs);
ExConnections.close(conn);
return list;
}
//读取ResultSet里面的内容.
private java.util.List<String> getResultSet(ResultSet rs) throws SQLException {
java.util.List<String> list = new java.util.ArrayList<String>();
String reads = "";
String COLTitle = "";
boolean addTitle = false;
ResultSetMetaData rsm = rs.getMetaData();
int count = rsm.getColumnCount();
while (rs.next()) {
for (int i = 1; i <= count; i++) {
reads += rs.getString(i) + "\t\t";//
COLTitle += rsm.getColumnName(i) +"\t\t";
if (i == count) {// When you arrive at the last column,
// the data added to the list.
if (addTitle == false) {
list.add(COLTitle);
list.add("-------------------------------\n");
addTitle = true;
}
// System.out.println(COLname);
list.add(reads);
reads = "";
}
}
}
ExConnections.close(rs);
return list;
}