JDBC  «Prev  Next»
Lesson 7 A sample program
ObjectiveWrite a simple yet complete JDBC program.

Sample JDBC Program

View the Code below to see a Java program that issues a message when it successfully connects to the chosen database.
import java.sql.*;
public class Example1 {

public static void main(java.lang.String[] args) {
 //Perform all method calls within try/catch blocks to intercept any exceptions raised
 try {
  // Load the driver for our database
 catch (ClassNotFoundException e) {
  System.out.println("Exception: Cannot Load JDBC Driver");
 try {
  // Create a Connection to the database, pointing to the table used.
  // Supply this information in the URL format required by our Driver.
  Connection con = DriverManager.getConnection("jdbc:cloudscape:d:/cloudscape/demo/databases/toursDB");
  System.out.println("Our connection was successful!");
  // Close and release the database resources.
 catch (SQLException e) {
  // Display any SQL errors
  System.out.println("SQL Exception: " + e.getMessage());

Now you can see how the connection is established. In the first "try" block, we load the driver for the database we are using. In the second, the DriverManager obtains the connection with a single statement.

Details about the connection

The process shown in the code is straightforward, but might seem a bit mysterious. How, for example, does the DriverManager know about the driver you are using? There appears to be no explicit notification to the DriverManager about the driver.

Driver-Manager notification

The notification happens "behind the scenes." The interface for a driver specifies that it register itself with the DriverManager at the time the driver is loaded. Any operation that loads the driver will also advise the DriverManager of the existence of that specific driver. This is done with a static initializer method.
In the code above, you see a customary way to accomplish that with the Class.forName() method call. Occasionally, you may see a variation on this method invocation. Some Java virtual machines do not call a class's static initializers until an instance of that class is actually instantiated. Because of that, the newInstance()method call is often added to the form above:

All this implies is that the DriverManager keeps a list of drivers. You have seen one way to place an entry in that list.

Additional registration mechanisms

There are two additional mechanisms for registering drivers with the DriverManager. First, you could use new to load and instantiate a driver directly in the application. This is usually not done because it is inflexible. It requires a recompile and potential rewrite of the code if the driver class changes.
The second mechanism uses a feature of the DriverManager. The DriverManager will automatically register drivers referenced in the jdbc.drivers property. Those properties can be specified either on the command line of the JVM or in a properties file.

Alternative connection

Finally, there is an alternative to the DriverManager for making a JDBC connection. The JDBC 2.0 specification provided for DataSources for making connections. You will explore those later in this course. The next lesson wraps up this module.

Jdbc Connection - Exercise

Click the Exercise link below to create a connection.
JDBC Connection - Exercise