Chapter 6
Chapter 6
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
• 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
• A single JDBC driver does not enable you to access different “brands” of
databases.
10
JDBC Model(cont)
Oracle
Driver
Oracle
MySQL
Driver
MySQL
11
JDBC Driver Types
Type Description
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
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.
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.