读书人

JDBC执行SQL语句后的有关问题

发布时间: 2012-02-19 19:43:39 作者: rapoo

JDBC执行SQL语句后的问题。
我按照下面的操作
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();

[解决办法]
关闭连接后resultSet不可用了,用CachedRowSet替代即可!
[解决办法]
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();前已经关闭了

不行的
[解决办法]
this.connection.close();

resultSet.next();

连接关闭后,ResultSet就不可以用了
[解决办法]
resultSet.next();
把这段程序执行结束后再关闭;
楼上说的都对`
[解决办法]
不能遍历了...
[解决办法]
绝对不能用了

读书人网 >J2SE开发

热点推荐