Java  

Typical Uses of JDBC with Architecture

Introduction

The traditional client/server model has a rich GUI on the client and a database on the server. In this model, a JDBC driver is deployed on the client.

Database protocol

However, the world is moving away from client/server and toward a “three-tier model” or even more advanced “n-tier models. “In the three-tier model, the client does not make database calls. Instead, it calls on a middleware layer on the server that in turn makes the database queries. The three-tier model has a couple of advantages. It separates visual presentation (on the client) from the business logic (in the middle tier) and the raw data (in the database). Therefore, it becomes possible to access the same data and the same business rules from multiple clients, such as a Java application applet, or a web form.

Communication between the client and the middle tier can occur through HTTP (when you use a web browser as the client), RMI (when you use an application or applet), or another mechanism. JDBC manages the communication between the middle tier and the back-end database. There are, of course, many variations of this model. In particular, the Java 2 Enterprise Edition defines a structure for application servers that manage code modules called Enterprise JavaBeans and provides valuable services such as load balancing, request coaching, security, and simple database access. In that architecture, JDBC still plays an important role in issuing complex database queries.

HTTP

For Example Consider a program that connects to a MySQL database using JDBC and creates a STUDENT table with columns for student ID, name, course, and marks.

import java.sql.*;
public class Temp {
    public static void main(String args[]) {
        Connection con = null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
              con = DriverManager.getConnection(
                "jdbc:mysql://localhost:3306/your_database", "root", "password");
              Statement stmt = con.createStatement();
              String sql = "CREATE TABLE STUDENT (" +
                         "STU_ID INT, " +
                         "STU_NAME VARCHAR(32), " +
                         "COURSE VARCHAR(12), " +
                         "MARKS FLOAT)";
            stmt.executeUpdate(sql);
            System.out.println("TABLE IS CREATED…..");
        } catch (ClassNotFoundException e) {
            System.out.println("JDBC Driver not found: " + e);
        } catch (SQLException e) {
            System.out.println("SQL Error: " + e);
        } finally {
              try {
                if (con != null) con.close();
            } catch (SQLException e) {
                System.out.println("Error closing connection: " + e);
            }
        }
    }
}

This program establishes a connection to a MySQL database using JDBC (Java Database Connectivity) and creates a table named `STUDENT` within the specified database. It begins by loading the MySQL JDBC driver using Class.forName() and then connects to the database using DriverManager.getConnection() with appropriate credentials (`localhost`, database name, username, and password). Once the connection is established, it creates a Statement object and executes an SQL CREATE TABLE command to define the STUDENT table with four columns: STU_ID (integer), STU_NAME (32-character string), COURSE (12-character string), and MARKS (floating-point number). The program handles possible exceptions such as driver not found or SQL errors and ensures that the database connection is closed properly in the final block.

JDBC Architecture

The JDBC API supports both two-tier and three-tier processing models for database access but in general JDBC Architecture consists of two layers.

  1. JDBC API: This provides the application to the JDBC Manager connection.
  2. JDBC Driver API: This supports the JDBC Manager-to-Driver Connection.

The JDBC API uses a driver manager and database-specific drivers to provide transparent connectivity to heterogeneous databases.

The JDBC Driver Manager ensures that the correct driver is used to access each data source. The driver manager is capable of supporting multiple concurrent drivers connected to multiple heterogeneous database.

Following is the architectural diagram, which shows the location of the driver manager with respect to the JDBC drivers and the Java application

JDBC drivers

The diagram shows how multiple databases can be accessed through their respective JDBC Drivers using a single JDBC API interface, ensuring platform independence and flexibility in Java-based database applications.

Summary

This Article provides an overview of JDBC (Java Database Connectivity) and its typical uses within client/server and multi-tier architectures. It explains how JDBC facilitates database access from Java applications by using drivers and APIs. Initially, JDBC was used in the traditional client/server model, where the client directly communicates with the database. However, the document highlights a shift toward the three-tier model, where the client interacts with a middleware layer (business logic), which then communicates with the database improving scalability and separation of concerns. The architecture section outlines the roles of the JDBC API, Driver Manager, and database-specific drivers, emphasizing platform independence, flexibility, and transparent database access. Overall, the document underscores JDBC's central role in enterprise Java applications and multi-database systems.