关闭连接后第二次调用PreparedStatement不起作用

发布于 2024-12-04 20:59:10 字数 2582 浏览 0 评论 0原文

我正在尝试执行第二次 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

幽梦紫曦~ 2024-12-11 20:59:10

PreparedStatement 实例据我所知,它们与用于准备它们的连接有关。关闭连接后,您将无法使用 PreparedStatement,即使您稍后打开另一个连接也是如此。

PreparedStatement instances are tied to the connection used to prepare them, as far as I'm aware. You can't use the PreparedStatement after closing the connection, even if you open another one later.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文