VOOZH about

URL: https://www.simplilearn.com/tutorials/java-tutorial/java-jdbc

⇱ What is Java JDBC? The Complete Reference


HomeResourcesSoftware DevelopmentJava Tutorial for BeginnersWhat is Java JDBC? The Complete Reference
Tutorial Playlist
Java Tutorial for BeginnersOverview
10 Reasons That Explain Why You Need to Learn JavaLesson - 1What is Java: A Beginners Guide To JavaLesson - 2JDK in Java: Meaning, Components, and UsesLesson - 3One-Stop Solution for Java Installation in WindowsLesson - 4How to Get Started With Eclipse IDE?Lesson - 5What Are Java Strings And How to Implement Them?Lesson - 6Arrays In Java: Declare, Define, and Access ArrayLesson - 7Collections in Java: A Complete Beginner's GuideLesson - 8What Are Java Classes and Objects and How Do You Implement Them?Lesson - 9How to Implement the Revolutionary OOPs Concepts in JavaLesson - 10What is Encapsulation in Java and How to Implement It?Lesson - 11What is an Abstract Class in Java and How to Implement It?Lesson - 12What is Inheritance in Java and How to Implement ItLesson - 13What is Java Interface and Why it's Needed?Lesson - 14What is Polymorphism in Java and How to Implement It?Lesson - 15What is a Java Lambda Expression and How to Implement It?Lesson - 16Your One-Stop Solution for Multithreading in JavaLesson - 17Type Casting in Java: Everything You Need to KnowLesson - 18Scanner In Java: Everything You Need to KnowLesson - 19Access Modifiers in Java: Everything You Need to KnowLesson - 20Armstrong Number in Java: Everything You Need to KnowLesson - 21Singleton Class in Java: Everything You Need to KnowLesson - 22Final Keyword in Java: All You Need to KnowLesson - 23Wrapper Class in Java: A Complete GuideLesson - 24Fibonacci Series in Java: Explained with ExamplesLesson - 25Top 25 Pattern Programs in Java For Printing PatternsLesson - 26Top Brilliant Java Project Ideas For BeginnersLesson - 27Prime Number Program in JavaLesson - 28Java EE Tutorial: All You Need To Know About Java EELesson - 29What is Exception Handling in Java?Lesson - 30What Is Java JDBC? The Complete ReferenceLesson - 31What is Java API and The Need for Java APIs?Lesson - 32Introduction To Java Servlets and Its Life-CycleLesson - 3310 Best Java Frameworks You Should Know in 2026Lesson - 34All You Need to Know to Implement JUnit Testing in JavaLesson - 35What Is Junit? a Look Into the Best Java Testing FrameworkLesson - 36Java Programming: The Complete Reference You NeedLesson - 37The Differences Between C++ and Java That You Need To KnowLesson - 38Java vs. Python: Which is the Best Programming Language?Lesson - 39Java vs JavaScript: Know The 8 Major DifferencesLesson - 40Difference Between Encapsulation and Abstraction ExplainedLesson - 41Ruby on RailsLesson - 42The Best Guide to Know What Is Vue JSLesson - 43

What is Java JDBC? The Complete Reference

Lesson 31 of 43By Simplilearn

Last updated on Sep 16, 2021345049

Table of Contents

View More

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).

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.

Want a Top Software Development Job? Start Here!AI-Powered Full Stack Developer ProgramExplore Program

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.

πŸ‘ Image

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

πŸ‘ Image

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

πŸ‘ Image

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

πŸ‘ Image

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

Process Result

πŸ‘ Image

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

πŸ‘ Image

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

result.close();

Close Connection

πŸ‘ Image

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

πŸ‘ Simplilearn
Simplilearn

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

Recommended Programs

πŸ‘ Full Stack Java Developer Masters Program

Full Stack Java Developer Masters Program

1029 Learners
Lifetime Access*
πŸ‘ AI-Powered Full Stack Developer Program

AI-Powered Full Stack Developer Program

1594 Learners
Lifetime Access*
πŸ‘ Java Certification Training

Java Certification Training

16365 Learners

*Lifetime access to high-quality, self-paced e-learning content.

Explore Category
  • Acknowledgement
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, OPM3 and the PMI ATP seal are the registered marks of the Project Management Institute, Inc.
  • *All trademarks are the property of their respective owners and their inclusion does not imply endorsement or affiliation.
  • Career Impact Results vary based on experience and numerous factors.