Unit 1 - JDBC
Unit 1 - JDBC
• JDBC: Introduction
• JDBC Architecture
• Types of Drivers
• Statement
• ResultSet, Read Only ResultSet, Updatable
ResultSet, Forward Only ResultSet, Scrollable
ResultSet
• PreparedStatement, Connection Modes,
SavePoint, Batch Updations
• CallableStatement, BLOB & CLOB
IBM
Database Architectures
• Two-tier
• Three-tier
• N-tier
2 IBM
Two-Tier Architecture
3 IBM
Two-Tier Pros
• Simple
• Client-side scripting offloads
work onto the client
4 IBM
Two-Tier Cons User Interface
User Data
DML Operations
Network
Validation
Checks
Database
Server Layer
Tables
5 IBM
User Interface
Three-Tier Architecture
Client Layer
UI Logic
• Application Server sits
between client and database
Business Messages
Network
Application Server Business Logic
Layer
User Data
DML Operations
Network
Validation
Checks
6 IBM
Three-Tier Pros
7 IBM
Three-Tier Cons
• higher complexity
• higher maintenance
• lower network efficiency
• more parts to configure (and buy)
8 IBM
N-Tier Architecture
9 IBM
Java Application Connects to
Database
• The below given figure shows the Employee Logging System
application developed in Java interacting with the Employee
database using the JDBC API:
10
JDBC Architecture
It can be categorized into into two layers:
Driver Oracle
Java JDBC
Driver DB2
Application API
11
JDBC Architecture (Continued)
12 IBM
JDBC Drivers
13 IBM
Type I Drivers
Go Back To
JDBC Driver List
14 IBM
Disadvantage of Type-I Driver
Go Back To
JDBC Driver List
15 IBM
Type II Drivers
Go Back To
JDBC Driver List
16 IBM
Disadvantage of Type-II Driver
Go Back To
JDBC Driver List
17 IBM
Type III Drivers
• Follows a three tier communication
approach.
Go Back To
JDBC Driver List
18 IBM
Disadvantage of Type-III Driver
Go Back To
JDBC Driver List
19 IBM
Type IV Drivers
Go Back To
JDBC Driver List
20 IBM
Disadvantage of Type-IV Driver
Go Back To
JDBC Driver List
21 IBM
JDBC Drivers (Fig.)
Type I ODBC
JDBC
“Bridge” Driver
Type II
“Native” CLI (.lib)
JDBC
Type III Middleware
“Middleware” Server
Type IV
“Pure”
22 IBM
Related Technologies
• ODBC
Requires configuration (odbc.ini)
• RDO, ADO
Requires Win32
• JavaBlend
maps objects to tables transparently (more or less)
23 IBM
JDBC API
24 IBM
JDBC API
• The JDBC API classes and interfaces are available in the
java.sql and the javax.sql packages.
25
JDBC API (Continued)
26
Steps to create JDBC Application
DriverManager
Driver
Connection
Statement
ResultSet
27 IBM
Steps to create JDBC Application (Continued)
Load A Driver
Connect to a Database
28
JDBC API (Continued)
Load A Driver
• Programmatically:
Using the forName() method
Using the registerDriver()method
• Manually:
By setting system property
29
JDBC API (Continued)
30
JDBC API (Continued)
31
JDBC API (Continued)
32
JDBC API (Continued)
Connect to a Database
Example:
Connection con=DriverManager.getConnection(“jdbc:odbc:MyDSN”,”scott”,”tiger”);
33
JDBC API (Continued)
34 IBM
JDBC API (Continued)
Statement createStatement()
returns a new Statement object
35 IBM
JDBC API (Continued)
Statement Interface
36 IBM
JDBC API (Continued)
37 IBM
JDBC API (Continued)
ResultSet Interface
• 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.
you can’t rewind
38 IBM
JDBC API (Continued)
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
39 IBM
JDBC API (Continued)
40 IBM
JDBC API (Continued)
41 IBM
JDBC API (Continued)
42 IBM
JDBC API (Continued)
43 IBM
JDBC API (Continued)
11. boolean absolute(int rowno) 11. Shifts the cursor to the row
number that you specify as an
argument.
12. boolean relative(int rowno)
12. Shifts the cursor relative to the
row number that you specify as an
argument.
44 IBM
JDBC API (Continued)
45 IBM
JDBC API (Continued)
46 IBM
JDBC API (Continued)
ResultSet Fields
47 IBM
JDBC API (Continued)
ResultSet Fields
48 IBM
JDBC API (Continued)
SQL Syntax
INSERT INTO table ( field1, field2 ) VALUES ( value1, value2
)
inserts a new record into the named table
UPDATE table SET ( field1 = value1, field2 = value2 )
WHERE condition
changes an existing record or records
DELETE FROM table WHERE condition
removes all records that match condition
SELECT field1, field2 FROM table WHERE condition
retrieves all records that match condition
49 IBM
JDBC API (Continued)
Database Operations
Querying a table
Inserting rows
Updating rows
Deleting rows
50
JDBC API (Continued)
Database Operations
Querying a table
51
JDBC API (Continued)
Database Operations
Inserting rows
52
JDBC API (Continued)
Database Operations
Updating rows
53
JDBC API (Continued)
Database Operations
Deleting rows
54
JDBC API (Continued)
DDL Operations
Creating Table
Altering Table
Dropping Table
55
JDBC API (Continued)
DDL Operations
Creating Table
56
JDBC API (Continued)
DDL Operations
Altering Table
57
JDBC API (Continued)
DDL Operations
Dropping Table
58
JDBC API (Continued)
PreparedStatement Interface
59
JDBC API (Continued)
The value The code snippet to pass the employee id during It acts as a
of each ‘?’ runtime using prepareStatement() method: placeholder
is set by
calling
String s=“select * from employee where eid=? “
appropriate
setXXX() PreparedStatement pst = con.prepareStatement(s);
method, In pst.setInt(1,100 );
this case
setInt()
ResultSet rs=pst.executeQuery();
60
JDBC API (Continued)
61 IBM
Transactions
Transactions Overview
62 IBM
Transactions (Continued)
Transaction Management
• Transactions are not explicitly opened and closed
• if AutoCommit is true, then every statement is automatically
committed
• default case: true
• if AutoCommit is false, then every statement is added to an
ongoing transaction
• Must explicitly rollback or commit.
63 IBM
JDBC Class Diagram
64 IBM
Learning & Knowledge Education - India
con.commit();
con.close();
66
}} IBM © 2007 IBM Corporation
Summary
67
Summary (Continued)
68
Summary (Continued)
69
Summary (Continued)
• You can create, alter, and drop tables from a database using the
DDL statements in Java applications.
• A ResultSet object stores the result retrieved from a database when
a SELECT statement is executed.
• You can create various types of ResultSet objects such as read only,
updatable, and forward only.
70
Test Your Understanding
71 IBM
Test Your Understanding (Contd.)
72 IBM