Advanced Programming CH3
Advanced Programming CH3
Advanced Programming
How It Works
– Java code calls JDBC library
– JDBC loads a driver
– Driver talks to a particular database
– An application can work with several databases by using all
corresponding drivers
– Ideal: can change database engines without changing any
application code (not always in practice)
JDBC Model (cont)
– JDBC consists of two parts:
• The JDBC API
• JDBC Driver Manager, which
communicates with vendor-
specific drivers that interact
with the database
JDBC Model(cont)
Oracle
Driver
Oracle
DB2
Network
MySQL
Driver
MySQL
JDBC Driver Types
– JDBC drivers implement the defined interfaces
in the JDBC API for interacting with your
database server.
– For example, using JDBC drivers enable you to
open database connections and to interact with it
by sending SQL or database commands then
receiving results with Java.
– The Java.sql package contains various classes
with their behaviors defined and their actual
implementaions are done in third-party drivers.
Third party vendors implements the
java.sql.Driver interface in their database driver.
JDBC Driver Types(cont)
Type Description
java.sql.package
java.sql.DriverManager
(JDBC library)
4. Connection
supplied by
Driver for MySQL Driver for Oracle DriverManager is
databases databases used by
application to talk
How JDBC establishes a JDBC through
connection between your the driver to the
MySQL
code and a database database.
DB
JDBC Programming Steps
– The first argument indicate the type of a ResultSet object and the
second argument is one of two ResultSet constants for specifying
whether a result set is read-only or updatable.
JDBC - Result Sets
Type of ResultSet:
• The possible RSType are given below, If you do not specify any ResultSet type,
you will automatically get one that is TYPE_FORWARD_ONLY.
Type Description
ResultSet.TYPE_FORWARD_ONLY The cursor can only move forward in the result set.
Concurrency Description
• getMaxRows/setMaxRows
– Maximum number of rows a ResultSet may contain
– Unless explicitly set, the number of rows is unlimited
JDBC - Result Sets
Updating a Result Set:
• The ResultSet interface contains a collection of update methods for updating
the data of a result set.
• As with the get methods, there are two update methods for each data type:
1. One that takes in a column name.
2. One that takes in a column index.
• For example, to update a String column of the current row of a result set, you
would use one of the following updateString() methods: