Connecting to Oracle using Java November 4, 2009 David Goldschmidt, Ph.D. David Goldschmidt, Ph.D.

  • Published on

  • View

  • Download


  • Connecting to Oracle using JavaNovember 4, 2009

    David Goldschmidt, Ph.D.

  • JDBCJDBC is a platform-independent Java API for executing SQL statements

    Use JDBC to:Connect to a databaseSend SQL statementsReceive resultsi.e. rows of dataAdd or update existing rows of dataCall PL/SQL procedures, functions, etc.JDBC

  • JDBC DriversBefore 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 JavaMySQL:"com.mysql.jdbc.Driver" Oracle:"oracle.jdbc.driver.OracleDriver"

    Oracle driver is located within the ojdbc14.jar JAR file of the Oracle distribution

  • JDBC DriversSun defines four categories of JDBC drivers:JDBC bridge driver uses native code to connect a Java client to a third-party API (e.g. JDBC-ODBC)Native API (part Java driver) wraps native code with Java classes (e.g. Oracle Call Interface (OCI ) driver)Network protocol (pure Java driver) Java classes communicate via a network protocol to a middle-tier server, which communicates with the databaseNative protocol (pure Java driver) Java classes communicate directly with the database (e.g. Thin)

  • JDBC DriversOracle RDBMS

  • JDBC Connection StringsConnect to a database using its connect string (i.e. its Connection URL):Access:"jdbc:odbc:dataSource" MySQL:"jdbc:mysql://hostname/dbname" Oracle:"jdbc:oracle:thin:@hostname:port#:SID"

    Use a Connection object: Connection connection = DriverManager.getConnection( dbConnectURL );

  • JDBC InterfacesDriver

  • Using JDBC

  • Building Statements

    Statement createStatement() Prepare a query without parametersResult set is read-only and forward-only For repeated queries, gain performance speedup by using prepareStatement() instead

  • Building Statements

    PreparedStatement prepareStatement( String sql ) Prepare a parameterized queryResult set is read-only and forward-only

  • Building Statements

    CallableStatement prepareCall( String sql ) Prepare a call to a stored procedureRegister any OUT (or IN OUT) parametersSet any IN (or IN OUT) parametersResults are read-only and forward-only

  • Processing Statementsboolean execute( String sql ) Use this method to execute DDL statements and stored proceduresReturn value indicates whether a ResultSet object is available

  • Processing StatementsResultSet executeQuery( String sql ) Use this method to execute DDL statements you expect to receive results fromi.e. Use for your SELECT statements

  • Processing Statementsint executeUpdate( String sql ) Use this method to execute INSERT, UPDATE, and DELETE statementsReturn value is the number of rows affected

  • Processing ResultsDefault ResultSet behavior is read-only and forward-only Change default using resultSetType and resultSetConcurrency parametersFor resultSetType: TYPE_FORWARD_ONLY TYPE_SCROLL_INSENSITIVE TYPE_SCROLL_SENSITIVE scrolling uses a client-side memory cache

  • Processing ResultsFor resultSetConcurrency: CONCUR_READ_ONLY CONCUR_UPDATABLE

    To implement updateability, all queries request the ROWID for each row

  • Sensitivity and UpdateabilityA scroll-sensitive ResultSet must:Perform SELECT against only one tableExplicitly specify columns (i.e. not SELECT *)Not use an ORDER BY clauseAn updateable ResultSet must also:Include all nonnullable columns (for INSERT)

  • BatchingImprove performance by combining multiple SQL statements into a batch Disable auto-commitOracle supports PreparedStatement batching onlyCall addBatch() instead of executeUpdate() Repeat!Then call executeBatch() and commit() Tune frequency of commitsUse clearBatch() to cancel your batch


View more >