Prepared statement initialized tiwice and closed once

Posted by sonam on Stack Overflow See other posts from Stack Overflow or by sonam
Published on 2010-05-19T06:58:12Z Indexed on 2010/05/19 7:00 UTC
Read the original article Hit count: 212

Filed under:

Hi,

I want to know that if a PreparedStatement object is initialized twice the way shown in code snippet below and closed only once in finally block, will it fail to close? I am not getting any error in this code but will it be a better idea to use 2 different preparedStatements instead of one. I think it fails to close the preparedStatement at #1.

    Connection conn = null;
 PreparedStatement ps = null;
 try {
  conn = getConnection();
  ps = conn.prepareStatement(QueryUtil.UPDATE_POLICY_DETAILS); // #1
  ps.setInt(1, iCancellationPolicyId);
  ps.executeUpdate();

  //some code here


  ps = conn.prepareStatement(QueryUtil.UPDATE_POLICY_CHARGES); // #2
  ps.setInt(1, iCancellationPolicyId);
  ps.executeUpdate();


  //some code here


 } catch (SQLException sqlExp) {
  sqlExp.printStackTrace();
  LOG.fatal(sqlExp);
 } finally {
  conn.close();
                ps.close();

 }

© Stack Overflow or respective owner

Related posts about preparedstatement