Pages

Sunday, 31 July 2022

JDBC

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

Description: 
1.Application: It is a java applet or a servlet that communicates with a data source.
2.The JDBC API: The JDBC API allows Java programs to execute SQL statements and retrieve results. Some of the important classes and interfaces defined in JDBC API are as follows:
3.DriverManager: It plays an important role in the JDBC architecture. It uses some database-specific drivers to effectively connect enterprise applications to databases.
4.JDBC drivers: To communicate with a data source through JDBC, you need a JDBC driver that intelligently communicates with the respective data source.

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 4 types of JDBC drivers:
1.Type-1 driver or JDBC-ODBC bridge driver
2.Type-2 driver or Native-API driver
3.Type-3 driver or Network Protocol driver
4.Type-4 driver or Thin driver 
 
TYPE 1 Driver:

  • 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.

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. They are as described below:  
1.Two-tier model: A java application communicates directly to the data source. The JDBC driver enables the 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 has the data source running acts as the server.
 
2.Three-tier model: In this, the user’s queries are sent to middle-tier services, from which the commands are again sent to the data source. The results are sent back to the middle tier, and from there to the user. 
This type of model is found very useful by management information system directors.

How many ways are there to register a driver in Java: (Registering the Driver) :

To connect with a database using JDBC you need to select get the driver for the respective database and register the driver. You can register a database driver in two ways −
Using Class.forName() method − The forName() method of the class named Class accepts a class name as a String parameter and loads it into the memory, Soon the is loaded into the memory it gets registered automatically.
Class.forName("com.mysql.jdbc.Driver");
Example
Following the JDBC program establishes a connection with the MySQL database. Here, we are trying to register the MySQL driver using the forName() method.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class RegisterDriverExample {
   public static void main(String args[]) throws SQLException {
      //Registering the Driver
      Class.forName("com.mysql.jdbc.Driver");
      //Getting the connection
      String mysqlUrl = "jdbc:mysql://localhost/mydatabase";
      Connection con = DriverManager.getConnection(mysqlUrl, "root", "password");
      System.out.println("Connection established: "+con);
   }
}
 

No comments:

Post a Comment

Servlet - JSP Programs

JSP(Java Server Pages) Programs : JavaServer Pages (JSP) is a Java standard technology that enables you to write dynamic data-driven pages f...