Where to close java PreparedStatements and ResultSets?

后端 未结 13 2204
忘了有多久
忘了有多久 2020-11-29 02:26

Consider the code:

PreparedStatement ps = null;
ResultSet rs = null;
try {
  ps = conn.createStatement(myQueryString);
  rs = ps.executeQuery();
  // process         


        
13条回答
  •  無奈伤痛
    2020-11-29 02:53

    focus finally clause,

    finally {
       try {
          rs.close();
          ps.close();
       } catch (Exception e) {
          // Do something
       }
    }
    

    I think you have to modify 2 points.

    First, use try & catch again in fainlly clause.

    Second, do rs.close() before doing ps.close().

    fly1997@naver.com

提交回复
热议问题