What is JDBC?JDBC ( Java Database connectivity ) is an API ( Application Programming Interface ), which consists of set of java classes, interfaces and exceptions. It allows the programmer to connect java application to the database. It provides methods for querying and updating data into the database. It is relational database oriented.
Why Use JDBC?
JDBC helps the java developers to develop a data access application without having knowledge of complex API’s of different database vendors. You only need to learn JDBC to develop java Data Access Application to different database to different database applications using different JDBC drivers.
JDBC attempts to create a platform neutral interface between database and java. JDBC API defines a set of interface that encapsulates major database functionality such as running query, updating results. A database vendor writes JDBC driver, which consists a set of classes and interfaces that implements interfaces of their database application.
JDBC API supports both 2-tier architecture and 3-tier architecture for accessing relational database model.
JDBC 2- Tier Architecture
In 2-tier architecture java application communicates directly with database. The database may reside on same machine or other machine in the network. The client will send the statement to database and result will communicated back to the client.
JDBC 2-Tier Architecture
JDBC 3- Tier Architecture
In 3-tier architecture a ‘middle-tier’ is used between java application and database. A java application sends a command to middle-tier, which then further sends to database. The database processes the query and returns the result to the middle-tier, which further returns to the application. This approach has the many advantages such as better maintainability, easier deployment, scalability, etc.
JDBC 3-Tier Architecture
As you knew that JDBC API is a collection of java classes and Interfaces. java.sql.*; and javax.sql.*; package contains the collection of JDBC APIs.
Following are the important classes and interfaces of java.sql.*; package.
DriverManager – It is very important class, which defines object to open a connection to the database. It is also used to load JDBC driver in memory.
Connection – The java database application manages the connection to a database. It represents a collection to the database. It is also used for creating Statement, PreparedStatement and CollableStatement Objects.
Statement – Statement object is used to retrieve a result into ResultSet. It represents a static SQL statement.
PreparedStatemet- It represents a precompiled SQL statement. It is alternative to Statement.
CallableStatement- It represents a stored procedures execute stored them in RDBMS.
ResultSet –It represents a set of results generated by SELECT Sql statement.
SQLException- JDBC provides SQLException class, which contains the database access errors. It is a core JDBC class that provides database access errors. Many JDBC API’s throws SQLException.
JDBC Driver Types
There are 4 types of JDBC drivers available.
Type 1: JDBC-ODBC Bridge driver (Bridge) – They are JDBC-ODBC bridge drivers. They are slowest among all the four types. They delegate the work to ODBC API, i.e. they translates the all the JDBC calls into ODBC calls and send them to ODBC driver.
Type 1: JDBC-ODBC Bridge
Type 2: Native-API/partly Java driver (Native)- The type 2 driver mainly uses native API for data access. They provides java wrapper classes to be able to invoke JDBC driver. The type 2 drivers are specific to particular database application.
Type 2: Native-API/partly Java driver
The native API must be installed on client system to use type 2 driver, if database changes the native API must be change. They are not threadsafe.
Type 3: AllJava/Net-protocol driver (Middleware)- Type 3 driver are written purely in java. The type 3 database request are passes throw the network to the middle-tier server and this middle-tier server translates the request to the database.
Type 3: AllJava/Net-protocol driver
Type 4: All Java/Native-protocol driver (Pure)- The most commonly used driver is Type 4 driver. They are also written purely in java.
Type 4: All Java/Native-protocol driver
Recommend the tutorial