0% found this document useful (0 votes)
2 views18 pages

3 Introduction to JDBC

JDBC (Java Database Connectivity) is an API that allows Java applications to interact with various relational databases, providing a standard interface for executing queries and manipulating data. It features platform independence, support for multiple databases, and extensibility for batch processing and transaction management. The document outlines the JDBC architecture, components, and steps to connect to a MySQL database using JDBC, including code examples.

Uploaded by

Sharda Kaur
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views18 pages

3 Introduction to JDBC

JDBC (Java Database Connectivity) is an API that allows Java applications to interact with various relational databases, providing a standard interface for executing queries and manipulating data. It features platform independence, support for multiple databases, and extensibility for batch processing and transaction management. The document outlines the JDBC architecture, components, and steps to connect to a MySQL database using JDBC, including code examples.

Uploaded by

Sharda Kaur
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Introduction to JDBC

By Dr. Jaspreet Singh Bajaj


Introduction

JDBC (Java Database Connectivity) is an API in Java that enables


applications to interact with databases. It allows a Java program to
connect to a database, execute queries, and retrieve and manipulate
data. By providing a standard interface, JDBC ensures that Java
applications can work with different relational databases like MySQL,
Oracle, PostgreSQL, and more.
Features of JDBC

Platform Independence: It enables database operations across


different platforms.
Standard API: It provides a uniform interface for various databases.
Support for Multiple Databases: It works with popular databases like
MySQL, PostgreSQL, Oracle, etc.
Extensibility: It offers features like batch processing, connection
pooling, and transaction management.
Why Should We Use JDBC?

Before JDBC, ODBC(Open Database Connectivity) API was the


database API to connect and execute the query with the database.
But ODBC API uses ODBC driver that is written in C language (i.e.
platform dependent and unsecured). That is why Java has defined its
own API (JDBC API) that uses JDBC drivers (written in Java language).
We can use JDBC API to handle database using Java program and can
perform the following activities:
• Connect to the database
• Execute queries and update statements to the database
• Retrieve the result received from the database.
Application: It is a Java applet or a servlet that communicates with a
data source.
The JDBC API: It allows Java programs to execute SQL queries and
retrieve results. Key interfaces include Driver, ResultSet, RowSet,
PreparedStatement, and Connection. Important classes include
DriverManager, Types, Blob, and Clob.
DriverManager: It plays an important role in the JDBC architecture. It
uses some database-specific drivers to effectively connect enterprise
applications to databases.
JDBC drivers: These drivers handle interactions between the
application and the database.
JDBC Architecture

The JDBC architecture consists of two-tier and three-tier processing


models to access a database. They are as described below:
1. Two-Tier Architecture
A Java Application communicates directly with the database using a
JDBC driver. Queries are sent to the database, and results are
returned directly to the application. In a client/server setup, the user’s
machine (client) communicates with a remote database server.
Structure:
Client Application (Java) -> JDBC Driver -> Database
2. Three-Tier Architecture
In this, user queries are sent to a middle-tier services, which interacts
with the database. The database results are processed by the middle
tier and then sent back to the user.
Structure:

Client Application -> Application Server -> JDBC Driver -> Database
JDBC Components

There are generally 4 main components of JDBC through which it can


interact with a database. They are as mentioned below:

1. JDBC API
It provides various methods and interfaces for easy communication
with the database. It includes two key packages
• java.sql: This package, is the part of Java Standard Edition (Java SE) ,
which contains the core interfaces and classes for accessing and
processing data in relational databases. It also provides essential
functionalities like establishing connections, executing queries, and
handling result sets
Contd…

javax.sql: This package is the part of Java Enterprise Edition (Java EE) ,
which extends the capabilities of java.sql by offering additional
features like connection pooling, statement pooling, and data source
management.
2. JDBC Driver Manager
Driver manager is responsible for loading the correct database-
specific driver to establish a connection with the database. It manages
the available drivers and ensures the right one is used to process user
requests and interact with the database.
It also provides a standard to connect a database to a
client application.
3. JDBC Test Suite
It is used to test the operation(such as insertion, deletion, updating)
being performed by JDBC Drivers.

4. JDBC Drivers
JDBC drivers are client-side adapters (installed on the client machine,
not on the server) that convert requests from Java programs to a
protocol that the DBMS can understand.
There are 4 types of JDBC drivers:

Type-1 driver or JDBC-ODBC bridge driver


Type-2 driver or Native-API driver (partially java driver)
Type-3 driver or Network Protocol driver (fully java driver)
Type-4 driver or Thin driver (fully java driver) – It is deprecated and no
longer supported since Java 8. Instead modern drivers like the Type –
4 driver are widely used.
JDBC Classes and Interfaces
Class/Interfaces Description
Manages JDBC drivers and establishes
DriverManager
database connections.

Represents a session with a specific


Connection
database.
Statement Used to execute static SQL queries.

Precompiled SQL statement, used for


PreparedStatement
dynamic queries with parameters.

Used to execute stored procedures in the


CallableStatement
database.

Represents the result set of a query,


ResultSet
allowing navigation through the rows.

Handles SQL-related exceptions during


SQLException
database operations.
Pre-Requisite to use JDBC

Before moving further, you need to have a good understanding of the


following two subjects −

• Core JAVA Programming

• SQL or MySQL Database


Steps to Connect to MySQL
Database Using JDBC
Step 1: Load the JDBC Driver
Class.forName(“com.mysql.cj.jdbc.Driver”);
Step 2: Establish a Connection
Connection connection = DriverManager.getConnection(
“jdbc:mysql://localhost:3306/your_database”,
“your_username”,
“your_password”
);
Step 3: Create a Statement
Statement statement = connection.createStatement();
Step 4: Execute a Query
String query = “INSERT INTO students (id, name) VALUES (101, ‘Amit’)”;
int rowsAffected = statement.executeUpdate(query);
System.out.println(“Rows affected: ” + rowsAffected);
Step 5: Close the Connection
statement.close();
connection.close();
// Java program to implement a simple JDBC application
import java.sql.*; // Establish connection
public class Geeks { Connection c = DriverManager.getConnection(
public static void main(String[] args) url, username, password);
// Create a statement
{
Statement st = c.createStatement();
// Database URL, username, and password // Execute the query
// Replace with your database name int count = st.executeUpdate(query);
String url System.out.println(
= "jdbc:mysql://localhost:3306/your_database"; "Number of rows affected by this query: "
// Replace with your MySQL username + count);
// Close the connection
String username = "your_username";
st.close();
// Replace with your MySQL password c.close();
String password = "your_password"; System.out.println("Connection closed.");
// Updated query syntax for modern databases }
String query catch (ClassNotFoundException e) {
= "INSERT INTO students (id, name) VALUES (109, System.err.println("JDBC Driver not found: "
'bhatt')"; + e.getMessage());
}
// Establish JDBC Connection
catch (SQLException e) {
try { System.err.println("SQL Error: "
// Load Type-4 Driver + e.getMessage());
// MySQL Type-4 driver class }}}
Class.forName("com.mysql.cj.jdbc.Driver");

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy