Thursday, January 21, 2010

JDBC

JDBC
The Java Database Connectivity API (JDBC) is used to connect Java programs with databases.

The API provides for a call level API for SQL based database access. It allows you to establish a connection with a database, send SQL statements, and process the results.

The JDBC API consists of two major sets of interfaces; the first is for application writers and the second is the JDBC driver for driver writers.

The connection to the database can be through direct connections to the database, through client libraries, or through JDBC-ODBC bridges.

The bridge connection is typically used for a device that does not have client libraries or native connections supported.

The different Oracle JDBC drivers are

Thin - pure Java driver used on the client side that does not need an Oracle client installation. It is recommended that you use this driver unless you need support for non-TCP/IP networks because it provides for maximum portability and performance.
Oracle Call Interface driver (OCI). This uses the Oracle client installation libraries and interfaces. If you want to support connection pooling or client side caching of requests, use this driver. You will also need this driver if you are using transparent application failover (TAF) from your application as well as strong authentication like Kerberos and PKI certificates.
JDBC-ODBC bridge. This uses the ODBC driver in Windows to connect to the database

The JDBC drivers that come with the 10gR2 Client and server are compatible with JDK 1.2.x and later. The JDK 1.0.x and 1.1.x are no longer supported.

A sample code segment for connecting to a database from Java looks like:


String url = "jdbc:odbc:mydatabase";
Connection con = DriverManager.getConnection(url, "userID", "password");
if (con) {
System.out.println(" connection good");
} else {
System.out.println(" connection failed");
}


In this example we use the ODBC-JDBC bridge to connect to the database using the ODBC data source named mydatabase.

We could have used "jdbc:dbnet://mydatabase.oracle.com:1521/mydatabase" to connect directly to the listener.

We could also add attributes to the ODBC connection to define things like CacheSize, AlternatServer, or connection timeout.

To create a failover or load balance definition for a JDBC connection you can do this with the following url definitions


String url = "jdbc:inetora:localhost:1521:mydatabase?failover=true&host1 =standbyDB.oracle.com&port1=1521&host2=remoteDB.oracle.com&port2=1521"; String url = "jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)
(HOST=example.com)(PORT=1521))
(ADDRESS=(PROTOCOL=TCP)
(HOST=failover.example.com)(PORT=1521))
(FAILOVER=true)(LOAD_BALANCE=false)
(CONNECT_DATA=(SERVER=DEDICATED)(SID=foo)))";

No comments: