Home » Java Basics » 10 - Java - Database and XML Input
10

JDBC Concepts

A platform-neutral interface between databases and Java

Commercial DBMS have little in common other than the quality of being datastores and similar query languages. Every Database Management System - MS Access, Oracle, MS SQL Server, DB2 and so on possesses its own interface. Knowledge of a DBMS's custom APIs and interface is prerequisite to being able to access and manipulate resident databases. Writing programs that will access data from a variety of DBMS is a formidable task.

JDBC is a platform-neutral interface between databases and Java. JDBC offers the developer a standard set of database access features and the SQL-92 Query Language to send commands to the database. The JDBC API contains interfaces that define database functions such as query execution, result processing, and configuration. JDBC drivers are configured to work with some particular DBMS. An application can use a number of drivers interchangeably. An application using JDBC need not know anything about the underlying DBMS and its API.

Figure 10a: JDBC in a Layered Application
Figure 10a: JDBC in a Layered Application

Before using a driver, it must be registered with the JDBC DriverManager. This may be done by loading the JDBC driver class using the Class.forName( ) method:

try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Class.forName("com.oracle.jdbc.OracleDriver");
}
catch (ClassNotFoundException e) {
/* Handle Exception */
}

The advantage of using Class.forName( ) is that this method accepts a String argument. The application maps the string to the actual location of the Driver class through an entry in a properties file like web.xml. However, most commercial J2EE applications use the JNDI or Java Naming and Directory Interface to access a driver loaded onto a J2EE server. We will use a Type 1 driver that implements a bridge technology to connect Java Applications to an ODBC database system. This JDBC-ODBC Bridge from Sun is the only common example of a Type 1 driver and is bundled with the JDK.