Oracle Database JDBC Developer’s Guide and Reference

[Pages:506]Oracle? Database

JDBC Developer's Guide 11g Release 2 (11.2)

E16548-03

September 2011 This book describes how to use Oracle JDBC drivers to develop powerful Java database applications.

Oracle Database JDBC Developer's Guide, 11g Release 2 (11.2)

E16548-03

Copyright ? 1999, 2011, Oracle and/or its affiliates. All rights reserved.

Primary Author: Tulika Das, Venkatasubramaniam Iyer, Elizabeth Hanes Perry, Brian Wright, Thomas Pfaeffle

Contributing Author: Brian Martin

Contributor: Kuassi Mensah, Douglas Surber, Paul Lo, Ed Shirk, Tong Zhou, Jean de Lavarene, Rajkumar Irudayaraj, Ashok Shivarudraiah, Angela Barone, Rosie Chen, Sunil Kunisetty, Joyce Yang, Mehul Bastawala, Luxi Chidambaran, Srinath Krishnaswamy, Longxing Deng, Magdi Morsi, Ron Peterson, Ekkehard Rohwedder, Catherine Wong, Scott Urman, Jerry Schwarz, Steve Ding, Soulaiman Htite, Anthony Lai, Prabha Krishna, Ellen Siegal, Susan Kraft, Sheryl Maring

This software and related documentation are provided under a license agreement containing restrictions on use and disclosure and are protected by intellectual property laws. Except as expressly permitted in your license agreement or allowed by law, you may not use, copy, reproduce, translate, broadcast, modify, license, transmit, distribute, exhibit, perform, publish, or display any part, in any form, or by any means. Reverse engineering, disassembly, or decompilation of this software, unless required by law for interoperability, is prohibited.

The information contained herein is subject to change without notice and is not warranted to be error-free. If you find any errors, please report them to us in writing.

If this is software or related documentation that is delivered to the U.S. Government or anyone licensing it on behalf of the U.S. Government, the following notice is applicable:

U.S. GOVERNMENT RIGHTS Programs, software, databases, and related documentation and technical data delivered to U.S. Government customers are "commercial computer software" or "commercial technical data" pursuant to the applicable Federal Acquisition Regulation and agency-specific supplemental regulations. As such, the use, duplication, disclosure, modification, and adaptation shall be subject to the restrictions and license terms set forth in the applicable Government contract, and, to the extent applicable by the terms of the Government contract, the additional rights set forth in FAR 52.227-19, Commercial Computer Software License (December 2007). Oracle America, Inc., 500 Oracle Parkway, Redwood City, CA 94065.

This software or hardware is developed for general use in a variety of information management applications. It is not developed or intended for use in any inherently dangerous applications, including applications that may create a risk of personal injury. If you use this software or hardware in dangerous applications, then you shall be responsible to take all appropriate fail-safe, backup, redundancy, and other measures to ensure its safe use. Oracle Corporation and its affiliates disclaim any liability for any damages caused by use of this software or hardware in dangerous applications.

Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners.

Intel and Intel Xeon are trademarks or registered trademarks of Intel Corporation. All SPARC trademarks are used under license and are trademarks or registered trademarks of SPARC International, Inc. AMD, Opteron, the AMD logo, and the AMD Opteron logo are trademarks or registered trademarks of Advanced Micro Devices. UNIX is a registered trademark of The Open Group.

This software or hardware and documentation may provide access to or information on content, products, and services from third parties. Oracle Corporation and its affiliates are not responsible for and expressly disclaim all warranties of any kind with respect to third-party content, products, and services. Oracle Corporation and its affiliates will not be responsible for any loss, costs, or damages incurred due to your access to or use of third-party content, products, or services.

Contents

Preface ............................................................................................................................................................... xxi

Audience..................................................................................................................................................... xxi Documentation Accessibility ................................................................................................................... xxi Related Documents .................................................................................................................................. xxii Conventions ............................................................................................................................................. xxiv

What's New.................................................................................................................................................... xxix

New Features for Release 2 (11.2) ......................................................................................................... xxix

Part I Overview

1 Introducing JDBC

Overview of Oracle JDBC Drivers........................................................................................................ 1-1 Common Features of Oracle JDBC Drivers .................................................................................... 1-2 Choosing the Appropriate Driver.................................................................................................... 1-4 Feature Differences Between JDBC OCI and Thin Drivers.......................................................... 1-4

Environments and Support .................................................................................................................... 1-5 Supported JDK and JDBC Versions................................................................................................. 1-5 JNI and Java Environments .............................................................................................................. 1-5 JDBC and IDEs.................................................................................................................................... 1-5

Feature List ................................................................................................................................................ 1-5

2 Getting Started

Version Compatibility for Oracle JDBC Drivers ............................................................................... 2-1 Verification of a JDBC Client Installation .......................................................................................... 2-2

Check the Installed Directories and Files ....................................................................................... 2-2 Check the Environment Variables ................................................................................................... 2-3 Ensure that the Java Code Can Be Compiled and Run ................................................................ 2-5 Determine the Version of the JDBC Driver .................................................................................... 2-5 Test JDBC and the Database Connection........................................................................................ 2-5 Basic Steps in JDBC ................................................................................................................................. 2-7 Importing Packages ........................................................................................................................... 2-8 Opening a Connection to a Database .............................................................................................. 2-8 Creating a Statement Object ............................................................................................................. 2-9

iii

Running a Query and Retrieving a Result Set Object................................................................... 2-9 Processing the Result Set Object ................................................................................................... 2-10 Closing the Result Set and Statement Objects............................................................................. 2-10 Making Changes to the Database ................................................................................................. 2-11 Committing Changes...................................................................................................................... 2-13

Changing Commit Behavior................................................................................................... 2-13 Closing the Connection .................................................................................................................. 2-14 Sample: Connecting, Querying, and Processing the Results........................................................ 2-14 Stored Procedure Calls in JDBC Programs....................................................................................... 2-15 PL/SQL Stored Procedures ........................................................................................................... 2-15 Java Stored Procedures................................................................................................................... 2-16 Processing SQL Exceptions ................................................................................................................. 2-16

Part II Oracle JDBC

3 JDBC Standards Support

Support for JDBC 2.0 Standard.............................................................................................................. 3-1 Data Type Support ............................................................................................................................. 3-2 Standard Feature Support................................................................................................................. 3-2 Extended Feature Support ................................................................................................................ 3-2 Standard versus Oracle Performance Enhancement APIs ........................................................... 3-2

Support for JDBC 3.0 Standard.............................................................................................................. 3-2 Transaction Savepoints...................................................................................................................... 3-3 Creating a Savepoint .................................................................................................................. 3-3 Rolling Back to a Savepoint ....................................................................................................... 3-3 Releasing a Savepoint................................................................................................................. 3-4 Checking Savepoint Support..................................................................................................... 3-4 Savepoint Notes .......................................................................................................................... 3-4 Retrieval of Auto-Generated Keys................................................................................................... 3-4 java.sql.Statement ....................................................................................................................... 3-4 Sample Code ................................................................................................................................ 3-4 Limitations ................................................................................................................................... 3-5 JDBC 3.0 LOB Interface Methods..................................................................................................... 3-5 Result Set Holdability........................................................................................................................ 3-6

Support for JDBC 4.0 Standard.............................................................................................................. 3-6 Wrapper Pattern Support.................................................................................................................. 3-7 SQLXML Type .................................................................................................................................... 3-7 Enhanced Exception Hierarchy and SQLException................................................................... 3-10 The RowId Data Type..................................................................................................................... 3-10 LOB Creation ................................................................................................................................... 3-10 National Language Character Set Support.................................................................................. 3-11

4 Oracle Extensions

Overview of Oracle Extensions ............................................................................................................. 4-1 Features of the Oracle Extensions ........................................................................................................ 4-1

Database Management Using JDBC................................................................................................ 4-2

iv

Support for Oracle Data Types......................................................................................................... 4-2 Support for Oracle Objects................................................................................................................ 4-3 Support for Schema Naming ............................................................................................................ 4-4 DML Returning .................................................................................................................................. 4-4 Accessing PL/SQL Index-by Tables................................................................................................ 4-5 Oracle JDBC Packages............................................................................................................................. 4-5 Package oracle.sql .............................................................................................................................. 4-5 Package oracle.jdbc ............................................................................................................................ 4-9 Oracle Character Data Types Support............................................................................................... 4-10 SQL CHAR Data Types .................................................................................................................. 4-10 SQL NCHAR Data Types............................................................................................................... 4-10 Class oracle.sql.CHAR.................................................................................................................... 4-11 Additional Oracle Type Extensions................................................................................................... 4-13 Oracle ROWID Type....................................................................................................................... 4-13 Oracle REF CURSOR Type Category ........................................................................................... 4-14 Oracle BINARY_FLOAT and BINARY_DOUBLE Types ......................................................... 4-15 Oracle SYS.ANYTYPE and SYS.ANYDATA Types ................................................................... 4-16 The oracle.jdbc Package ................................................................................................................. 4-19

Interface oracle.jdbc.OracleConnection ................................................................................ 4-20 Interface oracle.jdbc.OracleStatement................................................................................... 4-21 Interface oracle.jdbc.OraclePreparedStatement .................................................................. 4-21 Interface oracle.jdbc.OracleCallableStatement .................................................................... 4-22 Interface oracle.jdbc.OracleResultSet .................................................................................... 4-22 Interface oracle.jdbc.OracleResultSetMetaData................................................................... 4-22 Class oracle.jdbc.OracleTypes................................................................................................ 4-22 Method getJavaSqlConnection............................................................................................... 4-24 DML Returning ..................................................................................................................................... 4-25 Oracle-Specific APIs........................................................................................................................ 4-26 Running DML Returning Statements........................................................................................... 4-26 Example of DML Returning .......................................................................................................... 4-26 Limitations of DML Returning...................................................................................................... 4-27 Accessing PL/SQL Index-by Tables................................................................................................... 4-28 Overview .......................................................................................................................................... 4-28 Binding IN Parameters ................................................................................................................... 4-29 Receiving OUT Parameters............................................................................................................ 4-30 Type Mappings................................................................................................................................ 4-31

5 Features Specific to JDBC Thin

Overview of JDBC Thin Client ............................................................................................................. 5-1 Additional Features Supported ............................................................................................................. 5-1

Support for Applets ........................................................................................................................... 5-2 Default Support for Native XA ........................................................................................................ 5-2 JDBC in Applets ....................................................................................................................................... 5-2 Connecting to the Database Through the Applet.......................................................................... 5-2 Connecting to a Database on a Different Host Than the Web Server ........................................ 5-3

Using the Oracle Connection Manager.................................................................................... 5-4 Using Signed Applets................................................................................................................. 5-5

v

Using Applets with Firewalls........................................................................................................... 5-6 Configuring a Firewall for Applets that use the JDBC Thin Driver .................................... 5-6 Writing a URL to Connect Through a Firewall ...................................................................... 5-7

Packaging Applets ............................................................................................................................. 5-8 Specifying an Applet in an HTML Page ......................................................................................... 5-8

CODE, HEIGHT, and WIDTH .................................................................................................. 5-8 CODEBASE.................................................................................................................................. 5-9 ARCHIVE..................................................................................................................................... 5-9

6 Features Specific to JDBC OCI Driver

OCI Connection Pooling......................................................................................................................... 6-1 Client Result Cache.................................................................................................................................. 6-1

Benefits of Client Result Cache ........................................................................................................ 6-1 Usage Guidelines in JDBC ................................................................................................................ 6-2

RESULT_CACHE_MODE Parameter ...................................................................................... 6-2 Table Annotations....................................................................................................................... 6-2 SQL Hints ..................................................................................................................................... 6-3 Transparent Application Failover ......................................................................................................... 6-4 OCI Native XA .......................................................................................................................................... 6-4 OCI Instant Client.................................................................................................................................... 6-4 Overview of Instant Client................................................................................................................ 6-4 Benefits of Instant Client ................................................................................................................... 6-5 JDBC OCI Instant Client Installation Process ................................................................................ 6-5 Usage of Instant Client ...................................................................................................................... 6-7 Patching Instant Client Shared Libraries ........................................................................................ 6-7 Regeneration of Data Shared Library and ZIP files ...................................................................... 6-8 Database Connection Names for OCI Instant Client .................................................................... 6-8 Environment Variables for OCI Instant Client ........................................................................... 6-10 Instant Client Light (English) ............................................................................................................. 6-11 Globalization Settings..................................................................................................................... 6-12 Operation.......................................................................................................................................... 6-12 Installation........................................................................................................................................ 6-13

7 Server-Side Internal Driver

Overview of the Server-Side Internal Driver ..................................................................................... 7-1 Connecting to the Database.................................................................................................................... 7-1 Session and Transaction Context .......................................................................................................... 7-3 Testing JDBC on the Server ................................................................................................................... 7-4 Loading an Application into the Server............................................................................................... 7-4

Using the Loadjava Utility ................................................................................................................ 7-4 Using the JVM Command-Line........................................................................................................ 7-6

Part III Connection and Security

8 Data Sources and URLs

Data Sources .............................................................................................................................................. 8-1

vi

Overview of Oracle Data Source Support for JNDI ...................................................................... 8-1 Features and Properties of Data Sources ........................................................................................ 8-2 Creating a Data Source Instance and Connecting ......................................................................... 8-5 Creating a Data Source Instance, Registering with JNDI, and Connecting............................... 8-5 Supported Connection Properties ................................................................................................... 8-7 Using Roles for SYS Login ................................................................................................................ 8-7 Configuring Database Remote Login.............................................................................................. 8-7 Bequeath Connection and SYS Logon............................................................................................. 8-8 Properties for Oracle Performance Extensions .............................................................................. 8-9 Database URLs and Database Specifiers............................................................................................. 8-9

9 JDBC Client-Side Security Features

Support for Oracle Advanced Security ................................................................................................ 9-1 Support for Login Authentication ........................................................................................................ 9-3 Support for Strong Authentication....................................................................................................... 9-3 Support for OS Authentication ............................................................................................................. 9-4

Configuration Steps for Linux.......................................................................................................... 9-4 Configuration Steps for Windows ................................................................................................... 9-5 JDBC Code Using OS Authentication ............................................................................................. 9-6 Support for Data Encryption and Integrity......................................................................................... 9-6 JDBC OCI Driver Support for Encryption and Integrity.............................................................. 9-7 JDBC Thin Driver Support for Encryption and Integrity............................................................. 9-8 Setting Encryption and Integrity Parameters in Java ................................................................... 9-9 Support for SSL ..................................................................................................................................... 9-11 Managing Certificates and Wallets............................................................................................... 9-12 Keys and certificates containers .................................................................................................... 9-12 Support for Kerberos ............................................................................................................................ 9-13 Configuring Windows to Use Kerberos....................................................................................... 9-13 Configuring Oracle Database to Use Kerberos ........................................................................... 9-13 Code Example.................................................................................................................................. 9-15 Support for RADIUS ............................................................................................................................ 9-19 Configuring Oracle Database to Use RADIUS ........................................................................... 9-19 Code Example.................................................................................................................................. 9-20 Secure External Password Store ......................................................................................................... 9-21

10 Proxy Authentication

About Proxy Authentication ............................................................................................................... Types of Proxy Connections................................................................................................................ Creating Proxy Connections................................................................................................................ Closing a Proxy Session ....................................................................................................................... Caching Proxy Connections ................................................................................................................ Limitations of Proxy Connections......................................................................................................

10-1 10-2 10-3 10-5 10-5 10-5

Part IV Data Access and Manipulation

vii

11 Accessing and Manipulating Oracle Data

Data Type Mappings ............................................................................................................................ 11-1 Table of Mappings .......................................................................................................................... 11-1 Notes Regarding Mappings........................................................................................................... 11-3

Data Conversion Considerations ....................................................................................................... 11-4 Standard Types Versus Oracle Types .......................................................................................... 11-4 Converting SQL NULL Data ......................................................................................................... 11-5 Testing for NULLs .......................................................................................................................... 11-5

Result Set and Statement Extensions ................................................................................................ 11-5 Comparison of Oracle get and set Methods to Standard JDBC ................................................... 11-6

Standard getObject Method........................................................................................................... 11-6 Oracle getOracleObject Method.................................................................................................... 11-7 Summary of getObject and getOracleObject Return Types ...................................................... 11-8 Other getXXX Methods .................................................................................................................. 11-9

Return Types of getXXX Methods ....................................................................................... 11-10 Special Notes about getXXX Methods ................................................................................ 11-10 Data Types For Returned Objects from getObject and getXXX ............................................. 11-10 The setObject and setOracleObject Methods............................................................................. 11-11 Other setXXX Methods................................................................................................................. 11-11 Input Data Binding ................................................................................................................ 11-12 Method setFixedCHAR for Binding CHAR Data into WHERE Clauses....................... 11-13 Using Result Set Metadata Extensions............................................................................................ 11-14 Using SQL CALL and CALL INTO Statements ............................................................................ 11-15

12 Java Streams in JDBC

Overview of Java Streams.................................................................................................................... 12-1 Streaming LONG or LONG RAW Columns.................................................................................... 12-2

LONG RAW Data Conversions .................................................................................................... 12-2 LONG Data Conversions ............................................................................................................... 12-3 Streaming Example for LONG RAW Data.................................................................................. 12-3 Avoiding Streaming for LONG or LONG RAW ........................................................................ 12-5 Streaming CHAR, VARCHAR, or RAW Columns ......................................................................... 12-6 Streaming LOBs and External Files ................................................................................................... 12-6 Data Streaming and Multiple Columns............................................................................................ 12-7 Closing a Stream.................................................................................................................................... 12-8 Notes and Precautions on Streams..................................................................................................... 12-8 Streaming Data Precautions .......................................................................................................... 12-8 Using Streams to Avoid Limits on setBytes and setString........................................................ 12-9 Streaming and Row Prefetching ................................................................................................. 12-10

13 Working with Oracle Object Types

Mapping Oracle Objects ...................................................................................................................... Using the Default STRUCT Class for Oracle Objects ...................................................................

STRUCT Class Functionality ......................................................................................................... Retrieving STRUCT Objects and Attributes................................................................................ Creating STRUCT Objects..............................................................................................................

13-1 13-2 13-2 13-3 13-4

viii

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download