我无法找到发生以下异常的位置。我在循环结束后关闭了PreparedStatement
,但在调用sendBill
方法时它仍然给出了错误:
例外文字:
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException:
No operations allowed after statement closed.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.Util.getInstance(Util.java:386)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1015)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:989)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:975)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:920)
at com.mysql.jdbc.StatementImpl.checkClosed(StatementImpl.java:461)
相关代码:
void executeBills()
{
try{
PreparedStatement preStatementUpdate = null;
PreparedStatement preStatementInsert = null;
if(preStatementUpdate==null || preStatementUpdate.isClosed())
preStatementUpdate = con.prepareStatement(sqlUpdatequery);
if(preStatementInsert==null || preStatementInsert.isClosed())
preStatementInsert = con.prepareStatement(sqlInsertQuery);
for(int i=0;i<countrylist.size();i++)
{
country=countrylist.get(i);
//Error shown on this line no.
sendBill(country,preStatementUpdate,preStatementInsert)
}
}catch(Exception e) {
e.printStackTrace();
}finally{
//closing PreparedStatement
preStatementUpdate.close();
preStatementInsert.close();
}
}
void sendBill(String country, PreparedStatement _1, PreparedStatement _2)
{
//Code of PreparedStatement setting String parameter..
//...error shown on this line no.
//getCreditAmount returns int, so String.valueOf is used
_1.setString(1,String.valueOf(country.getCreditAmount()));
_1.executeUpdate() ;
_2.executeUpdate();
//No closing preparedStatement Here
}