08 JDBC
08 JDBC
JDBC Overview
• What is JDBC?
• Features
• Architecture
• Development process
• JDBC data access statements
• Java JDBC Transactions
What is JDBC?
• JDBC
– Allows a Java application to connect to a relational
database.
– The major databases are supported such as
Oracle, Microsoft SQL Server, DB2 and many
others.
Features
• The main feature of JDBC is that it is a standard API.
You develop your application code to the JDBC API
and you can connected to various databases.
• JDBC supported a large number of databases
– Oracle, Microsoft SQL server, MySQL, SyBase, DB2,
PostgreSQL …
• You can build your own custom SQL statement:
– select, insert, update and delete.
– Complex SQL queries: inner and outer joins.
– Call stored procedures.
JDBC architecture
JDBC architecture
• JDBC driver
– Provides a connection to a database.
– Converts JDBC calls to for specific database.
• JDBC driver implementations
– Provided by database vendor.
JDBC Driver manager
• Driver manager helps connect an application
based on a database connection string.
• In JDBC is version 4.0, the JDBC drivers are
automatically loaded based on the classpath.
• Legacy JDBC 3.0 drivers have to be explicitly
loaded with Java code is
Class.forName(theDriverName)
JDBC API
• The JDBC API is defined in two packages.
– java.sql and javax.sql.
• Key classes
– java.sql.DriverManager
– java.sql.Connection
– java.sql.statement
– java.sql.ResultSet
– java.sql.DataSource
Development Process
• Get a Connection to database.
• Create a Statement object.
• Execute SQL query.
• Process Results set.
• Close connection
Step 1: Get a Connection to database
• In order to connect to database
– Need to connection string in form of JDBC URL.
• Basic syntax
– jdbc:<driver protocol>:<driver connection details>
• Examples
Database JBDC URL
MS SQL Server jdbc:sqlserver://<HOST>:<PORT>;DatabaseName=DB
Oracle jdbc:oracle:thin:@<HOST>:<PORT>:<DB>
MySQL jdbc:mysql://<HOST>:<PORT>/<DB>
Step 1: Get a Connection to database
Step 2: Create a Statement object
• The Statement object is based on connection.
– It will be used later to execute SQL query.
Step 3: Execute SQL query.
• Pass in your SQL query
Step 4: Process Results set
• Results set is initially placed before first now.
• Method: boolean next()
– Moves forward one row
– Return true if there are more rows to process
• Looping through a result set
Step 4: Process Results set
• Collection of methods for reading data
– getXXX(columnName)
– getXXX(columnIndex)
Step 5: Close connection
• By closing connection object statement and
ResultSet will be closed automatically.
• The close() method of Connection interface is
used to close the connection.
public void close()throws SQLException
• Example
con.close();
JDBC data access statements
// Statement creation
Statement statement = connection.createStatement();
// Query string
String query = <SQLQuery>; // CRUD
// for retrieve data
ResultSet result = statement.executeQuery(query);
while (result.next()){
result.getString(<ColName>);
result.getInt(<ColName>);
result.getFloat(<ColName>);
}
// for data modification: insert, update, delete
int nbUpdated = statement.executeUpdate(query);
JDBC ResultSet update preparation
// for use with ResultSet only
// No “previous” method using, no update
connection.createStatement(
ResultSet.TYPE_FORWARD_ONLY,
ResultSet.CONCUR_READ_ONLY);
• Java coding
Java JDBC Transactions
• What are Transactions?
• How to develop transactions with JDBC
What are Transactions?
• A transaction is basically a unit of work.
• One or more SQL statements executed
together.
– Either all of the statements are executed – Commit
– Or none of the statements are executed – Rollback
JDBC Transactions
• By default, the database connection is to
auto-commit
– Need to explicitly turn off auto-commit