[go: up one dir, main page]

0% found this document useful (0 votes)
30 views23 pages

Chapter 6

Uploaded by

Firomsa Dine
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)
30 views23 pages

Chapter 6

Uploaded by

Firomsa Dine
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/ 23

Mizan Tepi University

School of Computing and


Informatics
Department of Software Engineering
Chapter 6:
Java Database Programming
Contents
Overview of JDBC
JDBC Drivers
Statements
Connecting to a Database

2
Contents
Overview of JDBC
JDBC Drivers
Statements
Connecting to a Database

3
4
5
6
7
8
9
Overview of JDBC
• JDBC (Java Data Base Connectivity) is a complete set of interfaces and

classes that allows access to an underlying database through java application.


JDBC = JDBC API + JDBC Driver

• The API defines the interfaces and methods vendors implement when writing

a JDBC driver.

• JDBC drivers implement the defined interfaces in the JDBC API for

interacting with DBMS of database.

• A single JDBC driver does not enable you to access different “brands” of

databases.

10
JDBC Model(cont)

Oracle
Driver

Oracle

Java JDBC DB2


Application Driver
API
DB2

MySQL
Driver

MySQL
11
JDBC Driver Types
Type Description

Bridge Driver (JDBC-to-ODBC) – connects Java to a


1
Microsoft ODBC (Open Database Connectivity) data source.
Native-API Driver –is a database driver implementation that
uses the client-side libraries of the database. The driver
2
converts JDBC method calls into native calls of the database
API.
Network Protocol Driver (Middleware Driver) –The
3 requests are sent to an application server, which translates the
database requests into a database-specific protocol.
Database Protocol Driver (Pure Java Driver) – convert
JDBC requests to database-specific network protocols at the
4
client side, so that Java programs can connect directly to a
database.
Type 1:
Advantages
• Almost any database for which an
ODBC driver is installed can be
accessed, and data can be retrieved.
Disadvantages
• Performance overhead
• The ODBC driver needs to be
installed on the client machine.
• Not suitable for applets, because
the ODBC driver needs to be
installed on the client.
Type 2:
Advantages
• As there is no implementation of
jdbc-odbc bridge, its considerably
faster than a type 1 driver.
Disadvantages
• The vendor client library needs to be
installed on the client machine.
• Not all databases have a client side
library.
• This driver is platform dependent
• This driver supports all java
applications except Applets
Type 3:
Advantages
Since the communication between
client and the middleware server is
database independent, there is no need
for the database vendor library on the
client. The client need not be changed
for a new database.

Disadvantages
• Requires database-specific coding to
be done in the middle tier.
•The middleware layer added may
result in additional latency, but is
typically overcome by using better
middleware services.
Type 4:
Advantages
• No translation or middleware layers
are used, improving performance.
• The JVM can manage all aspects of
the application-to-database
connection; this can
facilitate debugging.
Disadvantages
Drivers are database dependent, as
different database vendors use widely
different (and usually proprietary)
network protocols.
Some Popular JDBC Drivers
RDBMS JDBC Driver Name
Driver Name : com.mysql.jdbc.Driver
MySQL Database URL format:
jdbc:mysql//hostname/databaseName
Driver Name : oracle.jdbc.driver.OracleDriver
Oracle Database URL format:
jdbc:oracle:thin@hostname:portnumber:databaseName
Driver Name : com.ibm.db2.jdbc.net.DB2Driver
DB2 Database URL format:
jdbc:db2:hostname:portnumber/databaseName
Driver Name : com.jdbc.odbc.JdbcOdbcDriver
Access Database URL format:
jdbc:odbc:databaseName
Driver Name : com.org.derby.jdbc.ClintDriver
Java DB Database URL format:
jdbc:derby:databaseName
Overview of JDBC (contd)
The JDBC Interfaces and Classes are in the java.sql package
What does JDBC do?
 Establish a connection with a database
 Send SQL statements
 Process the results

Steps in using JDBC (refer to word file )


1. Load the JDBC driver
2. Define the Connection URL
3. Establish the Connection
4. Create a Statement object
5. Execute a query
6. Process the results
7. Close the connection

18
Primary JDBC class and interfaces
Java class and interfaces used for JDBC
 DriverManager: used to load the class which is the driver for a specific
database.
 Connection: the database connection interface.
 Statement: An SQL statement interface to be executed on the connection.
 ResultSet: The set of results interface that are returned by a query.

 Once a connection is obtained we can interact with the database.


 The JDBC Statement, PreparedStatement and CallableStatement,
interfaces define the methods and properties that enable you to
send SQL commands and receive data from your database.
 They also define methods that help bridge data type differences
19
between Java and SQL data types used in a database.
JDBC - Statements
Interfaces Recommended Use

Use for general-purpose access to your database.


Useful when you are using static SQL statements
Statement
at runtime. The Statement interface cannot accept
parameters.

Use when you plan to use the SQL statements


PreparedStatement many times. The Prepared Statement interface
accepts input parameters at runtime.

Use when you want to access database stored


CallableStatement procedures. The Callable Statement interface can
also accept runtime input parameters.
20
The Statement
Creating Statement Object:
Objects
 you need to create a statement using the Connection object's createStatement( ) method, as in
the following example:
Closing Statement Object:
 Just as you close a Connection object to save database resources, for the same reason you
should also close the Statement object.
 A simple call to the close() method will do the job.

Example:
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
try{
stmt = con.createStatement();
stmt.execute("INSERT INTO StudentInfo VALUES (1,‘Alem')");
}
catch (SQLException e) { }
finally {
stmt.close();
21
}
The Statement Objects:
Once you've created a Statement object, you
can then use it to execute a SQL statement
with one of its three execute methods.
1. boolean execute(String SQL) : Returns a boolean value of true
if a ResultSet object can be retrieved; otherwise, it returns false.
2. int executeUpdate(String SQL) : Returns the numbers of rows
affected by the execution of the SQL statement. Use this
method to execute SQL statements for which you expect to get
a number of rows affected - for example, an INSERT,
UPDATE, or DELETE statement.
3. ResultSet executeQuery(String SQL) : Returns a ResultSet
object. Use this method when you expect to get a result set, as
22 you would with a SELECT statement.

JDBC - Result Sets
The SQL statements that read data from a database query return the
data in a result set.
 The rows that satisfy a particular query are called the result set.
 A user can access the data in a result set using a cursor one row at a
time from top to bottom
 The java.sql.ResultSet interface represents the result set of a database
query.
 A ResultSet object maintains a cursor that points to the current row in
the result set.
 ResultSet interface methods can be broken down into three
categories:
 Navigational methods: used to move the cursor around.

 Get methods: used to view the data in the columns of the current row being
pointed to by the cursor.
23  Update methods: used to update the data in the columns of the current row.
The updates can then be updated in the underlying database as well.

You might also like