JDBC Drivers Questions

What is a JDBC Driver?
----------------------
A JDBC driver is the set of classes that implement the JDBC interfaces for a particular database.
There are four different types of JDBC driver: A Type 1 driver is a JDBC-ODBC bridge driver; this
type of driver enables a client to connect to an ODBC database via Java calls and JDBC -- neither
the database nor middle tier need to be Java compliant.However, ODBC binary code must be
installed on each client machine that uses this driver.

A Type 2 driver converts JDBC calls into calls for a specific database. This driver is referred to as a "native-API, partly Java driver." As with the Type 1 driver, some binary code may be required on the client machine, which means this type of driver is not suitable for downloading over a network to a client.

A Type 3 driver is a JDBC-Net pure Java driver, which translates JDBC calls into a database -independent net protocol. Vendors of database middleware products can implement this type of driver into their products to provide interoperability with the greatest number of database servers.

Finally, a Type 4 driver, or, "native protocol, pure Java" driver converts JDBC calls into the network protocol used by the database directly. A Type 4 driver requires no client software, so it's ideal for deployment to browsers at runtime. Each of these driver types has its own optimal usage scenarios, and will affect the way you deploy a given Java application.

For example, because Type 4 drivers are 100% Java, use Java sockets to connect to the database, and require no client-side data access code, they are ideal for applets or other download situations inside a firewall.

Oracle's JDBC Drivers
---------------------
Oracle provides both Type 2 and Type 4 drivers. All Oracle JDBC drivers support the full JDBC specification, but in addition, they support the extended capabilities of the Oracle database. For example, the JDBC specification doesn't support LOB data, but the Oracle OCI8 JDBC driver does. Oracle's implementation of the Type 2 JDBC driver is referred to as the Oracle "OCI driver," and the version of this driver that supports an Oracle 7 database is the OCI7 driver and the OCI8 supports Oracle 8. These drivers are platform specific; for example, the Windows NT and Windows 95 version of the driver (oci805jdbc.dll.) is implemented as a dynamic link library (DLL) in C. As mentioned previously, Type 2 drivers may require client code. In the case of the OCI8 driver, the clients must have Oracle's Net*8 and all other dependent files loaded.

A common way to implement Oracle OCI drivers is to use Oracle Application Server with the JWeb cartridge on the middle tier and deploy the client presentation logic as an applet; the interaction with the Oracle database is conducted from the middle tier only, with just the results sent to the client applet as pure HTML or Java and HTML. All Oracle drivers are compliant with the Java Development Kit JDK 1.0 and 1.1.x and support the JDBC 1.22 standard. In addition, all Oracle JDBC drivers support data types such as RAW and LONG RAW, ROWID, and RECURSOR, which are supported in Oracle databases but not part of the JDBC standard. Oracle's drivers also support execution of PL/SQL stored procedures and anonymous blocks (for dynamic execution), and
include capabilities such as row pre-fetching, execution batching, and defining query columns to
reduce the network round trips to Oracle database. In addition, the OCI driver for Oracle8 supports oracle data types CLOB, BLOB, NCLOB, and BFILE. The screenshot shows an example of one of the classes in the Oracle JDBC, as part of the class hierarchy from which it descends, as displayed in Oracle's JDeveloper integrated development environment. As you can see, the OraclePreparedStatement class inherits from the java.sql.PreparedStatement class, which in turn inherits from the java.sql.Statement.

Oracle also provides a Type 4 JDBC driver, referred to as the Oracle "thin" driver. This driver includes its own implementation of a TCP/IP version of Oracle's Net8 written entirely in Java, so it is platform independent, can be downloaded to a browser at runtime, and does not require any Oracle software on the client side. This driver requires a TCP/IP listener on the server side, and the client connection string uses the TCP/IP port address, not the TNSNAMES entry for the database name.

1.Add JDBC classes to your Java application or applet class by adding the following statement to your Java source code:

import java.sql.*;

To use the extended capabilities of the Oracle database, you must also import the Oracle
JDBC driver. The statement in Java source looks like this:

import oracle.JDBC.driver.*

2.Load the JDBC driver by including the following statement in your class.

Class.forName("oracle.JDBC.driver.OracleDriver");

You can load the driver from your class-initialization routine.

3.Obtain a connection to an Oracle database by calling the getConnection() method of the
JDBC DriverManager class. When you call this method you need to specify the connection
information for the database in the form of a URL. The form the URL will take depends on
the driver used. For example, to use the pure Java Type 4 Oracle driver (the thin driver) to connect to an Oracle7 database, the URL would read:

jdbc:oracle:thin@database_name:port_no:SID

To connect to an Oracle8 database using the OCI driver, the URL would be more like:

jdbc:oracle:oci8@database_name

To specify the database for use with an OCI driver, you can use either a SQL*Net name-
value pair, or, if you're using an Oracle Name server, you can use the name from the
tnsname.ora file. (Both of these strings would conclude with the logon information as well --
specifically the user name and password -- but we've eliminated that from this example.)
The preliminary driver and database-connection issues now taken care of, there are still
several other things your Java source code must include in order for the compiled code to
submit queries to the database and process results.

4.Create a Statement object by calling the createStatement() method of the Connection object you created in the previous step. The following statement creates a Statement object stmt:

Statement stmt = conn.createStatement ();

5.Once the Statement object exists (in code), the application can then include code to execute a SQL query by calling the executeQuery() method of the Statement object.The executeQuery() method returns the result of the query in the ResultSet object. The following statement executes a query :

ResultSet rset = stmt.executeQuery (SELECT ename from emp where empno = 7900);

6.Finally, call the next() method of a ResultObject to retrieve a row and display it. Use a loop if the query returns more then one row from the database. For example, the following statements get the name of an employee from the ResultSet object and display it in the
java.awt text control placed on the GUI.

rset.next();

enameTxtb.setText = ((String)rset.getString(1));

No comments: