views:

42

answers:

4

Dear All,

I would like to know

1.the purpose of statement closing in java

2.what will happen if i didnt give stmt.close() in my program

try{        
    Statement DelQTY=OPConnect.createStatement();
    Statement DelPMQTY=OPConnect.createStatement();
    Statement DelPReq = OPConnect.createStatement();
    Statement DelPro=OPConnect.createStatement();
    Statement DelPPro=OPConnect.createStatement();
    String DelSql="DELETE FROM ITEM_REQUIRES_MAT WHERE ITEM_NO=6364;
    String DelPM="DELETE FROM PROGRAM_MATERIAL WHERE ITEM_NO=6364;
    String DelPReqPro="DELETE FROM PROGRAM_REQ_PRODUCE_MAT WHERE ITEM_NO=6364;
    String DelProcess="DELETE FROM PROCESS_TRANSFORMS_MAT WHERE ITEM_NO=6364;
    String DelPProcess="DELETE FROM PROGRAM_PROCESS_TRANSFORMS_MAT WHERE ITEM_NO=6364;  
    DelPReq.executeUpdate(DelPReqPro);          
    DelQTY.executeUpdate(DelSql);           
    DelPMQTY.executeUpdate(DelPM);          
    DelPro.executeUpdate(DelProcess);           
    DelPPro.executeUpdate(DelPProcess);
    DelQTY.close();
    DelPMQTY.close();
    DelPReq.close();
    DelPro.close();
    OPConnect.close();
}

This is my sample code here i missed DelPPro.close() catch(SQLException e) { e.printStackTrace(); }

+1  A: 

You mean java.sql.Statement? It releases jdbc and db resources bound by that statement. If you don't do it and it get's garbage collected, this is done then. But I would do it myself as this might throw an exception which I would like to handle myself.

hackbert
A: 

Assuming you are talking about SQL statements...

close() Releases this Statement object's database and JDBC resources immediately instead of waiting for this to happen when it is automatically closed.

Taken from Oracle

npinti
+1  A: 

Releases this Statement object's database and JDBC resources immediately instead of waiting for this to happen when it is automatically closed. It is generally good practice to release resources as soon as you are finished with them to avoid tying up database resources. Calling the method close on a Statement object that is already closed has no effect.

Note: A Statement object is automatically closed when it is garbage collected. When a Statement object is closed, its current ResultSet object, if one exists, is also closed.

Document

org.life.java
If i close the resultset then stmt object will also closed?
Mohan
@Mohan No. The only way to release resources associated with the statement object is to call close() on it.
diciu
+1  A: 

While you can sometimes get away with not closing statements by calling close(), you will run into trouble if the statement execution is done inside a loop.

The usual server side error is in this case "too many open cursors". You will see this in your client as a SQLException.

diciu