CS124 L9 JDBC
CS124 L9 JDBC
CS 124
JDBC
JavaDatabase Connectivity
Database Access Interface
provides access to a relational database (by
allowing SQL statements to be sent and executed
through a Java program)
JDBC package: set of Java classes that facilitate
this access (java.sql.*)
Comes with JDK (since 1.1)
JDBC Driver
Need a driver, specific to the DB product, to
mediate between JDBC and the database
the driver is a Java class that needs to be loaded
first
Java Program
- load driver Relational
- establish connection DBMS
- send SQL statements
JDBC-ODBC Bridge
Driver that interfaces with ODBC (Object
Database Connectivity--also an access
interface)
Easiest way to access databases created by
Microsoft products
register database as an ODBC data source
use JDBC-ODBC bridge as the JDBC driver
(included in JDK distribution)
Key Classes in JDBC
Connection
need to create an instance of this class when
establishing a connection to the database
Statement
for issuing SQL statements
ResultSet (interface)
a ResultSet object represents the table returned
by an SQL select statement
Establishing a Connection
Use the getConnection() method
under the DriverManager class
String argument: "jdbc:driver:name”
returns a Connection object
Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);
// above line loads the jdbc-odbc driver
String dbname = “jdbc:odbc:MyDB”;
Connection c = DriverManager.getConnection(dbname);
Creating a
Statement Object
Execute the createStatement() method on the
Connection object
returns a Statement object
afterwards, run methods on the Statement object
to execute an SQL statement
Statement s = c.createStatement();
Methods of the
Statement Class
Methods of the Statement class require a string
parameter containing the SQL statement
executeQuery()
requires a String argument (a SELECT statement)
returns a ResultSet object representing the table returned
executeUpdate()
requires a String argument
(an INSERT, UPDATE, or DELETE statement)
returns an int (row count, in most cases)
The ResultSet Interface
A ResultSet object represents the table
returned by the select statement sent
Navigation/retrieval methods
next(): moves to the next row (first row if called
for the first time), returns false if no rows remain
getXXX() methods return the value of a field for
the current row
ResulSet example Need braces because
ORDER is a reserved
word in SQL
ResultSet rs;
rs = s.executeQuery(“SELECT * FROM [ORDER]”);
rs.next(); // gets the first row (use in a loop for multiple rows)
ResultSet rs = s.executeQuery(
“SELECT * FROM STUDENT WHERE QPI > 3.0”
);
while ( rs.next() )
{
String name = rs.getString(“LastName”);
int y = rs.getInt(“Year”);
double qpi = rs.getDouble(“QPI”);
System.out.println( name + “ ” + y + “ ” + qpi);
}
executeUpdate( ) example
…
Statement s = con.createStatement();
int result;
result = s.executeUpdate(
“DELETE FROM EMPLOYEE WHERE DeptCode=‘CS’”
);
System.out.println( result + “ rows deleted.” );
The PreparedStatement class
PreparedStatement: a Statement that
specifies parameters through Java code
The SQL statements take different forms
ResultSet rs = s.executeQuery(
“SELECT * FROM STUDENT WHERE LastName = ‘” + lastName +”’”
);
while ( rs.next() )
{
String name =
rs.getString(“LastName”) + rs.getString(“FirstName”);
int y = rs.getInt(“Year”);
double qpi = rs.getDouble(“QPI”);
System.out.println( name + “ ” + y + “ ” + qpi);
}
Version 2 (PreparedStatement)
// suppose lastName is a String variable
PreparedStatement s = con.prepareStatement(
“SELECT * FROM STUDENT WHERE LastName = ?”
);
s.setString( 1, lastName );
ResultSet rs = s.executeQuery(); the appropriate literal
is “inserted” in the query
while ( rs.next() )
{
String name =
rs.getString(“LastName”) + rs.getString(“FirstName”);
int y = rs.getInt(“Year”);
double qpi = rs.getDouble(“QPI”);
System.out.println( name + “ ” + y + “ ” + qpi);
}
Summary
JDBC allows you to write Java programs that
manipulate a database
A driver (often a separate product) is required
invocation