Contact Us
   
   
 
   
   
 
     
 
 
JSP Tutorial
JDBC Tutorial
 
Book Must Read
 
 

JDBC Close Connection Object

 
 
Print   Email
 
     
 

Close Statement

After completion use of Statement object in java, it should clear from java objects for Garbage collection. Garbage collection automatically free space from memory, give chance to other objects and resources to occupy space in memory. This Statement can be closed by close method of Statement, should be in try catch block. Close Statement help to boost performance of application.

<%
        try{
           if(stm!=null){
            stm.close();
            }
        }
        catch(Exception e)
        {
          e.printStackTrace();
        }
 %>

Close ResultSet

After using ResultSet, ResultSet object in no longer needed, it should be removed from memory. This can be done by close method of ResultSet. Close help to boost performance of application.

<%
        try{
             if(rs!=null){
              rs.close();
             }
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
%>

Close Connection

Every Connection makes an individual session with database. This session reserves particular resources for itself. Every database have default connection pool, Mysql max_connections are 100. Beyond this limit it throws error of “error too many connection in mysql”. Better it to close connection after it use, get better performance. It is good practice to close each and every connection object after it use.

<%
        try{
             if(conn!=null){
              conn.close();
             }
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
%>

Or close all objects simultaneously

<%
     Connection conn = null;
     Statement stm = null;
     ResultSet rs = null;
     
     try{
       conn=/// get connection from connection object
       stm=conn.createStatement();
       rs=stm.executeQuery("Select * from tableName");
     }
     catch(SQLException ex)
     {
       ex.printStackTrace();
     }
 
    try{
         if(stm!=null){
             stm.close();
         }
         if(rs!=null){
             rs.close();
         }
            
         if(conn!=null){
          conn.close();
         }
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
%>

 

 

 

     
 
If You Like this Article, share this

 
 
     
   
     
     
   
     
     
     
 
  Privacy Policy