Intro To JDBC
Intro To JDBC
Database Connectivity
(JDBC)
Connect
Query
Process
results
Close
Stage 1: Connect
Process
results
Close
A JDBC Driver
Is an interpreter that translates JDBC
method calls to vendor-specific database
commands
Database
JDBC calls commands
Driver
Database
Database
Protocol Subprotocol
identifier
jdbc:oracle:<driver>:@<database>
Example: jdbc:mysql://localhost:3306/
How to Make the Connection
1. Load JDBC Driver
Class.forName(driver);
Class.forName(driver);
// or
Class.forName("com.mysql.jdbc.Driver");
Connect
Close
The Statement Object
A Statement object sends your SQL
statement to the database.
You need an active connection to create
a JDBC statement.
Statement has three methods to execute
a SQL statement:
– executeQuery() for QUERY
statements
– executeUpdate() for INSERT,
UPDATE, DELETE, or DDL
statements
– execute() for either type of statement
How to Query the Database
Connect
Query
Step through the results
Close
The ResultSet Object
JDBC returns the results of a query in a
ResultSet object.
A ResultSet maintains a cursor pointing to
its current row of data.
Use next() to step through the result set
row by row.
getString(), getInt(), and so on assign
each value to a Java variable..
How to Process the Results
1. Step through the result set.
while (rset.next()) { … }
while (rset.next()) {
String title = rset.getString("TITLE");
String year = rset.getString("YEAR");
… // Process or display the data
}
How to Handle SQL Null Values
Java primitive types cannot have null
values.
Do not use a primitive type when your
query might return an SQL null.
Use ResultSet.wasNull() to determine
whether a column has a null value.
while (rset.next()) {
String year = rset.getString("YEAR");
if (rset.wasNull() {
… // Handle null value
}
…}
Mapping Database Types to Java Types
ResultSet maps database types to
Java types.
ResultSet rset = stmt.executeQuery
("select RENTAL_ID, RENTAL_DATE, STATUS
from ACME_RENTALS");
int id = rset.getInt(1);
Date rentaldate = rset.getDate(2);
String status = rset.getString(3);
Col Name Type
RENTAL_ID NUMBER
RENTAL_DATE DATE
STATUS VARCHAR2
Stage 4: Close
Connect
Query
Close the result set
Process
results Close the statement
• A PreparedStatement object
holds precompiled SQL statements.
• Use this object for statements you want
to execute more than once.
• A prepared statement can contain
variables that you supply each time
you execute the statement.
How to Create a Prepared Statement
1.Register the driver and create the database
connection.
2.Create the prepared statement, identifying
variables with a question mark (?).
PreparedStatement pstmt =
conn.prepareStatement("update ACME_RENTALS
set STATUS = ? where RENTAL_ID = ?");
PreparedStatement pstmt =
conn.prepareStatement("select STATUS from
ACME_RENTALS where RENTAL_ID = ?");
How to Create a Prepared Statement
1.Register the driver and create the
database connection.
2.Create the prepared statement,
identifying variables with a question
mark (?).
Example :
PreparedStatement pstmt = conn.prepareStatement
("select STATUS from ACME_RENTALS where RENTAL_ID = ?");
PreparedStatement ps = null;
ps = conn.prepareStatement("INSERT INTO tbl_product (key_product, key_category,
fld_product_name, fld_inventory_qty, fld_unit_price) “ + "values (?, ?, ?, ?, ?)");
How to Execute a Prepared Statement
1. Supply values for the variables.
pstmt.setXXX(index, value);
ps.executeUpdate();
A Simple JDBC Program…