j2se5.0的文档中,关于ResultSet接口的描述有这么一段:
“当生成 ResultSet
对象的 Statement
对象关闭、重新执行或用来从多个结果的序列检索下一个结果时,ResultSet
对象会自动关闭。 ”
所以以下的方法将会产生异常:
import java.sql.*;
public class Bo{
private Connection connection = null;
private PreparedStatement ps = null;
private CallableStatement cs = null;
public Bo(Connection connection){
this.connection = connection;
}
public ResultSet getRsBySql(String sql) throws Exception {
ResultSet rs = null;
ps = connection.prepareStatement(sql);
try{
rs = ps.executeQuery();
} catch(Exception err){
throw err;
} finally{
//原因在这里,关闭ps后,rs也自动关闭。返回的rs值将不可用。
if(ps != null){
ps.close();
}
}
return rs;
}
}
解决的办法是,不返回rs,而用list和map模仿ResultSet接口放回一个list。下边是例程:
import java.io.*;
import java.sql.*;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;
import java.text.SimpleDateFormat;
public class Test{
private Connection con = null;
private PreparedStatement ps = null;
private CallableStatement cs = null;
private void openConnection() throws Exception{
String dbUrl = "jdbc:oracle:thin:@192.168.1.10:1521:ORADB";
String user = "username";
String pwd = "password";
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
con = java.sql.DriverManager.getConnection(dbUrl, user, pwd);
}
private void closeConnection() throws Exception{
if(con != null && con.isClosed() != true){
con.close();
}
}
private String getGuid() throws Exception{
String guid = null;
try{
cs = con.prepareCall("{call select sys_guid() into ? from dual}");
cs.registerOutParameter(1,Types.VARCHAR);
cs.executeQuery();
guid = cs.getString(1);
} finally{
if(cs != null) cs.close();
}
return guid;
}
private List getRecordListBySql(String sql) throws Exception{
List recordList = new ArrayList();
try{
ps = con.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
int columnCount = rs.getMetaData().getColumnCount();
while(rs.next()){
ResultSetMetaData resultSetMetaData = rs.getMetaData();
Map recordMap = new HashMap();
for(int i = 1; i <= columnCount; i++){
String columnTypeName = resultSetMetaData.getColumnTypeName(i);
String columnName = resultSetMetaData.getColumnName(i);
if(columnTypeName.equals("DATE"))
recordMap.put(columnName,rs.getTimestamp(i));
else if(columnTypeName.equals("NUMBER"))
recordMap.put(columnName,rs.getString(i));
else if(columnTypeName.equals("VARCHAR2"))
recordMap.put(columnName,rs.getString(i));
else if(columnTypeName.equals("CLOB"))
recordMap.put(columnName,rs.getString(i));
else
recordMap.put(columnName, rs.getObject(i));
}
recordList.add(recordMap);
}
} finally{
if(ps != null) ps.close();
}
return recordList;
}
public static void main(String [] args){
Test test = new Test();
try{
test.openConnection();
List recordList = test.getRecordListBySql("select * from (select t.*,row_number()over(order by id) rn from tb_attrinfo t) where rn <= 10");
for(int i = 0; i < recordList.size(); i++){
Map recordMap = (Map)recordList.get(i);
//java.util.Date createTime = (java.util.Date)recordMap.get("CREATETIME");
//SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println((String)recordMap.get("INFOCONTENT"));
}
} catch(Exception err){
err.printStackTrace();
} finally{
try{
test.closeConnection();
} catch(Exception err){
err.printStackTrace();
}
}
}
}