JDBC: Connecting Java Applications to Databases
Introduction to JDBC (Java Database Connectivity)
JDBC stands for Java Database Connectivity. JDBC is a Java API used to connect and execute queries with a database. It is a specification from Sun Microsystems that provides a standard abstraction (API or protocol) for Java applications to communicate with various databases. It provides the language with Java database connectivity standards and is used to write programs required to access databases. JDBC, along with the database driver, can access databases and spreadsheets. Enterprise data stored in a relational database (RDB) can be accessed with the help of JDBC APIs.
Definition of JDBC (Java Database Connectivity)
JDBC is an API (Application Programming Interface) used in Java programming to interact with databases. The classes and interfaces of JDBC allow applications to send requests made by users to the specified database.
The current version of JDBC is JDBC 4.3, released on September 21st, 2017.
Purpose of JDBC
Enterprise applications created using Java EE technology need to interact with databases to store application-specific information. Interacting with a database requires efficient database connectivity, which can be achieved by using the ODBC (Open Database Connectivity) driver. This driver is used with JDBC to interact or communicate with various kinds of databases such as Oracle, MS Access, MySQL, and SQL Server.
Components of JDBC
There are generally four main components of JDBC through which it can interact with a database. They are:
- JDBC API: Provides various methods and interfaces for easy communication with the database. It includes two packages:
java.sql
: Contains interfaces and classes of JDBC API. This package provides APIs for data access and data processing in a relational database, included in Java Standard Edition (Java SE).javax.sql
: Extends the functionality of thejava.sql
package by providing a datasource interface for establishing connection pooling and statement pooling with a data source, included in Java Enterprise Edition (Java EE).
- JDBC Driver Manager: Loads a database-specific driver in an application to establish a connection with a database. It is used to make a database-specific call to the database to process the user request.
- JDBC Test Suite: Used to test the operations (such as insertion, deletion, updating) being performed by JDBC Drivers.
- JDBC-ODBC Bridge Drivers: Connects database drivers to the database. This bridge translates JDBC method calls to ODBC function calls. It uses the
sun.jdbc.odbc
package, which includes a native library to access ODBC characteristics.
Description:
- Application: A Java applet or a servlet that communicates with a data source.
- The JDBC API: Allows Java programs to execute SQL statements and retrieve results.
- DriverManager: Plays an important role in the JDBC architecture. It uses some database-specific drivers to effectively connect enterprise applications to databases.
- JDBC Drivers: To communicate with a data source through JDBC, you need a JDBC driver that intelligently communicates with the respective data source.
Types of JDBC Architecture (2-Tier and 3-Tier)
The JDBC architecture consists of two-tier and three-tier processing models to access a database:
- Two-tier model: A Java application communicates directly with the data source. The JDBC driver enables communication between the application and the data source. When a user sends a query to the data source, the answers for those queries are sent back to the user in the form of results. The data source can be located on a different machine on a network to which a user is connected. This is known as a client/server configuration, where the user’s machine acts as a client, and the machine running the data source acts as the server.
- Three-tier model: The user’s queries are sent to middle-tier services, from which the commands are sent to the data source. The results are sent back to the middle tier and then to the user. This type of model is very useful for management information systems.
What is an API?
Before jumping into JDBC Drivers, let us know more about API.
API stands for Application Programming Interface. It is essentially a set of rules and protocols that transfers data between different software applications and allows different software applications to communicate with each other. Through an API, one application can request information or perform a function from another application without having direct access to its underlying code or the application data.
JDBC API uses JDBC Drivers to connect with the database.
JDBC Drivers
JDBC drivers are client-side adapters (installed on the client machine, not on the server) that convert requests from Java programs to a protocol that the DBMS can understand. There are four types of JDBC drivers:
- Type-1 driver or JDBC-ODBC bridge driver
- Type-2 driver or Native-API driver (partially Java driver)
- Type-3 driver or Network Protocol driver (fully Java driver)
- Type-4 driver or Thin driver (fully Java driver)
Interfaces of JDBC API
A list of popular interfaces of JDBC API is given below:
- Driver interface
- Connection interface
- Statement interface
- PreparedStatement interface
- CallableStatement interface
- ResultSet interface
- ResultSetMetaData interface
- DatabaseMetaData interface
- RowSet interface
Classes of JDBC API
A list of popular classes of JDBC API is given below:
- DriverManager class
- Blob class
- Clob class
- Types class
Working of JDBC
A Java application that needs to communicate with the database has to be programmed using the JDBC API.