Prerequisites

Classes for use with JDK 1.5. It contains the JDBC driver classes, except classes for NLS support in Oracle Object and Collection types.

ojdbc5_g.jar

Same as ojdbc5.jar, except that classes were compiled with "javac -g" and contain tracing code.

ojdbc5dms.jar

Same as ojdbc5.jar, except that it contains instrumentation to support DMS (i.e., Oracle Dynamic Monitoring Service) and limited java.util.logging calls.

ojdbc5dms_g.jar

Same as ojdbc5_g.jar, except that it contains instrumentation to support DMS.

ojdbc6.jar

Classes for use with JDK 1.6. It contains the JDBC driver classes except classes for NLS support in Oracle Object and Collection types.

ojdbc6_g.jar

Same as ojdbc6.jar except compiled with "javac -g" and contains tracing code.

ojdbc6dms.jar

Same as ojdbc6.jar, except that it contains instrumentation to support DMS and limited java.util.logging calls.

ojdbc6dms_g.jar

Same as ojdbc6_g.jar except that it contains instrumentation to support DMS.

In this project, we have downloaded ojdbc6_g.jar and included it in Eclipse's Java Build Path.

Sample Code

If you are using ojdbc6.jar and JSE 6 or later, you don't have to register the driver at all no matter which driver you are using. As of JSE 6, the standard Java Service Provider Interface registers the drivers automatically. Just call DriverManager.getConnection and the runtime will find the driver and register it for you.

There are four types of drivers supported by Oracle. One of them is thin driver which is a 100% Java driver for client-side use without an Oracle installation, particularly with applets. The Thin driver type is thin.

When the method getConnection is called, the DriverManager will attempt to locate a suitable driver from amongst those loaded at initialization and those loaded explicitly using the same classloader as the current applet or application. In ojdbc6.jar, there is a driver named oracle.jdbc.driver.OracleDriver that implements the java.sql.Driverinterface.

You can open a connection to the database with the static getConnection() method of the java.sql.DriverManager class. The type of the object returned is java.sql.Connection. The form of getConnection() method we use in the example requires us to specify a database URL , user name and password. If you want to connect with the Thin driver, you must specify the port number and SID. For example, to connect user scott with password tiger to a database with SID (system identifier) XE through port 1521 of host xxx.us.oracle.com, using the Thin driver, you would write :

Note that although the method is not creating a new physical connection (only a single implicit connection is used), it is returning a new object.

Connection object represents a connection (session) with a specific database. You can use it to query the information of the database by using getMetaData method. For example, you can query its tables, its supported SQL grammar, its stored procedures, the capabilities of this connection, and so on. You can also use createStatement method to create a Statement object for sending SQL statements to the database:

Statement stmt = con.createStatement();

Statement object provides an executeQuery method that executes the given SQL statement, which returns a single ResultSet object:

A ResultSet object maintains a cursor pointing to its current row of data. Initially the cursor is positioned before the first row. The next method moves the cursor to the next row, and because it returns false when there are no more rows in the ResultSet object, it can be used in a while loop to iterate through the result set:

5 comments:

This post helps you explain how you can establish JDBC connection with Oracle database. Here all steps are given to establish a JDBC connection and also described what JDBC is all about. You can use the code to implement it.sap pp

Disclaimer

The statements and opinions expressed here are my own and do not necessarily represent those of Oracle.For your computer health, follow me @xmlandmore. To improve your personal health, follow me @travel2health.

About Me

Healthy pursuits are like traveling. We know there is a wonderland called wellness. But, there are no fixed routes to reach there. The pursuits need effort and determination. We cannot act like tourists who don't know where they've been. We must take notes of warning signs sent by our bodies.

On the journey to wellness, there are many dangers to be avoided; there are many footsteps to be taken; unfortunately, there are no short-cuts.

Travelers walking in the night follow North Star. Healthy pursuits work similarly. We know our goal; we know the signposts; we know the dangers on the road; we can adjust pace if we are tired; we can change travel plans due to the weather. But, We walk steadily and persistently. With my companionship, hopefully, your journey will be made easier.