I have written the following java code to connect the Oracle Express Edition database…..I have read a lot about closing the connection, statement and result but there are some questions about this issue…
1- I usually test my code several time so If I Don not close the connection and etc. will I face with any problem?!!(because the connection is made on the same variable or object in my code!!!!)
2-how can I understand how many connections exist now?(after testing the code for several time)
3- Should I place CLOSE methods in finally block with a new TRY CATCH block or add THROWS SQLException after main method?!!!! what is the difference between these to implementation?!!!
package mainAssignment;
import java.sql.*;
import java.text.MessageFormat;
import java.math.*;
import java.util.*;
//import java.text.MessageFormat;
public class Conn {
/**
* @param args
*/
public static void main(String[] args)throws SQLException{
// TODO Auto-generated method stub
String jdbcURL = "jdbc:oracle:thin:@localhost:1521:xe";
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
String user = "hr";
String passwd = "hr";
Scanner input = new Scanner(System.in);
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection(jdbcURL, user, passwd);
System.out.println("ok");
System.out.println("Please enter the desired employee ID: ");
Object[] arg={new String(input.nextLine())};
String query = MessageFormat.format(
"select * from EMPLOYEES where employee_id={0}", arg);
//System.out.println(query);
stmt = conn.createStatement();
stmt.executeQuery(query);
rs = stmt.getResultSet();
while (rs.next()) {
BigDecimal empid = rs.getBigDecimal(1);
String firstname = rs.getString("FIRST_NAME");
System.out.println("employee ID " + empid
+ " first name is " + firstname);
}
} catch (SQLException e) {
e.printStackTrace();
// TODO
} catch (ClassNotFoundException e) {
// TODO
e.printStackTrace();
} finally {
conn.close();
rs.close();
if (conn.isClosed())
System.out.println("no connection any more");
else
System.out.println("connection exists");
}
}
}
1) You’ll be left with many idle (hanging) connections after you finish the execution. To prevent that check whether the connection is null. If it is – establish a new one, if it’s not null – use existing.
2) You can check it on the sql server. Example for ms sql server: How to determine total number of open/active connections in ms sql server 2005
3) Putting close methods in finally block is a good practice. Don’t change that. Sometimes finally block also requires its own inner try-catch-finally block.
Side note: if you create methods that throw exceptions you’re not solving any issues. it’s just pushing the need to maintain the exception to the person who’ll use your methods. Sometimes it’s a good approach but more often it is not.
In real life applications you should also check whether the database is available, the connection was established and the query result is not null.
Appendix 1: How to check the number of active connections in oracle express:
How to list active / open connections in Oracle?
http://dbalink.wordpress.com/2008/06/08/find-active-sessions-in-oracle-database/