if ( conn != null ) // close connection conn.close();
在此行
conn不能 为null。直到Java 6为止,最受欢迎的模式是:
Connection conn = null;try { // initialize connection // use connection } catch { // handle exception} finally { if (conn != null) { try { conn.close(); } catch (Exception e) { } }}使用 Java 7 ,它的try-with-
resource结构将变得不那么麻烦。上面的代码可以更改为更短
try (Connection conn = createConnection()) { // use connection } catch { // handle exception}// close is not required to be called explicitly


