Java Database Connectivity
Java Database Connectivity
Java Database Connectivity
1
Resources used for this presentation
http://www.cs.huji.ac.il/~dbi/recitations/JDBC-PSQL-c.p
df
http://java.sun.com/docs/books/tutorial/jdbc/
http://www.java-samples.com/showtutorial.php?tutorial
id=202
2
Today’s Menu
JDBC Architecture
Using JDBC
Timeout
ResultSet Object
Null Values
Transactions
3
JDBC (Java Database Connectiveity) is an API
(Application Programming Interface)
That is, a collection of classes and interfaces
4
JDBC Architecture
5
JDBC Architecture
6
“Movies” Relation
moviename producer releasedate
7
7 Steps for Using JDBC
1. Load the driver
2. Define the connection URL
3. Establish the connection
4. Create a Statement object
5. Execute a query using the Statement
6. Process the result
7. Close the connection
8
1. Loading the Driver
Class.forName(“com.mysql.jdbc.Driver ”);
Class.forName loads the given class dynamically
When the driver is loaded, it automatically
creates an instance of itself
Another way:
Driver driver = new com.mysql.jdbc.Driver();
DriverManager.registerDriver(driver);
MySql JDBC driver can be downloaded from
here.
10
2. Define the connection URL
Every database is identified by a URL
Given a URL, DriverManager looks for the
driver that can talk to the corresponding
database
DriverManager tries all registered drivers,until
a suitable one is found
12
An Example
// A driver for imaginary1
Class.forName("ORG.img.imgSQL1.imaginary1Driver");
// A driver for imaginary2
Driver driver = new
ORG.img.imgSQL2.imaginary2Driver();
DriverManager.registerDriver(driver);
//A driver for PostgreSQL
Class.forName("org.postgresql.Driver");
14
3. Establish the connection
Connection con =
DriverManager.getConnection("jdbc:imaginaryDB1");
15
4. Create a Statement object
We use Statement objects in order to
Query the DB
17
5. Execute a query using the Statement
19
Manipulating DB with Statement
21
6. Process the result
We will discuss ResultSet in a while…
22
7. Close the connection
Close Connections, Statements, and
Result Sets
con.close();
stmt.close();
rs.close()
24
ResultSet
ResultSet objects provide access to the
tables generated as results of executing
Statement queries.
Only one ResultSet per Statement can be
open at a given time!
The table rows are retrieved in sequence:
A ResultSet maintains a cursor pointing to
26
ResultSet Methods
boolean next()
Activates the next row
void close()
Disposes of the ResultSet
28
Mapping Java Types to SQL Types
29
Null Values
In SQL, NULL means the field is empty
Not the same as 0 or “”!
30
Database Time
Times in SQL are notoriously non-standard
Java defines three classes to help
java.sql.Date
year, month, day
java.sql.Time
hours, minutes, seconds
java.sql.Timestamp
year, month, day, hours, minutes, seconds,
nanoseconds
Usually use this one
31
Exceptions
An SQLException is actually a list of
exceptions
32
Prepared Statements
The PreparedStatement object contains not
just an SQL statement, but an SQL statement
that has been precompiled.
This means that when the PreparedStatement is
executed, the DBMS can just run the
PreparedStatement SQL statement without having
to compile it first.
Most often used for SQL statements that take
parameters.
33
Creating a PreparedStatement Object
34
Supplying Values for
PreparedStatement Parameters
35
Example
the following line of code sets the first question mark
placeholder to a Java int with a value of 75:
updateSales.setInt(1, 75);
updateSales.setString(2, "Colombian");
36
Another Example
37
Callable Statements
Execute a call to a database stored
procedure.
38