Java Database Connectivity

Java Database Connectivity 
(JDBC)
  • JDBC stands for Java Database Connectivity. It is a step by step procedure to communicate with the database from the Java application in order to perform database operation. Basically, JDBC is an Application Programming Interface (API) that makes it possible to standardize and simplify the process of connecting Java applications to a database.
  • DBC is considered to be a part of Java platform, Standard Edition (Java SE) uses the Structured Query Languages (SQL) for performing different database queries like access, storage, update or delete.

  • Relational Database Management System (RDBMS) supports Structured Query Language (SQL). Since Java is a platform independent programming language which runs on most platforms, JDBC helps to write a single database application which can run on different platforms and interact with different Database Management Systems.


Architecture of JDBC


 

 JDBC Driver

  • JDBC Driver is a software component that enables java application to interact with the database. There are 4 types of JDBC drivers:

    1.      JDBC-ODBC bridge driver

    2.      Native-API driver (partially java driver)

    3.      Network Protocol driver (fully java driver)

    4.      Thin driver (fully java driver)   

    • JDBC-ODBC Bridge Driver/Type-1 Driver: There are some databases which provide ODBC driver to connect their databases. Here, ODBC stands for Open Database Connectivity. The JDBC-ODBC bridge driver converts JDBC method calls into the ODBC function calls. This is now discouraged because of thin driver.

Type of JDBC-ODBC driver: Native-API Driver

      Advantage: 1) Performance is upgraded compared to JDBC-OBDC/Type 1         driver.

     Disadvantage: 2) Need to install Native API driver on each of the client   machine.

  • Native-API driver:-

The Native API driver uses the client -side libraries  of the  database. This driver converts JDBC method calls into native calls of the database API. In order to interact with different database, this driver needs their local API, that's why data transfer is much more secure as compared to type-1 driver.

    • Driver needs to be installed separately in individual client machines
    • The Vendor client library needs to be installed on client machine.
    • Type-2 driver isn’t written in java, that’s why it isn’t a portable driver
    • It is a database dependent driver.


  • Network Protocol driver

The Network Protocol driver uses middleware (application server) that converts JDBC calls directly or indirectly into the vendor-specific database protocol. It is fully written in java.


Network Protocol driver

Advantage:

  • No client side library is required because of application server that can perform many tasks like auditing, load balancing, logging etc.

Disadvantages:

  • Network support is required on client machine.
  • Requires database-specific coding to be done in the middle tier.
  • Maintenance of Network Protocol driver becomes costly because it requires database-specific coding to be done in the middle tier.

 

  • Thin Driver :-
  •  The thin driver converts JDBC calls directly into the vendor-specific database protocol. That is why it is known as thin driver. It is fully written in Java language

Thin driver

Advantage:

  • Better performance than all other drivers.
  • No software is required at client side or server side.

Disadvantage:

  • Drivers depend on the Database.

Need of JDBC

It is important to understand  why  we need Java Database connectivity. Java  applications are required to connect with database. Java applications are written in Java programming language, but the database only understands Structured Query Language (SQL). In order to establish a connection between Java application and database, JDBC is used. JDBC contains a set of interface and classes which helps to connect Java application to the database.

JDBC components:

  1. DriverManager: DriverManager is a class which manages all the database drivers. The DriverManager is used to load the specific database drivers in an application to establish a connection with the database.
  2. Driver: Driver is the interface which manages the communications happening between the application and the database server.
  3. Connection: Connection is an interface which contains methods for contacting the database.
  4. Statement: Statement is an interface that creates an object to submit SQL queries or statements to the database.
  5. ResultSet: ResultSet contains the results that are retrieved from the database after the execution of SQL statements or queries.
  6. SQLException: SQLException class is used to handle any of the errors that occur in a database application.      

Java Database Connectivity with 5 Steps:-

Figure- Java Database Connectivity


    There are 5 steps to connect any java application with the database using JDBC. These steps are as follows:-

  1. Load the driver class: Load the JDBC driver class into Java Virtual Machine (JVM). The forName() method of class is used to load the driver class.
  2. Establish the connection: Here a connection between a Java program with the database is established using getConnection() method provided by DriverManager class.
  3. Create statement: In this step, the statement object is created using createStatement() method of connection interface. This statement object is used to interact with the database.
  4. Execute query: This step involves executing queries to the database and getting  the output by using executeQuery() method Statement interface.
  5. Close the connection: The close() method provided by Connection interface is used to close the connection.

  There are three types of Statement available in Statement class:- 

  1. Statement
  2. PreparedStatement
  3. CallableStatement 

  •   Types of Statements:-

  1. Statement:- This represent a simple sql/mysql statement . Statement stmt=con.createStatement(); 
  2. PreparedStatement :- This represent precompiled sql/my sql statement which allows improved performance. It allows to execute the query multiple times and we can set the values according to our need. PreparedStatement psmt=con.prepareStatement();
  3. CallableStatement :- This allows the access of stored procedures; that are stored on the database. CallableStatement csmt=con.prepareCall();


  • Advantages of Java Database Connectivity :-
     Java Database Connectivity comes up with several advantages, some of them are:

1.      JDBC itself creates XML format of data from the database automatically

2.      JDBC supports modules

3.      JDBC provides better security

4.      JDBC completely supports query and stored procedure

5.      JDBC supports both types of processing i.e. Synchronous and Asynchronous processing

6.      JDBC does not require content conversion.


 Difference between JDBC and ODBC drivers:

As we know, that Java Database Connectivity (JDBC) is an API for Java Programming Language used to connect the Java application to the database, whereas Open Database Connectivity (ODBC) is a standard application programming interface used for connecting application and database.
Let us look into some of the differences between JDBC and ODBC.

JDBCODBC
JDBC is platform independent.ODBC is platform dependent.
Code written in JDBC is easy to understand.Code written in ODBC is complex to understand.
JDBC can be used only with Java programming language.ODBC can be used in C, C++, Java programming language.

Conclusion:

   JDBC is a very important concept which every Java developer needs to understand to develop projects. We hope this blog helps our reader to understand various concepts related to JDBC.




Comments

  1. Well done Ramkrishna . Keep it up bro 👍

    ReplyDelete
  2. Well done Ramkrishna . Keep it up bro 👍

    ReplyDelete
  3. Well done Ramkrishna . Keep it up bro 👍

    ReplyDelete

Post a Comment