Download presentation
Presentation is loading. Please wait.
Published byNora Briggs Modified over 9 years ago
1
Connecting to Oracle using Java November 4, 2009 David Goldschmidt, Ph.D. David Goldschmidt, Ph.D. goldschmidt@gmail.com goldschmidt@gmail.com
2
JDBC is a platform-independent Java API for executing SQL statements JDBC is a platform-independent Java API for executing SQL statements Use JDBC to: Use JDBC to: Connect to a database Connect to a database Send SQL statements Send SQL statements Receive results Receive results i.e. rows of data i.e. rows of data Add or update existing rows of data Add or update existing rows of data Call PL/SQL procedures, functions, etc. Call PL/SQL procedures, functions, etc. JDBC Java program database JDBC
3
Before connecting to a database, a driver class must first be loaded into the Java Virtual Machine (or JVM) Before connecting to a database, a driver class must first be loaded into the Java Virtual Machine (or JVM) A driver is simply a class in Java A driver is simply a class in Java MySQL: "com.mysql.jdbc.Driver" MySQL: "com.mysql.jdbc.Driver" Oracle: "oracle.jdbc.driver.OracleDriver" Oracle: "oracle.jdbc.driver.OracleDriver" Oracle driver is located within the ojdbc14.jar JAR file of the Oracle distribution Oracle driver is located within the ojdbc14.jar JAR file of the Oracle distribution JDBC Drivers
4
Sun defines four categories of JDBC drivers: Sun defines four categories of JDBC drivers: 1. JDBC bridge driver – uses native code to connect a Java client to a third-party API (e.g. JDBC-ODBC) 2. Native API (part Java driver) – wraps native code with Java classes (e.g. Oracle Call Interface (OCI ) driver) 3. Network protocol (pure Java driver) – Java classes communicate via a network protocol to a middle- tier server, which communicates with the database 4. Native protocol (pure Java driver) – Java classes communicate directly with the database (e.g. Thin) JDBC Drivers
5
Oracle RDBMS Oracle Listener Oracle Call Interface (OCI) JDBC OCI driver JDBC-ODBC driver ODBC driver JDBC Thin driver
6
Connect to a database using its connect string (i.e. its Connection URL): Connect to a database using its connect string (i.e. its Connection URL): Access: "jdbc:odbc:dataSource" Access: "jdbc:odbc:dataSource" MySQL: "jdbc:mysql://hostname/dbname" MySQL: "jdbc:mysql://hostname/dbname" Oracle: "jdbc:oracle:thin:@hostname:port#:SID" Oracle: "jdbc:oracle:thin:@hostname:port#:SID" Use a Connection object: Use a Connection object: Connection connection = DriverManager.getConnection( dbConnectURL ); Connection connection = DriverManager.getConnection( dbConnectURL ); JDBC Connection Strings
7
JDBC Interfaces Driver Connection Statement ResultSet
8
Using JDBC
9
Statement createStatement() Statement createStatement() Prepare a query without parameters Prepare a query without parameters Result set is read-only and forward-only Result set is read-only and forward-only For repeated queries, gain performance speedup by using prepareStatement() instead For repeated queries, gain performance speedup by using prepareStatement() instead Building Statements
10
PreparedStatement prepareStatement( String sql ) PreparedStatement prepareStatement( String sql ) Prepare a parameterized query Prepare a parameterized query Result set is read-only and forward-only Result set is read-only and forward-only Building Statements
11
CallableStatement prepareCall( String sql ) CallableStatement prepareCall( String sql ) Prepare a call to a stored procedure Prepare a call to a stored procedure Register any OUT (or IN OUT ) parameters Register any OUT (or IN OUT ) parameters Set any IN (or IN OUT ) parameters Set any IN (or IN OUT ) parameters Results are read-only and forward-only Results are read-only and forward-only Building Statements
12
boolean execute( String sql ) boolean execute( String sql ) Use this method to execute DDL statements and stored procedures Use this method to execute DDL statements and stored procedures Return value indicates whether a ResultSet object is available Return value indicates whether a ResultSet object is available Processing Statements might be empty if zero rows!
13
ResultSet executeQuery( String sql ) ResultSet executeQuery( String sql ) Use this method to execute DDL statements you expect to receive results from Use this method to execute DDL statements you expect to receive results from i.e. Use for your SELECT statements i.e. Use for your SELECT statements Processing Statements
14
int executeUpdate( String sql ) int executeUpdate( String sql ) Use this method to execute INSERT, UPDATE, and DELETE statements Use this method to execute INSERT, UPDATE, and DELETE statements Return value is the number of rows affected Return value is the number of rows affected Processing Statements
15
Default ResultSet behavior is read-only and forward-only Default ResultSet behavior is read-only and forward-only Change default using resultSetType and resultSetConcurrency parameters Change default using resultSetType and resultSetConcurrency parameters For resultSetType : For resultSetType : TYPE_FORWARD_ONLY TYPE_FORWARD_ONLY TYPE_SCROLL_INSENSITIVE TYPE_SCROLL_INSENSITIVE TYPE_SCROLL_SENSITIVE TYPE_SCROLL_SENSITIVE Processing Results ResultSet sensitivity refers to whether database changes made while the ResultSet object is open are visible scrolling uses a client-side memory cache
16
For resultSetConcurrency : For resultSetConcurrency : CONCUR_READ_ONLY CONCUR_READ_ONLY CONCUR_UPDATABLE CONCUR_UPDATABLE To implement updateability, all queries request the ROWID for each row To implement updateability, all queries request the ROWID for each row Processing Results ResultSet ROWID is a proprietary SQL data type that uniquely identifies each row of the database
17
A scroll-sensitive ResultSet must: A scroll-sensitive ResultSet must: Perform SELECT against only one table Perform SELECT against only one table Explicitly specify columns (i.e. not SELECT * ) Explicitly specify columns (i.e. not SELECT * ) Not use an ORDER BY clause Not use an ORDER BY clause An updateable ResultSet must also: An updateable ResultSet must also: Include all nonnullable columns (for INSERT ) Include all nonnullable columns (for INSERT ) Sensitivity and Updateability
18
Improve performance by combining multiple SQL statements into a batch Improve performance by combining multiple SQL statements into a batch Disable auto-commit Disable auto-commit Oracle supports PreparedStatement batching only Oracle supports PreparedStatement batching only Call addBatch() instead of executeUpdate() Call addBatch() instead of executeUpdate() Repeat! Repeat! Then call executeBatch() and commit() Then call executeBatch() and commit() Tune frequency of commits Tune frequency of commits Use clearBatch() to cancel your batch Use clearBatch() to cancel your batch Batching
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.