JDBC - Java Database Connectivity
JDBC - Java Database Connectivity
Connectivity
Talking to Databases
Zulqarnain Hashmi
1
Introduction to JDBC
2
JDBC Architecture
We will
Access
These are
use this one… Driver
Java classes
MS Access
SQL Server
Java DB2
Application JDBC
Driver
DB2
Network
Oracle
Driver
Oracle
3
JDBC Architecture (cont.)
Java
application
JDBC-API
JDBC-
Driver manager
JDBC-ODBC Native
bridge API-driver
Client library
5
What is ODBC?
6
Running a JDBC Application
7
Seven Steps
Class.forName(“oracle.jdbc.driver.OracleDriver");
9
Connecting to the Database
10
Connecting to the Database
Connection con = DriverManager.
getConnection("jdbc.odbc:Books“,”userName”,”password” );
“jdbc.odbc:Books” URL
11
Interaction with the Database
13
Changing DB with Statement
String deleteStr =
“DELETE FROM Member " +
"WHERE Name = ‘harry potter’";
14
ResultSet
• A ResultSet provides access to a table of data
generated by executing a Statement
• Only one ResultSet per Statement can be open at
once
• The table rows are retrieved in sequence
– A ResultSet maintains a cursor pointing to its current
row of data
– The 'next' method moves the cursor to the next row
ResultSet Methods
• boolean next()
– activates the next row
– the first call to next() activates the first row
– returns false if there are no more rows
• void close()
– disposes of the ResultSet
– allows you to re-use the Statement that created it
– automatically called by most Statement methods
ResultSet Methods
Statement stmt = con.createStatement();
ResultSet rs = stmt.
executeQuery("select name, age from Employees");
// Print the result
while(rs.next()) {
System.out.print(rs.getString(1) + ”:“);
System.out.println(rs.getShort(“age”)+”“);
}
20
Null Values
22
Mapping Java Types to SQL
Types
SQL type Java Type
CHAR, VARCHAR, LONGVARCHAR String
NUMERIC, DECIMAL java.math.BigDecimal
BIT boolean
TINY INT byte
SMALL INT short
INTEGER int
BIG INT long
REAL float
FLOAT, DOUBLE double
BINARY, VARBINARY, LONGVARBINARY byte[]
DATE java.sql.Date
TIME java.sql.Time
TIMESTAMP java.sql.Timestamp
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
Cleaning Up After Yourself
rs.close();
stmt.close();
con.close();
25
Dealing With Exceptions
try {
Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver“);
connection = DriverManager.getConnection(
url, username, password );
}
catch (ClassNotFoundException cnfe) {
System.out.println(cnfe.getMessage());
}
26
Dealing With Exceptions
An exception can have more exceptions in it.
•
catch (SQLException e) {
while (e != null) {
System.out.println(e.getSQLState());
System.out.println(e.getMessage());
System.out.println(e.getErrorCode());
e = e.getNextException();
}
}
27
A Summary of JDBC application
Result handling
while (resultSet.next()) {
...
no
Last
execution ? }
resultSet.close();
yes
closeStatment stmt.close();
con.close();
closeConnection
28