用Java關閉數據庫連接我有點糊涂了,我正在讀下面的文章http:/en.wikipara.org/wiki/java_Database_ConnectivityConnection conn = DriverManager.getConnection(
"jdbc:somejdbcvendor:other data needed by some jdbc vendor",
"myLogin",
"myPassword" );Statement stmt = conn.createStatement();try {
stmt.executeUpdate( "INSERT INTO MyTable( name ) VALUES ( 'my name' ) " );} finally {
//It's important to close the statement when you are done with it
stmt.close();}不需要關閉Conn連接嗎?如果連接()沒有發生,到底會發生什么呢?我有一個私人網絡應用程序,我認為它目前沒有關閉任何一種形式,但重要的是真正的一個,康涅狄格,還是兩者兼而有之?該網站一直斷斷續續地關閉,但服務器一直說這是一個數據庫連接問題,我懷疑它沒有被關閉,但我不知道誰關閉,如果有的話。
3 回答

米琪卡哇伊
TA貢獻1998條經驗 獲得超6個贊
Connection
close()
ResultSet
, Statement
Connection
finally
Connection conn = null;PreparedStatement ps = null;ResultSet rs = null;try { // Do stuff ...} catch (SQLException ex) { // Exception handling stuff ...} finally { if (rs != null) { try { rs.close(); } catch (SQLException e) { /* ignored */} } if (ps != null) { try { ps.close(); } catch (SQLException e) { /* ignored */} } if (conn != null) { try { conn.close(); } catch (SQLException e) { /* ignored */} }}
finally
} finally { try { rs.close(); } catch (Exception e) { /* ignored */ } try { ps.close(); } catch (Exception e) { /* ignored */ } try { conn.close(); } catch (Exception e) { /* ignored */ }}
finally
} finally { DbUtils.closeQuietly(rs); DbUtils.closeQuietly(ps); DbUtils.closeQuietly(conn);}
DbUtils

千萬里不及你
TA貢獻1784條經驗 獲得超9個贊
Statement
Connection
ResultSet
java.sql.ResultSet
:
當語句對象關閉、重新執行或用于從多個結果序列檢索下一個結果時,生成該語句對象的語句對象將自動關閉ResultSet對象。
添加回答
舉報
0/150
提交
取消