代码比较简单
请多多指教
请多多指教
/**
* 查询结果以数组形式返回
*
* @param sql 查询的sql语句
* @return
*/
public String[][] query(String sql){
ConnectionPool pool = ConnectionPool.getInstance(); // 连接池
ResultSet rs = null;
String[][] result = null;
Connection conn = pool.getConnection();
try {
rs = conn.createStatement().executeQuery(sql);
ResultSetMetaData metadata = rs.getMetaData();
int columnCount = metadata.getColumnCount();
rs.last(); //移到最后一行
int length = rs.getRow(); //得到当前行号,也就是记录数
rs.beforeFirst(); //还要
result = new String[length][columnCount];
int index = 0;
while(rs.next()){
for(int i=0; i<columnCount; i++){
result[index][i] = rs.getString(i+1);
}
index++;
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
closeRs(rs);
pool.release(conn);
}
return result;
}