JDBC or Java Database connectivity is a Java API for connecting java applications with any kind of tabular data sources such as Relational databases, spreadsheets, or even flat files. It is used to create connections or fire queries on a database.
What is the need for JDBC?
Let’s understand it with the help of an example, you’re creating a simple java application where you have different users with different dashboards, which means every user will have their own credentials to log in. Now, you need to maintain the records for managing multiple users where you’d need to save their email addresses and passwords.
This means that you need a database that can store users’ login credentials as well as their details. Here, with the help of HTML, CSS, and Java you created the front end, but alone they cannot help you save the user’s data and for that, you’d need a database to store the data.
Let’s suppose we are using SQL(Structured Query Language) to create a database but java doesn’t know how structured query works, right! Here comes JDBC API to the rescue, JDBC API helps java applications to interact with the databases.
JDBC helps you to write java applications that can incorporate databases and their functionalities, the three functionality JDBC can provide you are:
- Creating connection to a data source, Database
- Querying and updating Databases
- Retrieve and process the result of your database
What Are The Components of JDBC?
There are four components of JDBC:
- JDBC API: The JDBC API provides java applications to access the relational data using java programming language. With the help of JDBC API, Applications can execute SQL statements, retrieve data from the database and perform changes over the database using java applications. There are two packages which come with JDBC API , java.sql and javax.sql. These packages are imported so that their classes and interfaces could be utilized for database connectivity and query. Both the packages are present in Java SE and Java EE platforms.
- JDBC Driver Manager: The JDBC Driver Managers class, as the name suggests, describes objects to connect java applications to JDBC Driver.
- JDBC Driver Test Suite: JDBC driver test suite basically helps you to check if the JDBC driver will run on your application or not.
- JDBC-ODBC Bridge: Using JDBC-ODBC bridge is used for accessing ODBC databases. It’s obsolete after JDK 8 as it had limited drivers and the connectivity could only be possible with ODBC Drivers, Oracle suggests to use JDBC drivers from database vendors or third parties.
JDBC Drivers are the software that helps java applications to interact with databases. There are 4 types of JDBC Drivers:
- Type 1 – JDBC-ODBC bridge driver
- Type 2 – Native-API driver
- Type 3 – Network Protocol Driver
- Type 4 – Thin Driver
Introduction to the JDBC Architecture
There are two architectures of JDBC:
- Two-tier Architecture
- Three-tier Architecture
The two-tier architecture of DBMS works on the client-server model. In this architecture, a java application or applet communicates directly to the data source which means that a user can directly access the database using the java application. The Database is within the server-side and the user interface and application reside within the client-side. In this tier APIs like ODBC, JDBC is used.
The three-tier architecture comprises client, server, and middle layer components. The client cannot directly communicate to the database server, instead, a middle tier or application server is used for communication.
The query from the user is sent through the middle tier and the middle tier then passes it on to the data source. The data source then processes the query and sends it back to the middle tier, which then passes it on to the client. This type of architecture is generally used for large web applications.
Resources for more details on JDBC are as follows: