JDBC: Interaction With Database

OMKAR KULKARNI
5 min readJun 10, 2021

--

JDBC is an acronym for Java Database Connectivity. JDBC is a Java API for connecting to a database and running queries on it. It’s a component of JavaSE (Java Standard Edition). To connect to a database, the JDBC API utilises JDBC drivers.

It is a Java application programming interface (API) that specifies how a client can interact with a database. It is a Java-based data access technology that is used to link Java databases. Oracle Corporation’s Java Standard Edition platform includes it.

The JDBC API is a Java API for accessing tabular data, particularly data stored in a Relational Database. JDBC is compatible with Java on a variety of systems, including Windows, Mac OS, and various UNIX versions.

Purpose of JDBC

Enterprise applications built with the JAVA EE platform must communicate with databases in order to store application-specific data. As a result, communicating with a database necessitates effective database connectivity, which can be accomplished with the ODBC (Open database connectivity) driver. This driver is used in conjunction with JDBC to connect or communicate with a variety of databases, including Oracle, MS Access, Mysql, and SQL Server.

Photo by Mark Fletcher-Brown on Unsplash

Why should you learn JDBC?

Java Database Communication, or JDBC, is a standard Java API providing database-independent connectivity between the Java programming language and a variety of databases.

Each of the actions listed below that are often connected with database usage have APIs in the JDBC library.

  1. Making a database connection.
  2. Creating SQL or MySQL statements is a common task.
  3. In the database, running SQL or MySQL queries.
  4. Viewing and editing the records that result.
Photo by Dewang Gupta on Unsplash

Components of JDBC:

JDBC is made up of four basic components that allow it to interact with a database. The following is a list of them:

(1) JDBC API:

It offers a variety of methods and interfaces for interacting with databases.

It includes the following two packages, which contain the Java SE and Java EE platforms to demonstrate WORA (write once run anywhere) capabilities.

(2) JDBC Driver Manager:

This component loads database-specific drivers into an application in order to connect to a database. It’s used to process the user’s request by making a database-specific call to the database.

(3) JDBC Test Suite:

It is used to test JDBC Drivers’ operations (such as insertion, deletion, and updating).

(4) JDBC-ODBC Bridge Drivers:

It connects database drivers to the database. This bridge converts ODBC function calls to JDBC method calls.

Types of JDBC:

JDBC drivers are divided into four categories:

  1. Bridge Driver for JDBC and ODBC
  2. Native Driver
  3. Network Protocol Driver and
  4. Thin Driver

The JDBC API can be used to access tabular data in any relational database. We may store, update, remove, and get data from the database using the JDBC API. It’s similar to Microsoft’s Open Database Connectivity (ODBC).

The key packages for JDBC 4.0 are java.sql and javax.sql. At the time of writing, this was the most recent JDBC version. It includes the most important classes for interfacing with data sources.

The JDBC 4.0 Packages

Changes in the following categories are among the new features in these packages:

The database driver is loaded automatically.

Exceptional handling has been improved.

BLOB/CLOB functionality has been improved.

Enhancements to the connection and statement interfaces.

Support for a national character set.

ROWID access in SQL.

Support for XML data types in SQL 2003.

Functionality

Multiple implementations of JDBC (‘Java Database Connectivity’) might exist and be utilised by the same application. The API includes a mechanism for dynamically loading and registering Java packages with the JDBC Driver Manager. The Driver Manager is used to create JDBC connections as a connection factory.

JDBC connections allow you to write and execute statements. These could be update statements like CREATE, INSERT, UPDATE, and DELETE in SQL, or query statements like SELECT. A JDBC connection can also be used to access stored procedures.

The following classes are used by JDBC to represent statements:

1) Statement

Every time the statement is executed, it is sent to the database server.

2) PreparedStatement

The statement is cached, and the execution path is pre-determined on the database server, allowing it to be run several times in a cost-effective manner.

3) CallableStatement

It is a database statement that can be used to execute stored procedures.

Update statements like INSERT, UPDATE, and DELETE provide an update count that shows how many rows in the database were affected. There is no more information returned by these statements.

A JDBC row result set is returned by query statements. The result set is walked over using the row result set. Individual columns in a row can be retrieved by column number or by name. The result set can have any number of rows. The information in the row result set describes the names and kinds of the columns.

In the javax.sql package, there is an extension to the core JDBC API.

Rather than obtaining connections directly from the driver, JDBC connections are frequently managed through a connection pool.

Applications of JDBC

JDBC is a specification that defines a complete set of interfaces for accessing an underlying database from a mobile device. Java can be used to create a variety of executables, including

· Java Applications

· Java Applets

· Java Servlets

· Java ServerPages (JSPs)

· Enterprise JavaBeans (EJBs).

All of these executables can utilise a JDBC driver to connect to a database and use the information contained there.

JDBC is similar to ODBC in that it allows Java programmes to contain database-independent code.

Photo by Luis Villasmil on Unsplash

Conclusion

As we know that the connectivity Java Database (JDBC) is an abbreviation for connectivity in Java database. JDBC is a Java API that connects and performs queries to a database. This is a JavaSE component (Java Standard Edition). The JDBC API uses JDBC drivers to connect to your database.

We got to learn about purpose of JDBC, components of JDBC, Types of JDBC and different different JDBC 4.0 Packages. We also learned about functionality of JDBC and its applications.

--

--