0% found this document useful (0 votes)
5 views32 pages

JDBC Programming Fundamentals in Java

The document provides an overview of Java Database Connectivity (JDBC), detailing its architecture, components, and programming steps for interacting with relational databases. It outlines the roles of various JDBC classes and interfaces, such as DriverManager, Connection, Statement, and ResultSet, as well as the types of JDBC drivers. Additionally, it describes the process of establishing database connections, executing SQL statements, and processing results in Java applications.

Uploaded by

raareetunaa006
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)
5 views32 pages

JDBC Programming Fundamentals in Java

The document provides an overview of Java Database Connectivity (JDBC), detailing its architecture, components, and programming steps for interacting with relational databases. It outlines the roles of various JDBC classes and interfaces, such as DriverManager, Connection, Statement, and ResultSet, as well as the types of JDBC drivers. Additionally, it describes the process of establishing database connections, executing SQL statements, and processing results in Java applications.

Uploaded by

raareetunaa006
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

AMiT

Program:
Computer
Science​
G3 – CS
Java Programming

Instructor:
Addisu M. (Asst.
Prof)
2
Ch ix

Java Database
rS
ap

Connectivity (JDBC)
te
4
Outline
0 JDBC Overview &
Architecture
10
JDBC Setup &

Java Programming
Configuration
2
0 JDBC Programming
Fundamentals
30
Database Operations
Using JDBC
4
Overview of JDBC

Jan 9, 2026
 JDBC (Java Database Connectivity) – Java API for
accessing relational DB
 provides a complete set of interfaces and classes
that allows for portable access and manipulating to a
wide range of relational DBs
 possible – execute SQL statements, retrieve results,
present data in a user-friendly interface, and propagate
changes back to the DB.

Java Programming
 used to interact with multiple data sources in a
distributed & heterogeneous environment
 provides RDBMS access by allowing to embed SQL
inside code
 JDBC library includes APIs for each of the tasks
commonly associated with DB usage
 Making a connection to a DB 5
Overview of JDBC

Jan 9, 2026
 JDBC Classes and Interfaces are in the [Link]
package
 Common JDBC components are:
 DriverManager - Manages loading and unloading of
DB drivers from the underlying system
 Driver - handles the connections with DB server
 Connection - Handles connection to a specific DB

Java Programming
 Statement - Contains SQL statement to be passed to
DB
 ResultSet - Contains the record result set from the
4. Create a Statement
SQL statement passed to the DB
 SQLException - handles any errors
object
that occur in a DB
application 5. Execute a query
Steps in using JDBC 6. Process the results 6
JDBC Architecture

Jan 9, 2026
 relationshipsbetween Java programs, JDBC API,
JDBC drivers, and relational DBs

Java Programming
Architecture of
JDBC

7
JDBC Architecture

Jan 9, 2026
 Case study: Yordi is in AMiT (Main campus) and
wants to buy 2kg of Banana and 5kg of Mango
Vehicle:
from Sikela
My Sikela:
Yordi in requirement
AMiT: Mr. Seid: Vehicle: 2kg Banana
Language Result of my 3kg Mango
Talk in

Java Programming
translator req.
Amharic! Talk in
Road Gamothoo
Yordi in AMiT: Java Application.
Sikela: Database; Banana and Mango are records in database.
Wher Mr. Seid (translator): JDBC Driver Software (eg.:
e: [Link])
Road: Connection 8
JDBC Driver Types

Jan 9, 2026
 What are JDBC drivers?
 software component enabling application to
interact with DB
 To connect with individual DBs, JDBC API requires
drivers for each DB
 gives out the connection to DB and implements the

Java Programming
protocol for transferring the query and result
between client and DB
 enable to open DB connections and to interact with
it by sending SQL or DB commands then receiving
results
9

JDBC Driver Types

Java Programming Jan 9, 2026


10
Some Popular JDBC
Drivers

Jan 9, 2026
RDBM
JDBC Driver Name
S
Driver Name : [Link]
MySQL DB URL format:
jdbc:mysql//hostname/databaseName
Driver Name : [Link]
DB URL format:
Oracle

Java Programming
jdbc:oracle:thin@hostname:portnumber:databaseName
Driver Name : [Link].DB2Driver
DB2 DB URL format:
jdbc:db2:hostname:portnumber/databaseName
Driver Name : [Link]
Access
DB URL format: jdbc:odbc:databaseName 11
JDBC Programming
Steps

Jan 9, 2026
1. Application
causes driver to be
loaded Java application
2. Application asks DriverManager
for a connection to a particular DB.
3. DriverManager DriverManager asks all loaded
gives connection drivers, and one of them responds
to the application. with a connection.

[Link]

Java Programming
[Link]
(JDBC library)

4. Connection
supplied by
Driver for MySQL Driver for Oracle DriverManager is
databases databases used by application
to talk JDBC through
How JDBC establishes a the driver to the
connection between your database.
code and a database
MySQL
DB 12
Primary JDBC classes

Jan 9, 2026
 Java classes used for JDBC
DriverManager:
used to load the class Connection: DB
which is the driver for connection interface
a specific DB

Statement: An SQL
ResultSet: set of

Java Programming
statement to be
results that are
executed on the
connection returned by a query
DataSource:
interface can be
used for any object
that can be used as 13
Developing DB Applications

Jan 9, 2026
Using JDBC
 JDBC API consists of classes and interfaces for
establishing connections with DBs, sending SQL
statements to DBs, processing results of the SQL
statements, and obtaining DB metadata
 Four key interfaces are needed to develop any DB
application:
 Driver, Connection, Statement, and ResultSet
 JDBC interfaces and classes are the building blocks in

Java Programming
the dev’t of Java DB programs 1. Load the JDBC driver
 A typical Java program takes the ff steps to access the
2. Define the Connection
DB URL
3. Establish the
Connection
4. Create a Statement
object 14
Steps in using JDBC

Jan 9, 2026
1. Loading drivers
 An appropriate driver must be loaded using the
statement

[Link]("[Link]");
 Note: Driver
Databa [Link]
Class is aSource
class in mysql-
se [Link]

Java Programming
 drivers
Access for Access, MySQL, and Oracle
[Link] are
Already in listed
JDK
ver
MySQL [Link] Companion
website
Oracle [Link] Companion
ver website 15
Steps in using JDBC

Jan 9, 2026
1. Loading drivers - NetBeans
 Create new project by selecting Java with
Ant  Java Application from Choose
Project window  Give a project name
 Expand the project folder and Right-click
on Libraries under the Projects tab,

Java Programming
select Add JAR/Folder
 Select mysql-connector-java-
[Link] and click open.
 Now you will see the jar file listed
16
under Libraries
Steps in using JDBC

Jan 9, 2026
2. Establishing connections
To connect to DB, use the static method
getConnection(databaseURL) in DriverManager
class:
Connection con =
[Link](databaseURL);
 Syntax or URL for obtaining connection:

Java Programming
 jdbc – main protocol which takes java request and
hand over into DB environment through Data
Source Name.
 odbc – sub protocol which takes DB result & gives
to java env’t 17
Steps in using JDBC

Jan 9, 2026
2. Establishing connections
 lists of URLs for MySQL, Oracle, and Access
databases.
Databas URL Pattern
e
Access jdbc:odbc:DataBaseName
MySQL “jdbc:mysql://hostname:portnumber/

Java Programming
dbname”, “username”, “password”
Oracle jdbc:oracle:thin:@hostname:port#:oracleDBS
ID
 N.B: for Mysql on my local computer:
hostname is localhost
port number is 3306 and 18
Steps in using JDBC

Jan 9, 2026
2. Establishing connections
 To obtain connection from DB, as a part of jdbc we have a
predefined class [Link] which contains
the ff methods:

public static Connection getConnection (String


URL); //if there is no username and password DBs

Java Programming
public static Connection getConnection (String
URL, String username, String password); //if there
is username & password
Connection con1=[Link]
19
Steps in using JDBC

Jan 9, 2026
3. Creating statements
 Once a Connection object is created, you can
create statements for executing SQL
statements as follows: Statement stat =
[Link]();

Java Programming
20
Developing DB
Applications

Jan 9, 2026
4. Executing statements.
 update statement can be executed using
executeUpdate(String sql)
 query statement can be executed using
executeQuery(String sql)
 result of the query is returned in ResultSet
 E.g, create table Temp (col1 char(5), col2

Java Programming
[Link]("create table Temp(col1
char(5)):
char(5), col2 char(5))");

 E.g, select firstName, mi, lastName from Student


// Select the columns from the Student table
where
ResultSetlastName
resSet == ‘Hawa':
[Link] ("SELECT firstName,
mi, lastName FROM Student WHERE 21
Developing DB
Applications

Jan 9, 2026
5. Processing ResultSet
 ResultSet maintains a table whose current row can
be retrieved
 initial row position is null
 You can use next() method to move to the next
row and the various get methods to retrieve
values from a current row

Java Programming
 For example, code given below displays all the

// results from the


Iterate through the preceding SQLthe
result and print query
student names
while ([Link]()){
[Link]([Link](1) + " " +
[Link](2) + ". "
+ [Link](3)); 22
Statements in JDBC

Jan 9, 2026
 Statement is an interface that represents a SQL
statement
 Via statement objects, SQL statements are sent to the
DB
 3 types of statement are available:
 Statement
 For executing a multiple SQL query

Java Programming
 But, every time we execute the query, it needs to be
re-compiled
 It’s the base interface for all other statements
 PreparedStatement
 For executing a precompiled SQL statement passing in
parameters
 Every time we execute the query, it compiled once
 Performance is best than Statement 23
Statements in JDBC

Jan 9, 2026
Interfaces Recommended Use
 Use for general-purpose access to your
DB
 Useful when you are using static SQL
Statement statements at runtime
 Used to implement statements with no
parameters
 Use when you plan to use statements

Java Programming
PreparedStatem  many times
ent accepts input parameters at runtime
 Used for precompiling SQL statements
that might contain input parameters
• can also accept runtime input
CallableStatem • parameters
ent Used to contain stored procedures that 24
Statement

Jan 9, 2026
 Once you've created a Statement object, you can
then use it execute(String
 boolean to execute a statement
SQL):
with one of its
three execute
 Returns methods
a boolean value of true if a ResultSet object
can be retrieved; otherwise, it returns false
 used if the execution produces multiple result sets,
multiple update counts, or a combination of result sets
 int and update counts
executeUpdate(String SQL):

Java Programming
 Returns numbers of rows affected by execution of
statement
 Used to execute SQL statements for which you expect
to get a number of rows affected or update
 E.g., INSERT, UPDATE, or DELETE statement
 ResultSet executeQuery(String SQL):
 Returns a single ResultSet object
 Use this method when you expect to get a result set,
as you would with a SELECT statement 25
Statement

Jan 9, 2026
Creating Statement Object:
 use Connection object's createStatement() method
[Link]();
Closing Statement Object:
 as closing Connection object save DB resources,
for the same reason also close the Statement
object - close() method

Java Programming
Statement stmt = [Link]();
[Link]("INSERT
int res = INTO StInfo VALUES
(1,'Atnafu')");
[Link]("delete from user where UserId=3
");
[Link](res+" records affected");
 getString(columnName) methods – retrieve 26
Statement

Jan 9, 2026
Updat Examp
eStatement stmt = [Link](); le
//[Link]("insert into user values
(3,'Selam','256')");
//int result =

Java Programming
[Link]("update user set username =
‘Selam’, password = ‘123’ where UserId = 4");
int result=[Link]("delete from user w
here UserId = 3");
[Link](result+" records are affected"); 27
Statement

Jan 9, 2026
Retriev Examp
eString QUERY = "select * from user"; le
ResultSet rs = [Link](QUERY);
while ([Link]()) {
int uid = [Link]("UserId");

Java Programming
String uname = [Link]("username");
String pwd = [Link]("password");
[Link]("Id: " + uid + "\n" +
"Username: " + uname + "\n" + "Password: "
+ pwd);
28
PreparedStatement

Jan 9, 2026
 sub-interface of Statement, used to execute
parameterized query & inherits all methods
defined in Statement
 Example: String sql =
"insert into user values(?,?,?)";
setString(int paramIndex, String value)
sets String value to the specified
parameter

Java Programming
Why use PreparedStatement?
Method Description
 Improves performance: performance of the
sets X value to the given parameter index.
application will
setX(int X isbethe
faster
typebecause query(integer,
of parameter is String,
compiled only
paramIndex, once Float)
Double,
X value)  paramIndex is the index of parameter in
statement
executeUpdat Used to execute query for create, insert, update, 29
PreparedStatement

Jan 9, 2026
[Link]("[Link]");
Connection con = [Link]
Examp
le(
"jdbc:mysql://localhost:3306/se",“root","");
PreparedStatement pstmt =[Link](
"insert into user values(?,?,?)"); Insert
[Link](1,8);//1 specifies the first parameter
in the query

Java Programming
[Link](2,"Selam");
[Link](3,"@all");
[Link]();
PreparedStatement pstmt =
[Link](); //Closing PreparedStatement
[Link] ("update user set
Obeject Updat username = ? where UserId = ?");
[Link](1,“Rutha");
e [Link](2,4); 30
PreparedStatement

Jan 9, 2026
Examp
PreparedStatement stmt = [Link]
("select * from user");
le
ResultSet rs = [Link](); Retriv
while([Link]()){ e
[Link]([Link](1) + "" +

Java Programming
[Link](2));
PreparedStatement stmt=[Link]
}
ement ("delete from emp where id=?");
[Link](1,101);
Delete
int i = [Link]();
[Link](i+" records deleted"); 31
ResultSet

Jan 9, 2026
 SQL statements that read data from a DB query
return the data in a result set
 rows that satisfy a particular query are called
result set
 executeQuery() method - used to send query to
DBMS and returns a ResultSet object that contains
data that was requested by the query

Java Programming
 user can access the data in a result set using a
cursor one row at a time from top to bottom
 maintains a cursor that points to current row in the
result set
 initial row position is null
 use next() method to move to the next row 32
33

THANKS
!
Questions,
Ambiguities,
Doubts, … ???

You might also like