What Is Java JDBC? The Complete Reference

Data is the new fuel that runs the current IT industry, and the data processing applications utilize this data to extract the necessary information. But, the real question is, how do we establish a connection between the data storage unit and the data processing application?

Well, the answer is to this is Java JDBC (Java Database Connection). This Java JDBC article will help you learn about Java JDBC through the following docket.

  • What is Java JDBC?
  • Need for Java JDBC
  • Data Types in Java JDBC
  • Java JDBC Architecture
  • Java JDBC Environment Setup
  • Steps to Connect Java JDBC
  • Java JDBC Example

What Is Java JDBC?

JDBC is an abbreviation for the term Java Database Connection. JDBC is a software tool, also known as an application programming interface that establishes a connection between a standard database and Java application that intends to use that database.

Now that we know the definition of JDBC, let us understand why and where we need it.

Full Stack Java Developer Course

The Gateway to Master Web DevelopmentExplore Course
Full Stack Java Developer Course

Need for Java JDBC

JDBC is used for multiple applications to connect to various types of data storage units that are both standard and advanced. We need JDBC for the following purposes.

  • For establishing stable database connectivity with the application API.
  • To execute SQL(Structured Query Language) queries and DDL/DML commands.
  • For viewing and modify data records in the data storage units. 

So, these were the places and purposes for which we needed a JDBC connection. We will learn the data types supported in SQL and their compatibility with Java over JDBC.

Data Types in Java JDBC

The data types for Java and the data storage unit (here, the SQL) will differ. Hence we need a translator. The JDBC driver takes the responsibility to communicate the data types from Java to SQL to eliminate the ambiguity. 

Some examples of data types used most frequently are organized in the form of a table, as shown below.

Java Data type

SQL Data type

java.lang.string

VARCHAR

boolean

BIT

java.math.BigDecimal

NUMERIC

int

INTEGER

float

REAL

float

FLOAT

double

DOUBLE

byte[]

BINARY

java.sql.Date

DATE

java.sql.Timestamp

TIMESTAMP

java.sql.Array

ARRAY

java.sql.Ref

REF

java.sql.Struct

STRUCT

java.lang,String

CHAR

long

BIGINT

byte[]

VARBINARY

java.sql.Clob

CLOB

java.sql.Blob

BLOB

After learning about the data types in Java JDBC, we will continue learning the architecture of Java JDBC.

Java JDBC Architecture

The Java JDBC architecture consists of the following primary segments. They are:

  • JDBC Application
  • JDBC API
  • JDBC Manager
  • JDBC Drivers
  • Data Storage Units

JDBC_Architecture_Updated

JDBC Application

The JDBC application is in the topmost position of the JDBC architecture. JDBC application is the data processing application that intends to access the data from the different data storage units.

JDBC API

The JDBC API plays a significant role in the JDBC architecture. The JDBC API ensures that a stable connection is established between the data storage unit and the JDBC application.

JDBC Manager

The JDBC manager takes care of selecting the appropriate driver manager to ensure that the driver software chosen supports the data storage unit's requirements to offer an uninterrupted connection.

JDBC Drivers

The JDBC driver is the crucial unit in the JDBC architecture. JDBC driver is the first layer of JDBC architecture that has direct contact with the data storage units.

Data Storage Units

Data storage units are the base of JDBC. The data storage unit is the place where all the data is kept accessible for the JDBC Applications.

Now that we have a technical understanding of the components in the JDBC Architecture. Let us move ahead and understand how to set up the JDBC environment in real-time.

Java JDBC Environment Setup

The environment setup JDBC involves two primary requirements as follows:

In the first stage, you need to install an IDE that can support MySQL in your local system. MySQL WorkBench could be preferable as it provides many features that make writing queries look like child's play.

You can download MySQL Workbench from this link.

Once the setup file is downloaded, the next step is just to run it.

Press next, and then check your file location. By default, the installation will be done in the C drive. We recommend you follow the same route.

Tutorial-set-up_JDBC_Environment

Press next again, and now, select the complete option to have all the features installed. It ideally won’t take too much of your time.

Tutorial-set-up_JDBC_Environment

Take one final review of your installation location and proceed for installation.

_Tutorial-set-up_JDBC_Environment

The installation will initiate, and it will be as shown below.

utorial-set-up_JDBC_Environment_5

The final installation will look like this, as shown below.

JDBC_Tutorial-set-up_JDBC_Environment_6

Press finish and your installation steps are done. And by default, your workbench will launch itself.

Once the MySQL WorkBench is launched, the IDE looks something like this, as shown below.

/JDBC_Tutorial-set-up_JDBC_Environment_7

Here you can create your database using DDL and DML commands. Later, you can create your table and insert data into it. Once your table is ready with the data, you can jump to the next stage.

The second stage is all about installing Java into your local system. You can follow this article by Simplilearn called "How to install Java in Windows." This article will cover the step-by-step approach to install Java in your windows operating system.

We are done with setting up the real-time JDBC environment in our local system. Let us now continue with the steps to be followed to execute a real-time example.

Steps to Connect Java JDBC

There are eight crucial steps to be followed to establish the JDBC connection and process the data. Those steps are as follows:

  • Import-Package Dependencies
  • Load and Register the Driver
  • Connect to the Database
  • Frame Query
  • Execute Query
  • Process Result
  • Close Statement
  • Close connection

Import-Package Dependencies

Whenever we run an additional API package in eclipse, we need to make sure that the application has the support of the dependent classes. Similarly, when we use JDBC API, we need to import the SQL packages to ensure the classes are readily available for the program.

import java.sql*;

Load and Register the Driver

JDBC_load_and_register_Packages

In the next stage, we need to make the program understand that a particular package is imported and get the process of package registration to make it capable of communicating with the JDBC application and the database.

driverManager.registerDriver();

(or)

class.forName();

Connect to the Database

Tutorial-Steps_to_connect_JDBC_connect_database

Here, in this stage, we have the packages, and the loading and registration process is also done. Now, the database is ready for connection. We use a connection method from the Driver Manager to establish a connection with the database.

getConnecttion();

Frame Query

Once the application and database are ready to perform the necessary operation, we need to write in the SQL query and submit it to the JDBC API to run it and get results.

Execute Query

Executing the query is similar to implementing the JDBC API. We can use the executeQuery() command to run the SQL query written.

Process Result

After executing the query, the JDBC system takes care of the next step, which is to process the output and retrieve the necessary results.

Close Statement

Once the statement is made, it needs to be terminated explicitly.

result.close();

Close Connection

The last stage is to terminate the connection. Connection termination can be done by using the following command.

close();

Now, we have a theoretical understanding of the steps. Let us get to the practice mode and execute a program that is given in the next section.

Do you wish to become a Java Developer? Check out the Java Certification Training Course and get certified today.

Java JDBC Example

The following example is based on the JDBC API. Here, we will be creating a university database, and in the university database, we will create a table by name ‘EngineeringStudents’ that will store the student details.

We will then design a JDBC application and establish a connection with the database. Next up, we will be passing our queries to get executed via the JDBC Application.

//Create Database Command

create database University;

//Use Database Command

use University;

//Create Table Command

create table EngineeringStudents(

   Student_ID INT NOT NULL,

   Department VARCHAR(25),

   First_Name VARCHAR(25),

   Last_Name VARCHAR(25),

   PassOutYear INT NOT NULL,

   UniversityRank INT NOT NULL,

   PRIMARY KEY ( Student_ID )

);

//Insert Data Commands

INSERT INTO EngineeringStudents VALUES (10201, 'CSE', 'Kiran', 'Acharya',2018,1272); 

INSERT INTO EngineeringStudents VALUES (10202, 'ISE', 'Chaitanya', 'Pujar',2019,773); 

INSERT INTO EngineeringStudents VALUES (10203, 'Mech', 'Trever', 'Kruger',2020,1275); 

INSERT INTO EngineeringStudents VALUES (10204, 'ECE', 'John', 'Denver',2021,539); 

INSERT INTO EngineeringStudents VALUES (10205, 'Electrical', 'Raju', 'Kumar',2018,098); 

INSERT INTO EngineeringStudents VALUES (10206, 'CSE', 'Jennifer', 'Charles',2018,1372); 

INSERT INTO EngineeringStudents VALUES (10207, 'CSE', 'Chaitanya', 'Pujar',2019,1773); 

INSERT INTO EngineeringStudents VALUES (10208, 'Mech', 'Tom', 'Steven',2020,1223); 

INSERT INTO EngineeringStudents VALUES (10209, 'ECE', 'John', 'Conner',2021,1539); 

INSERT INTO EngineeringStudents VALUES (10210, 'ISE', 'Raju', 'Chaturvedi',2018,1098); 

INSERT INTO EngineeringStudents VALUES (10211, 'CSE', 'Kiran', 'Banerji',2018,1332); 

INSERT INTO EngineeringStudents VALUES (10212, 'ISE', 'Sowmya', 'Kumari',2019,721); 

INSERT INTO EngineeringStudents VALUES (10213, 'ISE', 'Jayson', 'Robert',2020,2275); 

INSERT INTO EngineeringStudents VALUES (10214, 'ECE', 'Praveen', 'Kumar',2021,1249); 

INSERT INTO EngineeringStudents VALUES (10215, 'CSE', 'Rajath', 'Kumar',2018,2218); 

//Queries:

select * from EngineeringStudents;

select Student_ID, Department, First_Name, Last_Name, PassOutYear, UniversityRank from EngineeringStudents where Student_ID = 10202;

select Student_ID, Department, First_Name, Last_Name, PassOutYear, UniversityRank from EngineeringStudents where First_Name = 'Sowmya';

select Student_ID, Department, First_Name, Last_Name, PassOutYear, UniversityRank from EngineeringStudents where PassOutYear = 2018;

select Student_ID, Department, First_Name, Last_Name, PassOutYear, UniversityRank from EngineeringStudents where PassOutYear = 2018 and Department = 'CSE';

select * from EngineeringStudents ORDER BY UniversityRank;

select Student_ID, Department, First_Name, Last_Name, PassOutYear, MIN(UniversityRank) AS Highest from EngineeringStudents;

So, that is how we execute a JDBC program in real-time.

With this, we have arrived at the end of this "Java JDBC" article. We hope you enjoyed understanding the essential concepts of Polymorphism in Java.

Are you are interested in Java Programming Language and getting certified as a professional Java Developer? Then, check out our Java training and certification program curated by the most experienced real-time industry experts.

Got a question on the "Java JDBC" article? Please leave it in the article's comment section below, and we'll have our experts answer it for you right away!

About the Author

SimplilearnSimplilearn

Simplilearn is one of the world’s leading providers of online training for Digital Marketing, Cloud Computing, Project Management, Data Science, IT, Software Development, and many other emerging technologies.

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.