0% found this document useful (0 votes)
20 views68 pages

CS378 Slide 07 JDBC

Uploaded by

iloik2013
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views68 pages

CS378 Slide 07 JDBC

Uploaded by

iloik2013
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 68

YANBU UNIVERSITY COLLEGE

CS378 – SLIDE – 10

MODULE 10: JDBC


1
OUTLINE
• Overview of databases
• JDBC Overview
• JDBC Design Strategies
• Steps in Using JDBC
• JDBC in Desktop Java Applications
• JDBC in Web Applications
• Class Work
• Practical Examples
2
DATABASE
OVERVIEW

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

• Structured collection of data.


• Tables
• Fields
• Query
• Reports
• Essentially a much more sophisticated
implementation of the flat files.
Relational Database

• Stores data in separate tables instead of


a single store.
• Relationships between tables are set
• In theory, this provides a faster, more
flexible database system.
Relational Database Example
Relational Database Example
Relational Database Example
Database Management System
• Manages the storage and retrieval of
data to and from the database and hides
the complexity of what is actually going
on from the user.

Database
Database Management User
Ssytem

• MySQL is a relational database


management system
Client: makes a request
requests an Internet
Client resource by
(browser) specifying a URL and
providing input via HTTP
Web encoded strings
browser

GET hello.php HTTP/1.1 Server


Host: www.massey.ac.nz:80
os
Web
server

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

MySQL server could be


Web
anywhere in the world
server My codes
PHP
HTTP HTML interpreter
Client
Operating System
Web
TCP/IP
browser

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)

Application API Database Database


Protocol Specific Protocol

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

We can use Class.forName to load the JDBC driver as indicated below:


Class.forName("com.mysql.jdbc.Driver");

However, this call could throw a ClassNotFoundException and therefore it


should be inside a try/catch block. Write the above jave code in a try/catch block
Step 2: Define the Connection URL
Step 3: Establish the Connection

• To make the actual network connection


you need to use the URL, database
username, and database password as
shown below:
Step 4: Create a Statement
Object

• A Statement object is used to send


queries and commands to the database.
Step 5: Execute a Query or
Update
Once you have a Statement object, you can use it to send SQL
queries by using the executeQuery method, which returns an
object of type ResultSet.
Step 6: Process the Results

• 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?

Due to the fact that the overhead of opening a connection is usually


large.
Using JDBC from
Desktop Java

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

Mention any two types of JDBC driver


There are 4 types of JDBC drivers:

JDBC-ODBC bridge driver

Native-API driver (partially java driver)

Network Protocol driver (fully java driver)

Thin driver (fully java driver)


Classwork
What is a JDBC driver?
A JDBC driver is a software component enabling a Java
application to interact with a database

Mention any two types of JDBC driver


There are 4 types of JDBC drivers:

JDBC-ODBC bridge driver

Native-API driver (partially java driver)

Network Protocol driver (fully java driver)

Thin driver (fully java driver)


Classwork

In which package can JDBC classes be found?


JDBC classes are in the java.sql package
Classwork

• One of the steps required to use JDBC is to define the connection


URL. Write code to achieve this assuming the following [1.5 Mark]:
• host: 127.0.0.1
• dbName: CS378_db
• port: 3333
• dbms = MySQL
String mySqlUrl = "jdbc:mysql//" + 127.0.0.1 + ":" + 3333 + "/" +
CS378_db;
DELETE FROM Customers WHERE Custom
erName='Alfreds Futterkiste';

• Which of the following methods should be


used to execute the above statement?
Statement.executeQuery(…);

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;

• Which of the following methods should be


used to execute the above statement?
Statement.executeQuery(…);

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;

• Which of the following methods should be


used to execute the above statement?

Statement.executeQuery(…);

Statement.executeUpdate(…);

Statement.execute(…);
ResultSet.next()

• The above statement will go to the next row


and it will return true if no next row.
True

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

You might also like