Monday, March 19, 2007

Oracle Thin Client JDBC

I've had problems setting connections to an Oracle DB using the thin client JDBC. This post is to track how this was solved.

First you must have the oracle.jar file in your build path.

Next I used the following for the actual connection:



public static Connection getConnection() {

Connection con = null;
try{
Class.forName("oracle.jdbc.OracleDriver");
} catch (Exception e) {
System.out.println("Failed to load driver.");
return con;
}
try {
con = DriverManger.getConnection(
"jdbc:oracle:thin:username/pw@hostname:port:SID"
);
} catch (SQLException ee) {
ee.printStackTrace();
}
return con;
}


I also had imported java.sql.*;

The thing that was causing a problem with this before was that I did not have the host name, port or SID listed out. This was easily located in the tnsnames.ora file in the oracle installation folder.