Documente Academic
Documente Profesional
Documente Cultură
IBM
Objectives
• Overview of JDBC
• JDBC API
• Transactions
2 IBM
Database Architectures
• Two-tier
• Three-tier
• N-tier
3 IBM
Two-Tier Architecture
4 IBM
Two-Tier Pros
• Simple
• Client-side scripting offloads
work onto the client
5 IBM
Two-Tier Cons User Interface
• Fat client
Client Layer UI Logic
• Server bottlenecks
• Software Distribution problem Business Logic
• Inflexible
Network
User Data
DML Operations
Validation
Checks
Database
Server Layer
Tables
6 IBM
User Interface
Three-Tier Architecture
Client Layer
UI Logic
Network
Business Messages
Network
User Data
DML Operations
Validation
Checks
7 IBM
Three-Tier Pros
8 IBM
Three-Tier Cons
• higher complexity
• higher maintenance
• lower network efficiency
• more parts to configure (and buy)
9 IBM
N-Tier Architecture
10 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:
Submit Clear
11
JDBC Architecture
It can be categorized into into two layers:
Driver Oracle
Java JDBC
Driver DB2
Application API
12
JDBC Architecture (Continued)
13 IBM
JDBC Drivers
14 IBM
Type I Drivers
Go Back To
JDBC Driver List
15 IBM
Disadvantage of Type-I Driver
Go Back To
JDBC Driver List
16 IBM
Type II Drivers
Go Back To
JDBC Driver List
17 IBM
Disadvantage of Type-II Driver
Go Back To
JDBC Driver List
18 IBM
Type III Drivers
• Follows a three tier
communication approach.
Go Back To
JDBC Driver List
19 IBM
Disadvantage of Type-III Driver
Go Back To
JDBC Driver List
20 IBM
Type IV Drivers
Go Back To
JDBC Driver List
21 IBM
Disadvantage of Type-IV Driver
Go Back To
JDBC Driver List
22 IBM
JDBC Drivers (Fig.)
Type I ODBC
JDBC
“Bridge” Driver
Type II
“Native” CLI (.lib)
JDBC
Type III Middleware
“Middleware” Server
Type IV
“Pure”
23 IBM
Related Technologies
• ODBC
Requires configuration (odbc.ini)
• RDO, ADO
Requires Win32
• JavaBlend
maps objects to tables transparently (more or less)
24 IBM
JDBC API
25 IBM
JDBC API
• The JDBC API classes and interfaces are available in the
java.sql and the javax.sql packages.
26
JDBC API (Continued)
27
Steps to create JDBC Application
DriverManager
Driver
Connection
Statement
ResultSet
28 IBM
Steps to create JDBC Application (Continued)
Load A Driver
Connect to a Database
29
JDBC API (Continued)
Load A Driver
• Programmatically:
Using the forName() method
Using the registerDriver()method
• Manually:
By setting system property
30
JDBC API (Continued)
31
JDBC API (Continued)
32
JDBC API (Continued)
33
JDBC API (Continued)
Connect to a Database
Example:
Connection con=DriverManager.getConnection(“jdbc:odbc:MyDSN”,”scott”,”tiger”);
34
JDBC API (Continued)
Statement createStatement()
returns a new Statement object
36 IBM
JDBC API (Continued)
Statement Interface
37 IBM
JDBC API (Continued)
38 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
39 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
40 IBM
JDBC API (Continued)
41 IBM
JDBC API (Continued)
42 IBM
JDBC API (Continued)
43 IBM
JDBC API (Continued)
44 IBM
JDBC API (Continued)
11. boolean absolute(int rowno) 11. Shifts the cursor to the row
number that you specify as an
argument.
45 IBM
JDBC API (Continued)
46 IBM
JDBC API (Continued)
47 IBM
JDBC API (Continued)
ResultSet Fields
48 IBM
JDBC API (Continued)
ResultSet Fields
49 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
50 IBM
JDBC API (Continued)
Database Operations
Querying a table
Inserting rows
Updating rows
Deleting rows
51
JDBC API (Continued)
Database Operations
Querying a table
52
JDBC API (Continued)
Database Operations
Inserting rows
53
JDBC API (Continued)
Database Operations
Updating rows
54
JDBC API (Continued)
Database Operations
Deleting rows
55
JDBC API (Continued)
DDL Operations
Creating Table
Altering Table
Dropping Table
56
JDBC API (Continued)
DDL Operations
Creating Table
57
JDBC API (Continued)
DDL Operations
Altering Table
58
JDBC API (Continued)
DDL Operations
Dropping Table
59
JDBC API (Continued)
PreparedStatement Interface
60
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();
61
JDBC API (Continued)
62 IBM
Transactions
Transactions Overview
63 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.
64 IBM
JDBC Class Diagram
65 IBM
Summary
66
Summary (Continued)
67
Summary (Continued)
• You can add the driver name to the jdbc.drivers system property to
load and register a JDBC driver manually.
• A Connection object establishes a connection between a Java
application and a database.
• A Statement object sends requests to and retrieves results from a
database.
• You can insert, update, and delete data from a table using the DML
statements in Java applications.
68
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.
69
Test Your Understanding
70 IBM
Test Your Understanding (Contd.)
71 IBM