32
CSC 2720 Building Web Applications JDBC

CSC 2720 Building Web Applications JDBC. JDBC Introduction Java Database Connectivity (JDBC) is an API that enables Java developers to access any tabular

  • View
    223

  • Download
    0

Embed Size (px)

Citation preview

Page 1: CSC 2720 Building Web Applications JDBC. JDBC Introduction  Java Database Connectivity (JDBC) is an API that enables Java developers to access any tabular

CSC 2720Building Web Applications

JDBC

Page 2: CSC 2720 Building Web Applications JDBC. JDBC Introduction  Java Database Connectivity (JDBC) is an API that enables Java developers to access any tabular

JDBC Introduction Java Database Connectivity (JDBC) is an API

that enables Java developers to access any tabular data source (e.g. relational databases) from Java code.

For examples, it offers Methods to establish connection to databases Methods to initiating queries Methods to create stored (parameterized) queries Data structures to store queried results

Independent of SQL JDBC class is located in java.sql package

Page 3: CSC 2720 Building Web Applications JDBC. JDBC Introduction  Java Database Connectivity (JDBC) is an API that enables Java developers to access any tabular

JDBC DriversJDBC consists of two parts: JDBC API – a purely Java-based

API JDBC Driver Manager which

communicates with vendor-specific drivers that perform the real communication with the database.

Translation to vendor format is performed on the client

No changes needed to server Driver (translator) needed on

clientDatabase

JDBC Driver Manager

Java Application

JDBC API

JDBC Driver API

Vendor Specific JDBC Driver

Vendor SpecificODBC Driver

JDBC-ODBCBridge

Database

Page 4: CSC 2720 Building Web Applications JDBC. JDBC Introduction  Java Database Connectivity (JDBC) is an API that enables Java developers to access any tabular

Seven Steps in Using JDBC

1. Load the driver

2. Define the Connection URL

3. Establish the Connection

4. Create a Statement object

5. Execute a query

6. Process the results

7. Close the connection

Page 5: CSC 2720 Building Web Applications JDBC. JDBC Introduction  Java Database Connectivity (JDBC) is an API that enables Java developers to access any tabular

Step 1: Loading Driver try { // e.g.: Loading a driver written for MySql // Only needs to perform once. Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException cnfe) { System.out.println("Error loading driver: " + cnfe); }

The driver is a Java class provided by the database vendor or by third party developers.

Typically packaged into a .jar file

Different drivers have different names (refer to the documentation of the driver)

You must include the JAR file containing the driver in the CLASSPATH (or in the proper folder in NetBeans project)

Page 6: CSC 2720 Building Web Applications JDBC. JDBC Introduction  Java Database Connectivity (JDBC) is an API that enables Java developers to access any tabular

Step 2: Defining Connection URL

String host = "localhost";String dbName = "someName";

String mySqlUrl = "jdbc:mysql://" + host + "/" + dBName;

String jdbcOdbcUrl = "jdbc:odbc:" + dbName;

URL format is driver specific Typically contains

Protocol and subprotocol Hostname (+ port number if there is any) Database name

Page 7: CSC 2720 Building Web Applications JDBC. JDBC Introduction  Java Database Connectivity (JDBC) is an API that enables Java developers to access any tabular

Establishing ConnectionString dbUrl = "jdbc:mysql://localhost/Person";

String username = "root";

String password = "secret";

try {

Connection connection =

DriverManager.getConnection(dbUrl, username, password);

} catch (SQLException sqle) {

// … print error messages

}

getConnection can throw SQLException (Most methods in the java.sql package throws SQLException)

Page 8: CSC 2720 Building Web Applications JDBC. JDBC Introduction  Java Database Connectivity (JDBC) is an API that enables Java developers to access any tabular

Creating SQL statements

// connection has to be established successfully first

Statement statement = connection.createStatement();

A Statement object is what sends your SQL statement to the DBMS.

You can use the same Statement object to execute all your SQL statements.

Page 9: CSC 2720 Building Web Applications JDBC. JDBC Introduction  Java Database Connectivity (JDBC) is an API that enables Java developers to access any tabular

Executing SQL Statements

First, prepare an SQL statement as a String (does not need statement terminator)

Call executeQuery(sqlStatement) to execute SELECT statement.

Call executeUpdate(sqlStatement) to execute statements that would modify the database (e.g.: UPDATE, INSERT, DELETE)

Use setQueryTimeout to specify a maximum delay to wait for results

String query = "SELECT col1, col2, col3 FROM sometable";

ResultSet resultSet = statement.executeQuery(query);

String updateQuery = "INSERT INTO Messages" +

"VALUES ('CJ', '[email protected]', '')";

int count = statement.executeUpdate(updateQuery);

Page 10: CSC 2720 Building Web Applications JDBC. JDBC Introduction  Java Database Connectivity (JDBC) is an API that enables Java developers to access any tabular

Processing ResultSet

executeQuery() always returns an instance of ResultSet ResultSet is forward-only and read-only. Resultset has a "cursor" that points to the "current rows".

Initially it is not pointing to any row

ResultSet rs = statement.executeQuery(

"SELECT name, email, message FROM Messages"

);

name email messages

John [email protected] Some msg 1

Jack [email protected] Some msg 2

Column 1 Column 2 Column 3

Row Pointer (cursor)

Page 11: CSC 2720 Building Web Applications JDBC. JDBC Introduction  Java Database Connectivity (JDBC) is an API that enables Java developers to access any tabular

Processing ResultSet

Calling next() the first time makes the cursor points to the first row (If there is one).

next() returns false if there is no more row in the result set. Otherwise it returns true.

ResultSet rs = statement.executeQuery(

"SELECT name, email, message FROM Messages"

);

rs.next();

name email messages

John [email protected] Some msg 1

Jack [email protected] Some msg 2

Column 1 Column 2 Column 3

Row Pointer (cursor)

Page 12: CSC 2720 Building Web Applications JDBC. JDBC Introduction  Java Database Connectivity (JDBC) is an API that enables Java developers to access any tabular

Processing ResultSet

First column has index 1, not 0

ResultSet provides various getXxx(int idx) and getXxx(String name) methods that take a column index or column name and returns the data of appropriate type.

If no records found, executeQuery() returns an empty ResultSet instead of null.

// Typical way to retrieve data from a result set

while(rs.next()) {

System.out.println(resultSet.getString(1) + " " +

resultSet.getString(2) + " " +

resultSet.getString(3));

}

Page 13: CSC 2720 Building Web Applications JDBC. JDBC Introduction  Java Database Connectivity (JDBC) is an API that enables Java developers to access any tabular

Closing Connection

connection.close();

When you are done using the database, call close() to close the connection.

As opening a connection is expensive, postpone this step if additional database operations are expected.

Page 14: CSC 2720 Building Web Applications JDBC. JDBC Introduction  Java Database Connectivity (JDBC) is an API that enables Java developers to access any tabular

Microsoft Access Example

Northwind sample database

• Northwind.mdb located in C:\Program Files\Microsoft Office\Office\Samples

Page 15: CSC 2720 Building Web Applications JDBC. JDBC Introduction  Java Database Connectivity (JDBC) is an API that enables Java developers to access any tabular

MS Access Example: Create a data source Create System (Data Source Name) DSN through

ODBC data source (Run "odbcad32.exe")

Page 16: CSC 2720 Building Web Applications JDBC. JDBC Introduction  Java Database Connectivity (JDBC) is an API that enables Java developers to access any tabular

JDBC-ODBC Bridge ODBC (Open DataBase Connectivity)

– A set of APIs for Database access

– Originally, designed for windows platforms

– Now, it extends to non-windows platforms

– Using C interfaces

More information:– http://www.microsoft.com/data/odbc/default.htm

Page 17: CSC 2720 Building Web Applications JDBC. JDBC Introduction  Java Database Connectivity (JDBC) is an API that enables Java developers to access any tabular

import java.sql.*;

public class OdbcAccessDemo { public static void main(String[] args) { try { // This driver comes with JDK Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

// No user and password required Connection connection = DriverManager.getConnection("jdbc:odbc:Northwind","","");

Statement statement = connection.createStatement(); String query = "SELECT FirstName, LastName FROM Employees"; ResultSet rs = statement.executeQuery(query);

while (rs.next()) { System.out.println(rs.getString(1) + " " + rs.getString(2)); } connection.close(); } catch (ClassNotFoundException cnfe) { System.err.println("Couldn't find class file" + cnfe); } catch (SQLException sqle) { System.err.println("SQL Exception: " + sqle); } }}

MS Access Example (Continued)

Page 18: CSC 2720 Building Web Applications JDBC. JDBC Introduction  Java Database Connectivity (JDBC) is an API that enables Java developers to access any tabular

ResultSet Overview

A ResultSet contains the results of the SQL query Represented by a table with rows and columns In JDBC 1.0 you can only proceed forward through

the rows using next

Useful Methods All methods can throw a SQLException

close Releases the JDBC and database resources The result set is automatically closed when the

associated Statement object executes a new query

Page 19: CSC 2720 Building Web Applications JDBC. JDBC Introduction  Java Database Connectivity (JDBC) is an API that enables Java developers to access any tabular

ResultSet (Useful Methods)

getMetaDataObject Returns a ResultSetMetaData object containing

information about the columns in the ResultSet

next Attempts to move to the next row in the ResultSet

If successful true is returned; otherwise, false The first call to next positions the cursor a the first

row Calling next clears the SQLWarning chain

getWarnings Returns the first SQLWarning or null if no warnings

occurred

Page 20: CSC 2720 Building Web Applications JDBC. JDBC Introduction  Java Database Connectivity (JDBC) is an API that enables Java developers to access any tabular

ResultSet (Useful Methods) findColumn

Returns the corresponding integer value corresponding to the specified column name

Column numbers in the result set do not necessarily map to the same column numbers in the database

getXxx

Returns the value from the column specified by column name or column index as an Xxx Java type

Returns 0 or null, if the value is a SQL NULL Legal getXxx types:

wasNull Used to check if the last getXxx read was a SQL NULL

double byte int Date Stringfloat short long Time Object

Page 21: CSC 2720 Building Web Applications JDBC. JDBC Introduction  Java Database Connectivity (JDBC) is an API that enables Java developers to access any tabular

Using Statement (Overview)

Through the Statement object, SQL statements are sent to the database.

Two types of statement objects are available: Statement

for executing a simple SQL statements PreparedStatement

for executing a precompiled SQL statement passing in parameters

Page 22: CSC 2720 Building Web Applications JDBC. JDBC Introduction  Java Database Connectivity (JDBC) is an API that enables Java developers to access any tabular

Useful Statement Methods executeQuery

Executes the SQL query and returns the data in a table (ResultSet) The resulting table may be empty but never null

ResultSet results =

statement.executeQuery("SELECT a, b FROM table");

executeUpdate Used to execute for INSERT, UPDATE, or DELETE SQL statements The return is the number of rows that were affected in the database Also supports Data Definition Language (DDL) statements CREATE

TABLE, DROP TABLE and ALTER TABLE

int rows = statement.executeUpdate(

"DELETE FROM employees WHERE status=0");

Page 23: CSC 2720 Building Web Applications JDBC. JDBC Introduction  Java Database Connectivity (JDBC) is an API that enables Java developers to access any tabular

Useful Statement Methods (Continued)

execute Generic method for executing stored procedures and prepared

statements Rarely used (for multiple return result sets) The statement execution may or may not return a ResultSet (use

statement.getResultSet). If the return value is true, two or more result sets were produced

getMaxRows/setMaxRows Determines the number of rows a ResultSet may contain Unless explicitly set, the number of rows are unlimited

getQueryTimeout/setQueryTimeout Specifies the amount of a time a driver will wait for a STATEMENT

to complete before throwing a SQLException

Page 24: CSC 2720 Building Web Applications JDBC. JDBC Introduction  Java Database Connectivity (JDBC) is an API that enables Java developers to access any tabular

Prepared Statements (Precompiled Queries)

Idea If you are going to execute similar SQL statements multiple

times, using “prepared” (parameterized) statements is more efficient

Create a statement in standard form that is sent to the database for compilation before actually being used

Each time you use it, you simply replace some of the marked parameters using the setXxx methods

To execute a "prepared statement", use the following methods (without parameters) execute() executeQuery() executeUpdate()

Page 25: CSC 2720 Building Web Applications JDBC. JDBC Introduction  Java Database Connectivity (JDBC) is an API that enables Java developers to access any tabular

Prepared Statement, Example

Connection connection = DriverManager.getConnection(url, user, password);PreparedStatement statement = connection.prepareStatement("UPDATE employees " + "SET salary = ? " + "WHERE id = ?");int[] newSalaries = getSalaries();int[] employeeIDs = getIDs();for (int i = 0; i < employeeIDs.length; i++) { statement.setInt(1, newSalaries[i]); statement.setInt(2, employeeIDs[i]); statement.executeUpdate();}

Page 26: CSC 2720 Building Web Applications JDBC. JDBC Introduction  Java Database Connectivity (JDBC) is an API that enables Java developers to access any tabular

Useful Prepared Statement Methods

setXxx Sets the indicated parameter (?) in the SQL statement to the

specified value

clearParameters Clears all set parameter values in the statement

Handling Servlet Data Query data obtained from a user through an HTML form may have

SQL or special characters that may require escape sequences To handle the special characters, pass the string to the PreparedStatement setString method which will automatically escape the special characters in the string

Page 27: CSC 2720 Building Web Applications JDBC. JDBC Introduction  Java Database Connectivity (JDBC) is an API that enables Java developers to access any tabular

Exception Handling SQL Exceptions

Nearly every JDBC method can throw a SQLException in response to a data access error

If more than one error occurs, they are chained together

SQL exceptions contain: Description of the error, getMessage The SQLState (Open Group SQL specification) identifying

the exception, getSQLState A vendor-specific integer error code, getErrorCode A chain to the next SQLException, getNextException

Page 28: CSC 2720 Building Web Applications JDBC. JDBC Introduction  Java Database Connectivity (JDBC) is an API that enables Java developers to access any tabular

SQL Exception Exampletry {

... // JDBC statement.

} catch (SQLException sqle) { while (sqle != null) { System.out.println("Message: " + sqle.getMessage()); System.out.println("SQLState: " + sqle.getSQLState()); System.out.println("Vendor Error: " + sqle.getErrorCode()); sqle.printStrackTrace(System.out); sqle = sqle.getNextException(); }}

Page 29: CSC 2720 Building Web Applications JDBC. JDBC Introduction  Java Database Connectivity (JDBC) is an API that enables Java developers to access any tabular

SQL Warnings SQLWarnings are rare, but provide information about the database

access warnings Chained to object whose method produced the warning The following objects can receive a warning:

Connection Statement ResultSet

Call getWarning to obtain the warning object, and getNextWarning (on the warning object) for any additional warnings

Warnings are cleared on the object each time the statement is executed

Page 30: CSC 2720 Building Web Applications JDBC. JDBC Introduction  Java Database Connectivity (JDBC) is an API that enables Java developers to access any tabular

SQL Warning, ExampleResultSet results = statement.executeQuery(someQuery);SQLWarning warning = statement.getWarnings();while (warning != null) { System.out.println("Message: " + warning.getMessage()); System.out.println("SQLState: " + warning.getSQLState()); System.out.println("Vendor Error: " + warning.getErrorCode()); warning = warning.getNextWarning();}while (results.next()) { int value = rs.getInt(1); ... // Call additonal methods on result set. SQLWarning warning = results.getWarnings(); while (warning != null) { System.out.println("Message: " + warning.getMessage()); System.out.println("SQLState: " + warning.getSQLState()); System.out.println("Vendor Error: " + warning.getErrorCode()); warning = warning.getNextWarning(); } }

Page 31: CSC 2720 Building Web Applications JDBC. JDBC Introduction  Java Database Connectivity (JDBC) is an API that enables Java developers to access any tabular

Summary Why use database? How to use JDBC? In JDBC 1.0, ResultSet is forward-only and read-only. Be sure to handle the situation where getXxx returns a

NULL Understand the relationship among:

Connection object Statement object ResultSet object

By default, a connection is auto-commit SQL Exceptions and Warnings are chained together

Page 32: CSC 2720 Building Web Applications JDBC. JDBC Introduction  Java Database Connectivity (JDBC) is an API that enables Java developers to access any tabular

On-line Resources Sun’s JDBC Site

http://java.sun.com/products/jdbc/

JDBC Tutorial http://java.sun.com/docs/books/tutorial/jdbc/

List of Available JDBC Drivers http://industry.java.sun.com/products/jdbc/drivers/

API for java.sql (See JDK Documentation)

MySQL http://www.mysql.com/