Académique Documents
Professionnel Documents
Culture Documents
Objectives
After completing this lesson, you should be able to do the following: Describe how Java code connects to the database Describe how Java database functionality is supported by the Oracle database Load and register a JDBC driver Connect to an Oracle database Perform a simple SELECT statement Map simple Oracle database types to Java types Use a pooled connection
15 - 2
Client
Application server
Business logic
Data
Oracle database
JDBC
15 - 3
JDBC
Relational database
15 - 4
15 - 5
You must include the Oracle JDBC driver archive file in the CLASSPATH. The JDBC class library is part of Java Platform, Standard Edition (Java SE).
Copyright 2009, Oracle. All rights reserved.
15 - 6
2. Obtain a connection.
15 - 7
15 - 8
Database commands
Database
15 - 9
Applet JDBC
Thin driver
Client
Oracle
Server
15 - 10
Server
15 - 11
Type of Program
Applet Client application EJB, servlet (on the middle tier)
Driver
Thin Thin Thin OCI Server side OCI
Stored procedure
15 - 12
Example:
Connection conn = DriverManager.getConnection( "jdbc:oracle:thin:@myhost:1521:ORCL", "scott","tiger");
15 - 13
JDBC URLs
JDBC uses a URL-like string; the URL identifies:
The JDBC driver to use for the connection Database connection details, which vary depending on the driver used
jdbc:<subprotocol>:<subname> Protocol Database identification
jdbc:oracle:<driver>:@<database>
15 - 14
Example: "jdbc:oracle:thin:@eduhost:1521:orcl"
15 - 15
15 - 16
15 - 17
15 - 18
ResultSet Object
The JDBC driver returns the results of a query in a ResultSet object. ResultSet:
Maintains a cursor pointing to its current row of data Provides methods to retrieve column values
15 - 19
Example:
Statement stmt = conn.createStatement(); int rowcount = stmt.executeUpdate ("DELETE FROM order_items WHERE order_id = 2354");
15 - 20
Example:
Statement stmt = conn.createStatement(); int rowcount = stmt.executeUpdate ("CREATE TABLE temp (col1 NUMBER(5,2), col2 VARCHAR2(30)");
15 - 21
15 - 22
Type
Method
getInt() getDate() getString()
15 - 23
Step 6: Clean Up
Explicitly close a Connection, Statement, and ResultSet object to release resources that are no longer needed. Call their respective close()methods:
Connection conn = ...; Statement stmt = ...; ResultSet rset = stmt.executeQuery( "SELECT ename FROM emp"); ... // clean up rset.close(); stmt.close(); conn.close(); ...
15 - 25
15 - 27
Handling Exceptions
SQL statements can throw a java.sql.SQLException. Use standard Java error handling methods.
try { rset = stmt.executeQuery("SELECT empno, ename FROM emp"); } catch (java.sql.SQLException e) { ... /* handle SQL errors */ }
... finally { // clean up try { if (rset != null) rset.close(); } catch (Exception e) { ... /* handle closing errors */ } ...
15 - 28
15 - 29
PreparedStatement Object
A prepared statement prevents reparsing of SQL statements. Use the PreparedStatement object for statements that you want to execute more than once. A prepared statement can contain variables that you supply each time you execute the statement.
15 - 31
15 - 32
15 - 33
What Is a DataSource?
A DataSource object: Is the representation of a data sourcea facility for storing datain the Java programming language. Can reside on a remote server or on a local desktop machine. Can be thought of as a factory for connections to the particular data source that the DataSource instance represents. Can optionally be bound to Java Naming and Directory (JNDI) entities so that you can access databases by logical names for convenience and portability.
15 - 34
15 - 35
15 - 36
15 - 38
Connection Pooling
Middle tier
15 - 40
Summary
In this lesson, you should have learned how to: Load and register a JDBC driver Use the driver to connect to an Oracle database Perform a simple SELECT statement Process the results of the query by iterating through the rows of a result set Create and use a PreparedStatement object Use an OracleDataSource to connect to a database Use a pooled connection
15 - 43
15 - 44