开发者

2nd time calling PreparedStatement is not working after closing connection

开发者 https://www.devze.com 2023-04-05 15:03 出处:网络
I\'m trying to execute 2nd time PreparedStatement, but it fails if I close DriverManager.getConnection

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 = getConnect开发者_运维技巧ion();

            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;
}


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号