You are on page 1of 4

Connect to an Oracle database with JDBC - Real's Java How-to

http://www.rgagnon.com/javadetails/java-0112.html

Oracle Shipping Software


Multi-Carrier Compliant Fully Integrated, Priced Right
www.processweaver.com

The JDBC driver for Oracle is available at their Web site. All you need is to include the required jar in the classpath.

import java.sql.*; public class TestDBOracle { public static void main(String[] args) throws ClassNotFoundException, SQLException { Class.forName("oracle.jdbc.driver.OracleDriver"); // // or // DriverManager.registerDriver // (new oracle.jdbc.driver.OracleDriver()); String url = "jdbc:oracle:thin:@//server.local:1521/prod"; // jdbc:oracle:thin:@//host:port/service // or // String url = "jdbc:oracle:thin:@server.local:1521:prodsid"; // jdbc:oracle:thin:@host:port:SID // // SID - System ID of the Oracle server database instance. // By default, Oracle Database 10g Express Edition // creates one database instance called XE. // ex : String url = "jdbc:oracle:thin:@myhost:1521:xe";

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."); } }

1 de 4

02/07/2011 16:09

Connect to an Oracle database with JDBC - Real's Java How-to

http://www.rgagnon.com/javadetails/java-0112.html

You can find the SID in the tnsnames.ora file, ex :

XE = (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = myhost)(PORT = 1521)) (CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = XE) ) )
While it is not to difficult to connect using the example above, it would be nice to connect without having to specify a server and a port number. Since release 10.2.0.1.0, it's possible to give only a TNSNAMES entry and the driver extract the required infos (server and port) for the defined TNSNAMES.ORA file. In order for this to work you must have configured the file TNSNAMES.ORA correctly and set a the java property oracle.net.tns_admin.

import import import import import

java.sql.Connection; java.sql.DriverManager; java.sql.ResultSet; java.sql.SQLException; java.sql.Statement;

import oracle.jdbc.OracleDriver; public class TestOra { public TestOra() { System.setProperty("oracle.net.tns_admin","\\\\myserver\\TNSNAMES_DIR"); // or // java.exe -Doracle.net.tns_admin=\\myserver\TNSNAMES_DIR TestOra ... // } public void doit String usr = String pwd = String url = () throws SQLException { "scott"; "tiger"; "jdbc:oracle:thin:@MYORCL";

DriverManager.registerDriver(new OracleDriver()); Connection conn = DriverManager.getConnection(url,usr,pwd); String sql = "select {fn now()} from dual" ; Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery(sql); while (rs.next()) System.out.println("results: " + rs.getString(1)); conn.close(); } public static void main(String[] args){ TestOra test = new TestOra(); try { test.doit(); System.out.println("Done.."); } catch (SQLException e) {

2 de 4

02/07/2011 16:09

Connect to an Oracle database with JDBC - Real's Java How-to

http://www.rgagnon.com/javadetails/java-0112.html

e.printStackTrace(); } } } /* \\myserver\TNSNAMES_DIR\tnsnames.ora

MYORCL = (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = orcltest.local)(PORT = 1521)) ) (CONNECT_DATA = (SERVICE_NAME = orcl)(INSTANCE_ROLE=ANY)) ) */
See also this HowTo to connect using the Oracle Connection Pool.

Ratings and comments (2)


Average rating:

Want to contribute? Join or Sign In

From this site my maximum problem solved because I am new in development field and I am fresher now.... More Nikhilkumar 11/3 Thanks a million. You really hit the nail on the head. There has not been any clearer explanation. ... More Abayomi 2/9
Translate

SQL Server SSIS Tasks


SSIS Tasks for AS2, SFTP, FTPS, POP3, IMAP, SMTP, SSH, OFTP, etc
www.nsoftware.com/ssis/

Java Reporting Tools


Wow your boss with easy-to-use reporting software. Only $60/year!
www.myeclipseide.com

If you find this article useful, consider making a small donation to show your support for this Web site and its content.

Written and compiled by Ral Gagnon 1998-2011 [ home ]

3 de 4

02/07/2011 16:09

Connect to an Oracle database with JDBC - Real's Java How-to

http://www.rgagnon.com/javadetails/java-0112.html

4 de 4

02/07/2011 16:09

You might also like