Introduction to Java JDBC
JDBC stands for Java DataBase Connectivity. JDBC API is a Java API which is used to access tabular data, especially which are stored in the relational database. JDBC works with Java on platforms like Windows, MacOS, and UNIX.
What is JDBC?
JDBC is used for connecting java programming language with databases.
JDBC library has APIs for each task, which is mentioned below.
- Creating a connection to a database.
- Creating statements for SQL or MySQL.
- Executing queries using SQL or MySQL in the database.
- Viewing & modifying the resulting records.
JDBC provides a complete set of an interface which allows access to the database. It can also be used by different types of executables, they are as follows:
- Java Applications
- Java Applets
- Java Servlets
- Java ServerPages (JSPs)
- Enterprise JavaBeans (EJBs).
All these executables are able to use a JDBC driver to access a database.
The java.sql package contains classes and interfaces for JDBC API. Following is a list of some interfaces of JDBC API:
- Driver interface
- Connection interface
- Statement interface
- PreparedStatement interface
- CallableStatement interface
- ResultSet interface
- ResultSetMetaData interface
- DatabaseMetaData interface
- RowSet interface
Following is a list of some interfaces of JDBC API:
- DriverManager class
- Blob class
- Clob class
- Types class
JDBC Architecture consists of two layers:
- JDBC API: It provides the application-to-JDBC Manager connection.
- JDBC Driver API: It supports the JDBC Manager-to-Driver Connection.
For transparent connectivity, JDBC API uses a driver manager and database-specific drivers.
JDBC driver manager checks that the correct driver is used to access each data source. Driver manager can support multiple concurrent drivers which may be connected to multiple databases.
JDBC drivers implement the defined interfaces in the JDBC API, for interacting with your database server.
Java.sql package contains classes and their behaviors and their main implementation is done in third-party drivers. The Third-party vendor implements the java.sql.Driver interface in their database driver.
There are four types of drivers as following:
Type 1: JDBC-ODBC Bridge Driver
In Type 1 driver, a JDBC bridge accesses ODBC driver which is installed on each client machine. Data Source Name (DSN) is to be configured on the client machine which represents the target database. This type of driver is only for experimental use.
Type 2: JDBC-Native API
In Type 2 driver, JDBC API calls are converted into native C/C++ API calls, these calls are unique to the database. These types of drivers are provided by database vendors and it is used in the same manner as the JDBC-ODBC Bridge. This driver which is provided by vendors must be installed on each client machine. If the database is changed then the native API also must be changed, because native API is specific to the database.
Type 3: JDBC-Net pure Java
In Type 3 driver, to access the database, a three-tier approach is used. Standard network sockets are used by JDBC clients to communicate with the middleware application server. These middleware application servers translate socket information into call format which is required by the DBMS and then forward it to the database server. As it does not requires any code installation on the client that is why this kind of drivers are extremely flexible.
Type 4: 100% Pure Java
In Type 4 driver, pure Java-based driver communicates with the vendor’s database directly through a socket connection. Type 4 driver has the highest performance and it is provided by the vendor itself. It does not require special software on the client or server that is why it is extremely flexible and also these drivers can be downloaded dynamically.
Advantages of JDBC in Java
- If proper drivers are installed, any database can be read.
- Creates XML structure of data from the database automatically.
- No content conversion required.
- Query and Stored procedure supported.
- Can be used for both Synchronous and Asynchronous processing.
- Supports modules.
Disadvantages of JDBC in Java
- Correct drivers need to be deployed for each type of database.
- Cannot update or insert multiple tables with a sequence.