You are on page 1of 11

JDBC-java data base connectivity

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.
•JDBC receives queries from Java Application program and
communicate with Database
• All the communications are in the form of SQL commands
• JDBC is responsible for Open a Connection
1. Communicate with database
2. Execute SQL statements
3. Retrieve query result
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


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 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.
There are four types of JDBC drivers
1. JDBC-ODBC Bridge Driver,
2. Native Driver,
3. Network Protocol Driver, and
4. Thin Driver
JDBC Implementation
Seven Steps
– Import java.sql package
– Load and register the driver
– Establish a connection to the database server
– Create a statement
– Execute the statement
– Retrieve the result
– Close the statement and connection
Mysql DB connection steps
1.Cd ..
2.Cd xampp
3.C:\Users\HP>cd "C:\xampp\mysql\bin"
4.C:\xampp\mysql\bin>mysql -u root –p
5.MariaDB [(none)]> show databases;
6.Use dbname;
7.MariaDB [jdbc]> insert into students
values("abi","1ms21cy002",6,27,"ai/ml","19
96-13-21","bellroad");
Database Connection:
The DriverManager.getConnection method is used to establish a connection
to the MySQL database. The jdbcUrl variable contains the JDBC URL, which
includes the database URL (jdbc:mysql://localhost:3306/jdbc), specifying the
host, port, and database name. The username and password variables
provide the credentials for connecting to the database.
Executing SQL Query:
A SQL query is prepared using a PreparedStatement. The query selects all
columns (*) from the "students" table.
ResultSet and Displaying Data:
The executeQuery method is used to execute the query, and the result is
stored in a ResultSet (resultSet). The while loop iterates through the result
set, retrieving values for each student record and displaying them using
System.out.println.
Closing Resources:
The resultSet, preparedStatement, and connection are closed in the finally
block to ensure proper resource management.
Exception Handling:
The code includes a try-catch block to handle SQLException exceptions that
might occur during the database operations. If an exception occurs, the stack
trace is printed.

You might also like