JDBC执行SQL语句后的问题。

JacksonFox 2007-10-13 05:38:40
我按照下面的操作
1.打开数据库。
2.执行SQL语句,获得ResultSet。
3.关闭数据库。
那么,这个ResultSet还能使用吗?还能进行遍历吗?

简单代码如下:
if (this.connection == null)
{
this.connection = DriverManager.getConnection(this.connectionString, this.connectionName, this.connectionPassword);
}

if (this.preparedStatement != null)
{
this.preparedStatement.close();
this.preparedStatement = null;
}
this.preparedStatement = connection.prepareStatement(sql);

ResultSet resultSet = this.preparedStatement.executeQuery();

if (this.connection != null)
{
this.connection.close();
this.connection = null;
}

resultSet.next();
...全文
185 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
huaya1127 2007-10-13
  • 打赏
  • 举报
回复
绝对不能用了
billysky 2007-10-13
  • 打赏
  • 举报
回复
不能遍历了...
pc144818 2007-10-13
  • 打赏
  • 举报
回复
resultSet.next();
把这段程序执行结束后再关闭;
楼上说的都对`
暗夜星辰 2007-10-13
  • 打赏
  • 举报
回复
this.connection.close();

resultSet.next();

连接关闭后,ResultSet就不可以用了
xiyuan1999 2007-10-13
  • 打赏
  • 举报
回复
resultSet.next();

应该放在关闭链接的前面
xiyuan1999 2007-10-13
  • 打赏
  • 举报
回复
if (this.connection == null)
{
this.connection = DriverManager.getConnection(this.connectionString, this.connectionName, this.connectionPassword);
}

if (this.preparedStatement != null)
{
this.preparedStatement.close();
this.preparedStatement = null;
}
this.preparedStatement = connection.prepareStatement(sql);

ResultSet resultSet = this.preparedStatement.executeQuery();

if (this.connection != null)
{
this.connection.close();
this.connection = null;
}

resultSet.next();

干嘛这样写

这样在resultSet.next();前已经关闭了

不行的
ustbsjl 2007-10-13
  • 打赏
  • 举报
回复
关闭连接后resultSet不可用了,用CachedRowSet替代即可!

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧