Tuesday, June 3, 2008

Connecting to an Oracle database using JDBC

Oracle has a proprietary JDBC driver that allows for native Java connections to an Oracle Database. First, you must download Oracle's JDBC Driver here and then include the jar archive into your project.

The code to connect to the database would be as follows:

public void createDBConn() {
Class.forName("oracle.jdbc.driver.OracleDriver");
String url = "jdbc:oracle:thin:@//server.local:1521/prod";
Connection conn = DriverManager.getConnection(url,"scott", "tiger");
conn.setAutoCommit(false);
Statement stmt = conn.createStatement();
ResultSet rset = stmt.executeQuery("select BANNER from SYS.V_$VERSION");

while (rset.next()) {
System.out.println (rset.getString(1));
}
stmt.close();
System.out.println ("Ok.");
}

No comments: