天天看點

JDBC的資源釋放

1.1 JDBC的資源釋放

1.1.1 JDBC資源釋放

  • JDBC程式執行結束後,将與資料庫進行互動的對象釋放掉,通常是ResultSet,Statement,Connection。
  • 這幾個對象中尤其是Connection對象是非常稀有的。這個對象一定要做到盡量晚建立,盡早釋放掉。
  • 将資源釋放的代碼寫入到finally的代碼塊中。
  • 資源釋放的代碼應該寫的标準:
if(rs != null){
				try {
					rs.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
				
				rs = null;
			}
			
			if(statement != null){
				try {
					statement.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
				
				statement = null;
			}
			
			
			if(conn != null){
				try {
					conn.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
				
				conn = null;
			}