slides Topic slides

Download Report

Transcript slides Topic slides

Intro to JDBC
Joseph Sant
Applied Computing and Engineering Sciences
Sheridan ITAL
Persistence


Retaining information in a database or file after a
user request.
Could be implemented using
 Relational
databases.
 Files.
 Object-Oriented
Databases.
 Object-Relational Databases.
JDBC


Set of classes for interacting with a Relational
Database.
Implementation Neutral. The API is designed
with the aim that code, once written can be run
against several different database
implementations (e.g. Oracle, PostGresQL)
without modification. The reality is that true
implementation neutrality is still difficult.
JDBC
Ansi SQL
Abstract
SQL in
Java Program
Vendor’s SQL
JDBC
Driver
Java-Friendly
ResultSet
Vendor
Specific
Execution of
Java
SQL
SQL ResultSet
JDBC-Key terms


Connection. An authorized connection between
a Java Program or servlet and a running
database.
Driver. Software object to take JDBC API calls
from a Java program or servlet, translate it
appropriately for the specific database, then
pass it on to that database.
JDBC-Key terms

Query. A statement that requests a Result Set from a
database. It does NOT change the state of the
database.


SELECT
Update. A statement that potentially changes the state of
the database. It may be any of the following:





DELETE
INSERT
UPDATE
DROP
Others
Overview of Database Processing using
JDBC
1.
2.
3.
Load Database Driver (once).
Establish a Connection to database from program (usually once
but not always).
Create a Statement object to enable actions against the database.
1.
2.
3.
Execute Queries using executeQuery() (repetitively). Process the
ResultSet for each executeQuery().
AND/OR
Execute Updates using executeUpdate() (repetitively). Return result
of executeUpdate() should be tested and responded to.
Establishing a Connection: PreRequisites.



The database engine you are connecting to is
running.
The program or servlet connecting to the
database has access rights to that database.
The driver, which is a java class file or jar file, is
accessible to either the application, applet or
servlet.
Establishing a Connection: Technique



Dynamically Load the driver class using
Class.forName. Driver must be available on
CLASSPATH or in the case of servlets in the
common/lib folder.
Attempt to get a connection by calling the
getConnection method of the DriverManager
class.
Always handle a failure to connect.
Establishing a Connection: Technique
//Load driver class by package name then create instance
Class.forName("com.mysql.jdbc.Driver").newInstance();
String url = "jdbc:mysql://localhost:3306/RentalDatabase";
// Connect using a url, a username without password
con = DriverManager.getConnection(url, "RentalApp", "rentcars");
Statements

Create a Statement object to execute an SQL
command.
Statement stmt = con.createStatement();


Use the executeUpdate() to execute a
delete, drop, insert, or update (and other SQL
commands that change the state of the
database).
Use the executeQuery() method to execute a
select.
Statements


ResultSet rs = state.executeQuery(“Select
* from TableName”);
Create a Statement object to execute an SQL
command.
Statement stmt = con.createStatement();


Use the executeUpdate() to execute a delete, drop,
insert, or update (and other SQL commands that change
the state of the database).
Use the executeQuery() method to execute a select.
Processing ResultSets

A query returns 0 or more rows of into a
ResultSet.
ResultSet rs = stmt.executeQuery(“Select *
from TableName”);

You iterate through the results in a result set
using the next() method processing the data
1 row at a time.
while (rs.next()) {
// do something with current row.
}

Close the result set using close()
rs.close();
Processing a Row in a ResultSet


Rows returned during an iteration through a ResultSet
contain fields. These fields must be converted into
Java data types from the field type in the database.
You can convert a field into a Java type by specifying
either the Field Name or field number (if you know it)
and choosing the conversion (getDouble,getInt,
getString).
while (rs.next()) {
System.out.println(rs.getDouble(1));
System.out.println(rs.getString(“LastName”);
System.out.println(rs.getInt(3));
}

wasNull() tests whether the last conversion resulted in a
null.