java.sql.SQLException:关闭语句:下一个
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24693153/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java.sql.SQLException: Closed statement: next
提问by CursedChico
I have the following method which uses JDBC to get data from the DB:
我有以下使用 JDBC 从数据库获取数据的方法:
public List<QueryHolder> getData(String Name) throws SQLException {
OracleDataSource ds;
ds = new OracleDataSource();
String url="jdbc:oracle:thin:@192.***.*.**:1521/dbg";
ds.setURL(url);
Connection conn = null;
conn=ds.getConnection("name","password");
SQL_Str = "SELECT ASM4EVLENMEEHLIYETBELGESI.DAMATSOYAD,ASM4EVLENMEEHLIYETBELGESI.DAMATBABAADI,ASM4EVLENMEEHLIYETBELGESI.DAMATANNEADI FROM ASM4EVLENMEEHLIYETBELGESI where DAMATSOYAD like ('%"
+ Name + "%')";
Statement ps = conn.createStatement();
if (ps == null)
throw new SQLException("Can't get data source");
rs = ps.executeQuery(SQL_Str);
ps.close();
if (con != null) {
con.close();
}
if (rs == null)
throw new SQLException("Can't get data source");
List<QueryHolder> list = new ArrayList<QueryHolder>();
while (rs.next()) { // <-- This is line 89.
QueryHolder que = new QueryHolder();
que.setSoyad(rs.getString("DAMATSOYAD"));
que.setBabaadi(rs.getString("DAMATANNEADI"));
que.setAnneadi(rs.getString("DAMATBABAADI"));
list.add(que);
}
return list;
}
However, when I run this method, it throws the following exception:
但是,当我运行此方法时,它会引发以下异常:
java.sql.SQLException: Closed statement: next
oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:113)
oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:147)
oracle.jdbc.driver.OracleResultSetImpl.next(OracleResultSetImpl.java:187)
Que.Query.getData(Query.java:89)
...
How is this caused and how can I solve it?
这是怎么引起的,我该如何解决?
回答by Andrea
The execution of
的执行
ps.close();
is in the wrong place.
是在错误的地方。
You have to close the Statement after having iterate on its result set (that is, just close ps after the while loop).
您必须在迭代其结果集后关闭语句(即,在 while 循环后关闭 ps)。
回答by SparkOn
It is a good practice to keep you ps.close();
or other closing statements in the finally
block. Instead of handling Exception
on the method label use try and catch
instead.
将您ps.close();
或其他结束语保留在finally
块中是一种很好的做法。而不是Exception
对方法标签进行处理,而是使用try and catch
。
the problem in your case is you closed the Statement before and trying to iterate through the ResultSet to just put your ps.close();
after the while
block
您的情况的问题是您之前关闭了 Statement 并尝试遍历 ResultSet 以将您ps.close();
的放在while
块之后
Edit: do it something like this
编辑:做这样的事情
public returnType methodname()
{
try{
//your boilerplate code
return whateverYouWantToReturn;
}
catch(AppropriateExceptionClass e)
{
e.printStackTrace();
}
finally
{
//closing statements
}
return null;
}
回答by kunwar.sangram
Correct closing order :
正确的关闭顺序:
The following procedures should be done (in order)
应执行以下程序(按顺序)
- ResultSet
- PreparedStatement
- Connection.
- 结果集
- 准备好的语句
- 联系。
Edited code
编辑代码
Statement ps = conn.createStatement();
...
rs = ps.executeQuery(SQL_Str);
...
while (rs.next()) { // <-- This is line 89.
...
}
ps.close();
con.close();
return list;