JDBC Driver Types

Oracle, as mentioned in the last chapter, provides us with two JDBC drivers an OCI driver and a pure-Java thin driver. These are, respectively, type 2 and type 4 drivers. There are, more generally, four types of drivers available.

Type 1: Bridge driver. The purpose of a JDBC driver is to connect Java code to a database. In many cases, the most expedient way of doing this is to connect Java to an existing driver that uses another comparable technology, such as Microsoft's Open Database Connectivity (ODBC). In fact, the most common driver of this type is a JDBC-ODBC bridge, which maps JDBC calls to ODBC calls and allows a Java program to use databases supporting ODBC.

Type 2: Native API driver. This type of driver, of which the Oracle OCI driver is an example, uses the Java Native Interface (JNI) to allow Java code to connect to the database client API software written for a specific platform and database. In the case of the Oracle OCI driver, it requires that the standard Oracle client software be installed and properly configured. Java, through JNI, calls the Oracle Call Interface binaries.

Type 3: Middleware driver. This is a pure-Java driver (at least on the client side) that connects to a middleware layer, such as BEA's Weblogic. This middleware generally runs on a server of its own and provides the ability to connect to one or more databases, possibly from different vendors. This type of driver is provided by the middleware vendor and is the most flexible because of the additional layer of abstraction that the middleware provides.

Type 4: Pure-Java driver. This is a pure-Java driver that connects directly to the database, using the database's network protocol directly. No additional software or configuration is required on the client.

Oracle recommends using its OCI type 2 driver for best performance, but others have reported a conflicting experience and instead recommend the pure-Java driver. This difference is likely to be application-specific. In any case, if you have serious performance requirements, you should perform your own testing with your own application to decide which to use. The JDBC examples in this book will generally use the thin driver because of its greater portability.

URL Format Options

In addition to the URL format that was used in the last chapter, there are quite a few more that the Oracle drivers accept. I'll describe a few, not just in the interest of thoroughness, but because this may provide a clearer understanding of how the drivers locate and connect to a database.

The Oracle OCI driver and thin driver both accept URLs having the same basic format.

 "jdbc:oracle:driver:@database" 

The driver option can be either thin or oci.

The database option generally differs depending on whether the thin driver or the OCI driver is used. For the OCI driver, the Oracle database's System Identifier (SID) is sufficient. For the thin drive, the database's hostname and port must also be specified in the following format:

 host:port:sid 

This is how we can connect using the thin driver using this format:

 // Thin driver connection: Connection conn = DriverManager.getConnection(     "jdbc:oracle:oci:@noizmaker:1521:osiris",     "scott", "tiger"); 

The OCI driver uses the Oracle client software installed on the client machine so it can use the database's SID to find the database in the same way that other Oracle client software, such as SQL*Plus does using the keyword=value entries in the TNSNAMES.ORA file to identify the hostname and port. If you used the Oracle Universal Installer to set up the connection to your database, this file will have been created for you in the [ORACLE_ HOME]\network\admin directory. Mine has the following entry:

 OSIRIS =   (DESCRIPTION =     (ADDRESS_LIST =       (ADDRESS = (PROTOCOL = TCP)(HOST = noizmaker)(PORT = 1521))     )     (CONNECT_DATA =       (SERVICE_NAME = OSIRIS)     )   ) 

Using the name osiris for the <database> entry is sufficient to connect using the OCI driver.

 //OCI driver connection Connection conn = DriverManager.getConnection(       "jdbc:oracle:oci:@osiris", "scott", "tiger"); 

In addition, either driver will accept a string of keyword=value pairs as the database identifier, like those in tnsnames.ora.

 // OCI driver connection with keyword=value pairs Connection conn = DriverManager.getConnection(   "jdbc:oracle:oci:"   +"@(description=(address=(host= noizmaker)"   +"(protocol=tcp)(port=1521))(connect_data=(sid=osiris)))",    "scott",    "tiger"); //Thin driver connection with keyword=value pairs Connection conn = DriverManager.getConnection(   "jdbc:oracle:thin:"   +"@(description=(address=(host= noizmaker)"   +"(protocol=tcp)(port=1521))(connect_data=(sid=osiris)))",    "scott",    "tiger"); 

Including Username and Password in the URL

In addition to the methods calls shown above, which include the username and password as separate parameters, it is also possible to include username and password as part of the URL. We saw above that all URLs include a database identifier which is preceded by an @ sign. This can be preceded by an optional login parameter.

 login@database 

The login parameter has the following format:

 username/password 

Using this option, a call to get a connection might look like this:

 "jdbc:oracle:thin:scott/tiger@noizmaker:1521:osiris"); 


Java Oracle Database Development
Java Oracle Database Development
ISBN: 0130462187
EAN: 2147483647
Year: 2002
Pages: 71

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net