Introduction to JDBC (Java Database Connectivity):
JDBC or Java Database Connectivity is a Java API to connect and execute the query with the 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. It is used to write programs required to access databases. JDBC, along with the database driver, can access databases and spreadsheets. The 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 the application to send requests made by users to the specified database.
Purpose of JDBC :
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 database.
Components of JDBC
There are generally four main components of JDBC through which it can interact with a database
JDBC API: It provides various methods and interfaces for easy communication with the database. It provides two packages as follows, which contain the java SE and Java EE platforms to exhibit WORA(write once run everywhere) capabilities.java.sql.*;
2. It also provides a standard to connect a database to a client application.
3. JDBC Driver manager: It 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.
4. JDBC Test suite: It is used to test the operation(such as insertion, deletion, updation) being performed by JDBC Drivers.
5. JDBC-ODBC Bridge Drivers: It connects database drivers to the database. This bridge translates the JDBC method call to the ODBC function call. It makes use of the sun.jdbc.odbc package which includes a native library to access ODBC characteristics.
Architecture of JDBC
Type- 1 drivers is also known as a JDBC-ODBC bridge driver.
It is developed by sun microsystems and supplied as a part of JDK.
Internally this driver takes the help of ODBC driver to communicate with the database.
The Type- 1 driver converts all JDBC calls into ODBC calls and sends them to ODBC driver.
The ODBC driver converts all ODBC calls into database specific calls.
This Type- 1 driver acts as the bridge between JDBC and ODBC hence the name came into the picture.
Advantages:
Type- 1 driver is very easy to use and maintain.
Using Type- 1 driver we can access any data base.
It is available as part of JDK and hence, we are not required to install it separately.
Disadvantages:
The performance is very less. Because, first it converts JDBC calls into ODBC driver converts ODBC calls into database specific calls.
Type- 1 drivers may not be suitable for large scale applications.
TYPE 2 Drivers:
It is also known as Native-API partly java driver.
Type-2 driver is similar to Type-1 driver expect that ODBC driver is replaced with database vendor specific native library.
Native libraries are set of functions written in non java.
We have to install vendor provided native libraries on the client machine.
Type-2 drivers convert JDBC calls into vendor specific native library calls.
The native library calls can be understandable directly by database.
Advantages:
It provides better performance than Type-1 driver. Because it required only one level conversion from JDBC to native library calls.
No need of ODBC driver.
Disadvantages:
It is database dependent driver, because it internally uses database native libraries.
It is platform dependent driver.
We have to install native libraries on the client machine.
TYPE 3 DRIVERS (NET PROTOCAL DRIVER):
It follows 3-tier architecture where JDBC requests are passed through the network to middle tier server.
The middle tier server translates the request to database specific library and then sends it to the database.
The database server executes the request and gives back the result.
Advantages:
This driver is server based, so no need for vendor database library to present on the client machine.
Disadvantages:
Type-3 drivers require database specific coding to be done in the middle tier.
Maintenance of the middle tier becomes costly.
TYPE 4 DRIVERS: (THIN DRIVER)
It is also known as pure java driver or thin driver.
It uses database specific native protocol to communicate with the database.
It converts JDBC calls into database specific calls directly. So that client applications communicate directly with the data base server.
It is developed only in java and hence it is also known as pure java driver.
It is a platform independent driver.
This driver won’t require any ODBC driver or native libraries or middle ware server at client side and hence it is also called ad thin driver.
Advantages:
It has better performance than type-1, type-2 and type-3 driver. Because it won’t require ODBC driver or native libraries or middle ware server.
It is platform independent driver.
Disadvantages:
It is database dependent driver, because it communicates directly with the database.
The user needs a different driver for each database.
For example thin driver for oracle and connector/I of MYSQL.
It provides better performance than Type-1 driver. Because it required only one level conversion from JDBC to native library calls.
No need of ODBC driver.