Documente Academic
Documente Profesional
Documente Cultură
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
Web
server
Application
server
Presentation
Business
logic
15 - 3
Data
Oracle
database
Client application
or applet
15 - 4
JDBC
Relational
database
15 - 5
15 - 6
Query Statement.
5. Process query results.
15 - 7
or DDL statement.
6. Clean up.
15 - 8
OracleDriver
JDBC calls
15 - 9
Thin client
OCI-based
Server-based
Database
commands
Database
Applet
JDBC
Thin driver
Client
15 - 10
Oracle
Server
Application
JDBC
OCI driver
ocixxx.dll
Client
15 - 11
Oracle
Server
Type of Program
Driver
Applet
Thin
Client application
Thin
EJB, servlet
(on the middle tier)
Stored procedure
15 - 12
Thin
OCI
Server side
OCI
Example:
Connection conn =
DriverManager.getConnection(
"jdbc:oracle:thin:@myhost:1521:ORCL",
"scott","tiger");
15 - 13
JDBC URLs
Database identification
jdbc:oracle:<driver>:@<database>
15 - 14
15 - 15
15 - 16
15 - 17
15 - 18
ResultSet Object
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
15 - 23
Type
Method
empno
NUMBER
getInt()
hiredate
DATE
getDate()
job
VARCHAR2
getString()
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
{
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
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
Java servlet
Data source
Middle-tier server code
ConnectionPoolDataSource
JDBC
driver
Database
Database
commands
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
Practice 15 Overview:
Using JDBC to Access the Database
This practice covers the following topics:
Setting up the Java environment for JDBC
Adding JDBC components to query the database
15 - 44