首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

java.sql.SQLException:封闭的连接: next

2013-08-21 
java.sql.SQLException:关闭的连接: next我写一个 服务器端的 数据库查询,结果出了问题:java.sql.SQLExcep

java.sql.SQLException:关闭的连接: next
我写一个 服务器端的 数据库查询,结果出了问题:java.sql.SQLException:关闭的连接: next
首先,请看代码:


/***
 *这是查询部分
 */
private ResultSet select(String flag ,int id , String name){
ResultSet rs = null ;
GetConnection getConn = new GetConnection();
Connection conn ;
Statement stat = null ;
String sql = null ;
if( null == flag ) flag = " * ";
if( id < 0 && null == name)sql = "select "+ flag + " from user_inf " ;
if( id >=0 && null == name)sql = "select "+ flag + " from user_inf where id = " + id ;
if( id < 0 && null != name)sql = "select "+ flag + " from user_inf where name = " + parseSqlDate(name) ;
if( id >=0 && null != name)sql = "select "+ flag + " from user_inf where id = " + id + " and name = " + parseSqlDate(name);
System.out.println(sql);
try{
conn = getConn.open();
stat = conn.createStatement();
rs = stat.executeQuery(sql);
r =rs ;
}
catch(Exception err){err.printStackTrace();}
finally{
try {
if( null!=stat || !stat.isClosed())
stat.close();

catch (SQLException e) {e.printStackTrace();}
getConn.close();
}
return r;
}
private ResultSet r = null ;//这是类变量 !


这是测试部分:

package util.db;

import java.sql.ResultSet;
import java.sql.SQLException;

public class test {
public static void main(String[] args){
OperateUser_Inf o = new OperateUser_Inf();
ResultSet rs = o.findAll();
System.out.println(rs);
try {
while( rs.next()){
System.out.print(rs.getInt(1));
System.out.print(rs.getString(2));
System.out.print(rs.getString(3));
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}


}
}
}



这是报错:

java.sql.SQLException: 关闭的连接: next
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:111)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:145)
at oracle.jdbc.driver.OracleResultSetImpl.next(OracleResultSetImpl.java:180)
at util.db.test.main(test.java:12)

注:Debug发现rs存在,不为bull !
错在 rs.next()。

我不懂 ...... 
[解决办法]
你的代码:
        finally{
            try {
                if( null!=stat 
[解决办法]
 !stat.isClosed())
                    stat.close();
            } 
            catch (SQLException e) {e.printStackTrace();}
            getConn.close();
        }
这句话应该是关闭数据库连接的吧?

ResultSet是依赖Connection的,你执行next()的时候,它就要去数据库读取下一条记录。
[解决办法]
如果在rs.next()之前关闭了Connection,会导致:
java.sql.SQLException: 关闭的连接: next问题

如果在rs.next()之前关闭了Statement或PreparedStatement,会导致:
java.sql.SQLException: 关闭的语句: next

如果在rs.next()之前关闭了ResultSet,会导致:
java.sql.SQLException: 关闭的 Resultset: next
[解决办法]
楼上正解。。
[解决办法]
引用:
你的代码:
        finally{
            try {
                if( null!=stat 


[解决办法]
 !stat.isClosed())
                    stat.close();
            } 
            catch (SQLException e) {e.printStackTrace();}
            getConn.close();
        }
这句话应该是关闭数据库连接的吧?

ResultSet是依赖Connection的,你执行next()的时候,它就要去数据库读取下一条记录。


追问下:一般在什么时候才关闭数据库连接呢?或者说,数据库连接搞成固定个数的,一直保存在那里不关闭的?


(to LZ:外层rs的关闭操作可别删了)
[解决办法]
你不把?改成ResultSetjava.sql.SQLException:封闭的连接: next
[解决办法]

(霸占2012全年java月专家排行第一,牛掰~java.sql.SQLException:封闭的连接: next

热点排行