关闭连接后第二次调用PreparedStatement不起作用
我正在尝试执行第二次 PreparedStatement
,但是如果我关闭 DriverManager.getConnection
代码,它就会失败:
public void getRates(String id) throws Exception, DBException {
Connection conn = null;
ResultSet rs = null;
try {
conn = getConnection();
if (ratesQueryStmt == null){
ratesQueryStmt = conn.prepareStatement(ratesQuery);
}
ratesQueryStmt.setString(1, id);
ratesQueryStmt.setQueryTimeout(m_nTimeout);
rs = ratesQueryStmt.executeQuery();
while (rs.next()){
System.out.println("!!!\n\nDATE = " + rs.getString("RATE_DAY") + " PURCHASE_PRICE = " + rs.getString("PURCHASE_PRICE") + " SELLING_PRICE = " + rs.getString("SELLING_PRICE"));
}
}
catch (SQLException e) {
Utility.trace(m_session, "SQL exception - code: "+String.valueOf(e.getErrorCode())+" "+e.getMessage());
throw e;
}
finally {
DBAccess.closeEverything(rs, ratesQueryStmt, conn); //DO NOT WORK BECAUSE OF CLOSING CONNECTION (conn)
}
所以第一次它工作得很好,但是当我尝试调用它时方法两次显示错误:(
DBAccess.getInstance(mySession).getRates("USD"); //WORKS
DBAccess.getInstance(mySession).getRates("EUR"); // NOT WORKING
错误堆栈
java.lang.NullPointerException
at oracle.jdbc.dbaccess.DBDataSetImpl._createOrGetDBItem(DBDataSetImpl.java:825)
at oracle.jdbc.dbaccess.DBDataSetImpl.setBytesBindItem(DBDataSetImpl.java:2520)
at oracle.jdbc.driver.OraclePreparedStatement.setItem(OraclePreparedStatement.java:1248)
at oracle.jdbc.driver.OraclePreparedStatement.setString(OraclePreparedStatement.java:1690)
at asteros.DBAccess.getRates(DBAccess.java:141) //ratesQueryStmt.setString(1, id);
如果我不关闭连接,则
一切正常。谢谢!
UPD: getConnection() 的来源
public Connection getConnection() throws Exception {
Connection conn = null;
try {
Utility.trace(m_session, "DB string: "+m_strDBString+" user: "+m_strUser+" password: "+m_strPassword);
System.out.println("DB string: "+m_strDBString+" user: "+m_strUser+" password: "+m_strPassword);
Driver dr = new oracle.jdbc.driver.OracleDriver();
DriverManager.registerDriver(dr);
conn = DriverManager.getConnection(m_strDBString, m_strUser, m_strPassword);
} catch (Exception e) {
throw new Exception(e);
}
return conn;
}
I'm trying to execute 2nd time PreparedStatement
, but it fails if I close DriverManager.getConnection
the code:
public void getRates(String id) throws Exception, DBException {
Connection conn = null;
ResultSet rs = null;
try {
conn = getConnection();
if (ratesQueryStmt == null){
ratesQueryStmt = conn.prepareStatement(ratesQuery);
}
ratesQueryStmt.setString(1, id);
ratesQueryStmt.setQueryTimeout(m_nTimeout);
rs = ratesQueryStmt.executeQuery();
while (rs.next()){
System.out.println("!!!\n\nDATE = " + rs.getString("RATE_DAY") + " PURCHASE_PRICE = " + rs.getString("PURCHASE_PRICE") + " SELLING_PRICE = " + rs.getString("SELLING_PRICE"));
}
}
catch (SQLException e) {
Utility.trace(m_session, "SQL exception - code: "+String.valueOf(e.getErrorCode())+" "+e.getMessage());
throw e;
}
finally {
DBAccess.closeEverything(rs, ratesQueryStmt, conn); //DO NOT WORK BECAUSE OF CLOSING CONNECTION (conn)
}
So first time it works just fine, but when I try call this method twice it shows error :(
DBAccess.getInstance(mySession).getRates("USD"); //WORKS
DBAccess.getInstance(mySession).getRates("EUR"); // NOT WORKING
error stack
java.lang.NullPointerException
at oracle.jdbc.dbaccess.DBDataSetImpl._createOrGetDBItem(DBDataSetImpl.java:825)
at oracle.jdbc.dbaccess.DBDataSetImpl.setBytesBindItem(DBDataSetImpl.java:2520)
at oracle.jdbc.driver.OraclePreparedStatement.setItem(OraclePreparedStatement.java:1248)
at oracle.jdbc.driver.OraclePreparedStatement.setString(OraclePreparedStatement.java:1690)
at asteros.DBAccess.getRates(DBAccess.java:141) //ratesQueryStmt.setString(1, id);
if I DO not close connection everything works..
Thank you!
UPD: source of getConnection()
public Connection getConnection() throws Exception {
Connection conn = null;
try {
Utility.trace(m_session, "DB string: "+m_strDBString+" user: "+m_strUser+" password: "+m_strPassword);
System.out.println("DB string: "+m_strDBString+" user: "+m_strUser+" password: "+m_strPassword);
Driver dr = new oracle.jdbc.driver.OracleDriver();
DriverManager.registerDriver(dr);
conn = DriverManager.getConnection(m_strDBString, m_strUser, m_strPassword);
} catch (Exception e) {
throw new Exception(e);
}
return conn;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
PreparedStatement
实例据我所知,它们与用于准备它们的连接有关。关闭连接后,您将无法使用PreparedStatement
,即使您稍后打开另一个连接也是如此。PreparedStatement
instances are tied to the connection used to prepare them, as far as I'm aware. You can't use thePreparedStatement
after closing the connection, even if you open another one later.