Where to close java PreparedStatements and ResultSets?

后端 未结 13 2262
忘了有多久
忘了有多久 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 03:00

    I usually have a utility method which can close things like this, including taking care not to try to do anything with a null reference.

    Usually if close() throws an exception I don't actually care, so I just log the exception and swallow it - but another alternative would be to convert it into a RuntimeException. Either way, I recommend doing it in a utility method which is easy to call, as you may well need to do this in many places.

    Note that your current solution won't close the ResultSet if closing the PreparedStatement fails - it's better to use nested finally blocks.

提交回复
热议问题