Thursday 15 March 2012

java - closing a resultset after return statement -


I have created a function in Java to reuse every table reading database. My problem is that the return I do not know about closing the connection after statement.

  readTable as public static result (string query) {// TODO auto-generated method stub result rs = null; Try {class.forName ("com.mysql.jdbc.Driver"); Connection conx = DriverManager.getConnection ("jdbc: mysql: // localhost: 3306 / cbr_flexible_db", "root", ""); Description st = conex.createStatement (); Rs = st.executeQuery (query); } Hold (exception e) {e.printStackTrace (); } Returns RS; }  

I think if you add a block at the end, then do this work Should:

  try {class.forName ("com.mysql.jdbc.Driver"); Connection conx = DriverManager.getConnection ("jdbc: mysql: // localhost: 3306 / cbr_flexible_db", "root", ""); Description st = conex.createStatement (); Rs = st.executeQuery (query); Returns RS; } Hold (exception e) {e.printStackTrace (); } Finally {conex.close (); }  

No comments:

Post a Comment