如题,我想把结果集复制到另外一个类似也为ResultSet的东东里去,但有同志说这是个接口,无法复制的!是这样的吗?没其它办法把结果集内的同样东西放到另外一个ResultSet里吗?
如果可以放进去,我是想先关闭数据库连接,然后使用getString()类似的方法对这个新的ResultSet进行操作!可以吗?
如果可以放进去,我是想先关闭数据库连接,然后使用getString()类似的方法对这个新的ResultSet进行操作!可以吗?
解决方案 »
- 将一个对人员增删改查的程序改成Hibernate+spring+struts方式,需要配置哪些文件?
- 开始学习用jsp做论坛,希望哪位朋友能发一个论坛的源代码给我学习
- JSP问题!!请帮忙看下
- 如何得之用户已取消?
- 數據庫關鍵字查詢遇到的問題﹐請高手指點﹐在線等﹗
- weblogic7下配置连接池问题?
- 自定义标记是为了提高编写网页的效率吗?
- 请问如何处理这三个文件才可以正常使用jsp连接ms sql数据库??
- CSDN论坛助手 重出江湖!!!!爽!爽!!爽!!知道什么是极速吗?快来体验一下了。[周一早8:00发布]
- 如何让系统每隔一段时间向数据库中写一次数据
- JSP中的=号附近有错误
- JSP引用.inc出现乱码
我写了一个数据库的“基本操作类”,只包括建立连接,执行语句的方法!另外一个类(叫做B)里有几个方法,具体都是采用先前这个“基本操作类”,然后试图得到一个结果集!每个方法都是返回的结果集!
然后我在真正要操作结果集的一个java中,进行真正对数据的读取操作,但问题是得关闭连接!
所以我没办法,想在B里复制一个结果集,然后在B里把这些连接都关闭了!不知道我说清楚没??
所以,你还是需要先处理rs,否则,conn关闭后,rs就失效了
rs = stmt.executeQuery("select col1, col2 from test_table");
// create a new rowset and populate it...
CachedRowSet crs = new CachedRowSet();
crs.populate(rs);
CachedRowSet crs2 = (CachedRowSet)crs.clone();
clone()我查了一下,是对cookie的操作!这个CachedRowSet是哪里定义的?
populate
void populate(ResultSet data)
throws SQLException
Populates this CachedRowSet object with data from the given ResultSet object.
This method can be used as an alternative to the execute method when an application has a connection to an open ResultSet object. Using the method populate can be more efficient than using the version of the execute method that takes no parameters because it does not open a new connection and re-execute this CachedRowSet object's command. Using the populate method is more a matter of convenience when compared to using the version of execute that takes a ResultSet object.
Parameters:
data - the ResultSet object containing the data to be read into this CachedRowSet object
大家看看,我可以采用这个吗?
其它方法统统throwException。
然后写一个方法从一个ResultSet中把资料灌到你写的ResultSet对象中。
然后...就可以关掉数据库连接了。
给个简单例子:
public Collection getAllAreas() {
String sql = "SELECT * FROM area_code ORDER BY code";
Connection conn = DBConnectFactory.getInstance().getConnection();
PreparedStatement ps = null;
ResultSet rs = null;
Collection areaTypes = new ArrayList();
try {
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
while (rs.next()) {
Types type = new Types();
type.setId(rs.getInt("code"));
type.setName(rs.getString("name"));
areaTypes.add(type);
}
return areaTypes;
}
catch (Exception e) {
System.out.println("数据库操作失败!");
e.printStackTrace();
}
finally {
try {
ps.close();
DBConnectFactory.CloseConnection(conn);
}
catch (Exception e1) {
e1.printStackTrace();
}
}
return null;
}