JDBC Driver Types and Database Connection with Java

JDBC Driver Types

There are four different types of JDBC drivers defined by the JDBC specification from Sun.

  • Type 1: JDBC-ODBC Bridge Drivers. These drivers use a bridge or gateway, such as JDBC-ODBC. This is often not the best solution because it may require installing specific software on the client and can be slow.
  • Type 2: Native-API Drivers. This type of driver uses a native API. The driver contains Java code that makes calls to native methods (often C or C++) of the database. Sometimes, client software installation is required.
  • Type 3: Network-Protocol Drivers. These JDBC drivers communicate with an intermediate application server using sockets. The application server then communicates with the database using a specific API driver. This type has the advantage of not requiring any specific software on the client.
  • Type 4: Pure Java Drivers. These drivers use network protocols included in the DBMS (Database Management System) and communicate directly with the database, also using Java sockets. It is the best option because these drivers are written entirely in Java. Most of these drivers are provided by the DBMS manufacturer.

Developing Applications with the JDBC API

One advantage of using the JDBC API is that all functions are the same, regardless of the database. You can program once for any database handler, create an application for MySQL, and then move it to a production server with a more powerful database engine, like SQL Server or Postgres.

Loading the JDBC Driver

The first step is to connect our Java program to the server where our database is located. Often, this requires a username and password.

When using Tomcat, to connect to the database, you need the JDBC driver (which can be a .jar or .class file). This file must be included in the CLASSPATH of the environment. Specifically, the file must be copied to the lib folder within the JSPs interpreter installation folder. For example, the MySQL JDBC API might be in the file mysql-connector-java-3.0.9-stable-bin.jar.

Once you create a connection to the database, you can create statements or run queries from your Java program to interact with it. You can create queries that modify the database without returning any results (e.g., INSERT, ALTER, DELETE, CREATE, DROP, or UPDATE).

The code should have a structure like this:

Statement stmt;
stmt = con.createStatement();
stmt.executeUpdate("INSERT INTO interest (interes_nombre, interes_url, " +
"interes_tipo) VALUES ('" + args[0] + "', '" + args[1] + "', '" + args[2] + "')");

The createStatement() method creates an open channel for running queries.

Running a Query with executeUpdate()

The executeUpdate() method executes the query in the database, while the close() method releases the resources allocated to the open connection.

The following example inserts tuples into a table named interest belonging to the league database.

Running a Query with executeQuery()

Queries that retrieve tuples or records are executed with the executeQuery() method. This method returns a ResultSet object, which can be used to access each of the returned records:

Connection conn = null;
Statement stmt = null;
ResultSet rs = null;