Hi, Jawed:

I think you just want to know why it close conn in the end. 

(1) If you use DB connection pool(DBCP) to visit DB, conn.close() will put this 
instance to pool and can be used next time.
(2) If you don't use DBCP in your app, conn.close() will release the resource 
used by this instance, esp. a socket connection, or a file resource.

------------------                               
Kenneth Huang
2006-06-16

-------------------------------------------------------------
-----Original Message-----
From:Jawed Nazar Ali
Sent: 2006-06-15 23:00:06
[EMAIL PROTECTED]
Subject:DBCP - Why we close connection


Hello,


        try {
            conn =
DriverManager.getConnection("jdbc:apache:commons:dbcp:example");
            stmt = conn.createStatement();
            rset = stmt.executeQuery(args[1]);
            int numcols = rset.getMetaData().getColumnCount();
            while(rset.next()) {
                for(int i=1;i<=numcols;i++) {
                    System.out.print("\t" + rset.getString(i));
                }
                System.out.println("");
            }
        } catch(SQLException e) {
            e.printStackTrace();
        } finally {
            try { rset.close(); } catch(Exception e) { }
            try { stmt.close(); } catch(Exception e) { }
            try { conn.close(); } catch(Exception e) { }
        }

 

Why we close conn at the end, I don't understand this. Any explanation?


Regards,

Jawed Nazar Ali

 



Reply via email to