Introduction to JDBC
JDBC’s full form is Java Database Connectivity. It is basically a Java API that is used to execute and connect query along with the database. It is considered to be part of Java SE, that is, Java Standard Edition. API of JDBC makes uses of drivers of JDBC in order to get connected along with the database. We can consider four types of JDBC drivers mentioned as below :
- JDBC-ODBC Bridge Driver
- Thin Driver
- Native Driver and
- Network Protocol Driver
JDBC API can be used in order to access the tabular data stored in any of the relational databases. By using JDBC API, you have the ability to save, update, delete and also to fetch the data from the database. It is considered to be Open Database Connectivity, that is, ODBC provided by Microsoft.
- Understanding JDBC
How JDBC works?
Establishing a connection with an application or a data source, sending queries, updating statements, and processing results can be easily done using JDBC or Java Database Connectivity. It has made all the things mentioned below possible within a Java application –
- It helps in establishing a connection easily with a data source
- It makes sending queries and updating statements possible
- It helps in fetching data from the database and processing the fetched results in a very simple way.
The SQL statements and queries can be submitted and results can be retrieved by calling the JDBC classes and interfaces with the help of Java application. It is the JDBC driver that helps in the implementation of the JDBC API. This driver constitutes a set of classes that will implement the JDBC interfaces. This will further help in processing the JDBC calls and sending output/result to the Java application. The database is basically a data store where all the data that is retrieved using the JDBC driver by the Java application is stored.
Why should we use JDBC?
Before JDBC came into existence, we used the ODBC API database in order to connect as well as to execute queries along with the database. However, ODBC API makes use of ODBC drive in C language. Also, it is platform-dependent and in addition, unsecured. This is the reason that Java defined its own API known as JDBC API using JDBC drivers and also it is written in Java language.
We can perform various activities using the JDBC API required to handle database:
- Connection to database
- Execution of queries as well as update statements to the database
- Retrieving results fetched from the database.
Architecture of JDBBC
JDBC API supports two-tier as well as three-tier processing models for availing database access. However, generally speaking, Its Architecture has two layers listed below:
- JDBC API: This layer supports the connection to application-to-JDBC Manager. It makes use of the driver manager as well as database-specific drivers so as to give transparent connectivity to databases that are heterogeneous.
- JDBC Driver API: This layer provides the connection of JDBC Manager to Driver. This driver manager makes sure that the correct driver is being used in accessing each of the data sources. It is also capable of supporting many concurrent drivers that are connected to various heterogeneous databases.
JDBC API consists of following interfaces as well as classes which can be termed as components as well :
- Driver Manager: Driver Manager class as name suggests is responsible for managing the list of drivers of the database. It also matches the connection requests coming from the java application along with database driver with help of sub-protocol of communication. The first driver which recognizes subprotocol under the JDBC is used to establish database connection.
- Driver: The interface that handles the communications happening between the application and the database server is called the Driver. The chances of interacting with the Driver objects directly is very rare as most of the time the objects of Driver Manager are generally used in order to manage the objects of this type. All the detail=- that are associated with the working of Driver objects are extracted by it.
- Connection: Connection interface along with all the methods is used for contacting the database. The object of connection represents the context of communication, that is, all of the communication along with the database is only through the object of connection.
- Statement: The objects that are created from this interface will make it possible to submit the SQL statements to the database. Some of the derived interfaces also accept parameters while executing some stored procedures.
- ResultSet: ResultSet objects are used in holding the data that is retrieved from the database but that happens after you have executed an SQL query with the help of Statement objects. It also acts as an iterator that allows us to move through the data.
- SQL Exception: SQL Exception class is used in handling any of the errors which occur in the database application.
Advantages and Disadvantages
Below are the advantages and disadvantages:
Some of the advantages of using mentioned below:
- It is capable of reading any database. The only requirement for it to do so is the proper installation of all the drivers.
- It automatically creates the XML format of data from the database.
- It does not require the content to be converted.
- It provides full support to query and stored procedure.
- It provides support to both Synchronous and Asynchronous processing.
- It supports modules.
Just like most of the API’s, It also has some cons. Some of these disadvantages are mentioned below:
- It is very sensitive when it comes to the driver. Hence, it is very important to install correct drivers and to deploy them for each type of database in order to make use of it. This is a time taking task and challenging at times.
- It does not allow a single sequence to update or insert multiple tables.
Thus, It helps you in connecting to a database and thereby executing SQL statements against the database. JDBC api also as discussed gives a set of interfaces and also different implementations respective to their databases.
This has been a guide to What is JDBC. Here we have discussed the Architecture, Components, Advantages, and Disadvantages of JDBC. You can also go through our other suggested articles to learn more –