Previous | Table of Contents | Next |
The configuration of the JDBC drivers is covered in the installation, but is so simple it can be summed up in two steps:
Testing Connectivity
Here are the three steps for using JDBC/OCI8:
1. Connection conn = DriverManager.getConnection (jdbc;oracle;scott/tiger); 2. Connection conn = DriverManager.getConnection (jdbc;oracle:,scott,tiger); 3. Connection conn = DriverManager.getConnection (jdbc;oracle:@database,scott,tiger); 4. java.util.Properties info = new java.util.Properties(); info.addProperty (user, scott); info.addProperty (password,tiger); Connection conn = DriverManager.getConnection (jdbc;oracle:,info);
Listing 26.1. (Numbering is for reference only)
import java.sql.*; class Employee { public static void main (String args []) throws SQLException, ClassNotFoundException { // #1 // Load the Oracle JDBC driver Class.forName (oracle.jdbc.driver.OracleDriver); // Connect to the database // You can put a database name after the @ sign in the connection URL // we have used local as our database name. Scott and tiger are the user // name and password // #2 Connection conn = DriverManager.getConnection (jdbc;oracle;oci8:@local, scott, tiger); Statement stmt = conn.createStatement (); // #3 ResultSet rset = stmt.executeQuery (select username from all_users); // #4 while (rset.next () ) System.out.println (rset.getString (1)); stmt.close (); } }
Although JDBC is a universal data access method for Java, there are several capabilities and limitations that are worth mentioning.
Capabilities
Firewalls: Currently, Net8 (including JDBC Thin drivers) has the capability to connect through much of the most popular firewall software (Checkpoint, SunSoft, CISCO Systems, Milkyway Networks, Trusted Information Systems, Raptor, Secure Computing Corporation, and Global Internet). If you require the capability of connecting through a firewall, you should contact the firewall vendor to see what configuration is needed.
Considerations
Database Version Support The Thin JDBC driver supports only Oracle7.2 database versions and later; it can also access Oracle8 databases.
Although JDBC covers most functionality needed to access Oracle8, there are some Oracle-specific capabilities built into Oracles JDBC drivers.
You can convert a REFCURSOR value returned by a PL/SQL block into a ResultSet. JDBC enables you to call a stored procedure that executes a query and returns a ResultSet. The following example demonstrates the use of REFCURSORs. Make sure to add import oracle.jdbc.driver.*; to the beginning section of your Java code so that the Oracle JDBC driver classes are imported for use in your code.
CallableStatement callStmt; ResultSet resultCursor; // Use a PL/SQL block to open the cursor callStmt = conn.prepareCall(begin open ? for select cust_name from customer; end;); callStmt.registerOutParameter (1, OracleTypes.CURSOR); callStmt.execute (); resultCursor = ((OracleCallableStatement)callStmt).getCursor (1); // Use the cursor like a ResultSet while (resultCursor.next ()) {System.out.println (resultCursor.getString (1));}
Previous | Table of Contents | Next |