Where to close java PreparedStatements and ResultSets?

后端 未结 13 2145
忘了有多久
忘了有多久 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

    I know this is an old question, but just in case someone is looking for the answer, java now has the try-with-resouce solution.

    static String readFirstLineFromFile(String path) throws IOException {
          try (BufferedReader br =
                       new BufferedReader(new FileReader(path))) {
            return br.readLine();
        }
    }
    

提交回复
热议问题