CS378 Slide 07 JDBC
CS378 Slide 07 JDBC
CS378 – SLIDE – 10
3
Dynamic Web Sites
Truly dynamic web sites
Content changes over time
Content customised for individual user
Content automatically generated
Content Programmatically generated
Can be File system based
HTML and Images stored on File System
Gets hard to manage over time
Database based
HTML, Images etc all generated from database
Easier to manage
If data is too large, can overload the database
Relational Database
Database
Database Management User
Ssytem
os
Internet
Network Core
Server: responds
• Webserver supports
HTTP.
Server
Web
server My codes
MySQL PHP
HTTP HTML interpreter
Client
Operating System
Web
TCP/IP
browser
Internet
Server: responds
Internet
MySQL
Operating System Server
Internet
JDBC OVERVIEW
14
JDBC Overview
• JDBC: Java Database Connectivity
– It provides a standard library for Java programs to connect to
a database and send it commands using SQL
– It generalizes common database access functions into a set of
common classes and methods
– Abstracts vendor specific details into a code library making
the connectivity to multiple databases transparent to user
• JDBC API Standardizes:
– Way to establish connection to database
– Approach to initiating queries
– Method to create stored procedures
– Data structure of the query result
15
JDBC Overview
• Two main packages java.sql and javax.sql
– Java.sql contains all core classes required for accessing database
(Part of Java 2 SDK, Standard Edition)
– Javax.sql contains optional features in the JDBC 2.0 API
(part of Java 2 SDK, Enterprise Edition)
• Javax.sql adds functionality for enterprise applications
– DataSources
– JNDI
– Connection Pooling
– Rowsets
– Distributed Transactions
16
JDBC Architecture
• JDBC Consists of two parts:
– JDBC API, a purely Java-
based API
– JDBC Driver Manager,
which communicates with
vendor-specific drivers that
perform the real
communication with the
database
• Translation to the vendor
format occurs on the client
– No changes needed to the
server
– Driver (translator) needed
on client 17
JDBC Drivers
• JDBC uses drivers to translate generalized
JDBC calls into vendor-specific database calls
– Drivers exist for most popular databases
– Four Classes of JDBC drivers exist
Type I
Type II
Type III
Type IV
18
JDBC Drivers (Type I)
• Type I driver provides mapping between JDBC and access API of a
database
– The access API calls the native API of the database to establish communication
• A common Type I driver defines a JDBC to ODBC bridge
– ODBC is the database connectivity for databases
– JDBC driver translates JDBC calls to corresponding ODBC calls
– Thus if ODBC driver exists for a database this bridge can be used to
communicate with the database from a Java application
• Inefficient and narrow solution
– Inefficient, because it goes through multiple layers
– Narrow, since functionality of JDBC code limited to whatever ODBC supports
Driver (Type
ODBC API
Native API
Client
I)
07/01/2024 19
JDBC - ODBC
Open Database Connectivity
• The goal of ODBC is to make it
possible to access any data from any ODBC Interface
application, regardless of which
database management system
(DBMS) is handling the data.
• ODBC manages this by inserting a
middle layer
• The purpose of this layer is to
translate the application's data queries
into commands that the DBMS
understands.
• For this to work, both the application
and the DBMS must be ODBC-
compliant, that is, the application
must be capable of issuing ODBC
commands and the DBMS must be
capable of responding to them. 20
JDBC Drivers (Type II)
• Type II driver communicates directly with native API
– Type II makes calls directly to the native API calls
– More efficient since there is one less layer to contend with
(i.e. no ODBC)
– It is dependent on the existence of a native API for a
database
Driver (Type II)
Native API
Client
Application Database
API Database
Protocol Specific Protocol
07/01/2024 21
JDBC Drivers (Type III)
• Type III driver make calls to a middleware component running on
another server
– This communication uses a database independent net protocol
– Middleware server then makes calls to the database using database-
specific protocol
– The program sends JDBC call through the JDBC driver to the middle
tier
– Middle-tier may use Type I or II JDBC driver to communicate with
the database.
Driver (Type
III)
Client Middleware
Application Net Protocol Server Database Database
Specific Protocol
07/01/2024 22
JDBC Drivers (Type IV)
• Type IV driver is an all-Java driver that is also called a thin
driver
– It issues requests directly to the database using its native protocol
– It can be used directly on platform with a JVM
– Most efficient since requests only go through one layer
– Simplest to deploy since no additional libraries or middle-ware
Driver (Type
IV)
Client
Application Database Specific Database
Protocol
07/01/2024 23
JDBC Data Types
24
JDBC DESIGN
STRATEGIES
25
JDBC Design Strategies
26
BASIC STEPS IN
USING JDBC
27
Basic Steps in using JDBC
Step 1: Load the Driver
• The simplest way to handle the results is to use the next method of
ResultSet to move through the table a row at a time.
• Note that the first column in a ResultSet row has index 1, not 0.
Step 6: Process the Results
Step 6: Process the Results
Step 7: Close the Connection
• To close the connection explicitly, you can use the following code:
connection.close();
• Note that closing the connection also closes the corresponding Statement and ResultSet
objects.
• It is a good idea to close the connection but it is should be postponed if you expect to perform
additional database operations. Why do you think we should postpone closing the connection in
this situation?
38
Approach
Using JDBC from
Web Apps
40
Approach
Sample Database
A Servlet to Show Employee
Info
Employee Info Servlet
Employee Info Servlet Cont.
Employee Info Servlet Cont.
Employee Info Servlet Cont.
Employee Info Servlet Cont.
Employee Info Servlet Cont.
Employee Info Servlet
(Result)
JDBC CLASS
WORK
51
Classwork
What is a JDBC driver?
A JDBC driver is a software component enabling a Java
application to interact with a database
Statement.executeUpdate(…);
Statement.execute(…);
Select * from student;
• Which of the following methods should be
used to execute the above statement?
Statement.executeQuery(…);
Statement.executeUpdate(…);
Statement.execute(…);
UPDATE Customers
SET ContactName = 'Alfred Schmidt', City= 'Frankfurt'
WHERE CustomerID = 1;
Statement.executeUpdate(…);
Statement.execute(…);
DROP TABLE Teacher;
• Which of the following methods should be
used to execute the above statement?
Statement.executeQuery(…);
Statement.executeUpdate(…);
Statement.execute(…);
UPDATE Customers
SET ContactName = 'Alfred Schmidt', City= 'Frankfurt'
WHERE CustomerID = 1;
Statement.executeQuery(…);
Statement.executeUpdate(…);
Statement.execute(…);
ResultSet.next()
False
MORE
QUESTIONS
Considering the following
form and code
Assume that the database name is CS378_Quiz_3 and using Figure 1 and the associated code,
add the missing code in the following jsp file so that records can be inserted into the database.
PRACTICAL
EXAMPLE
66
THANK
YOU
67
? 68