Oracle Database JDBC Developer’s Guide and Reference
[Pages:520]Oracle? Database
JDBC Developer's Guide 12c Release 1 (12.1)
E49300-05
June 2014 This book describes how to use Oracle JDBC drivers to develop powerful Java database applications.
Oracle Database JDBC Developer's Guide, 12c Release 1 (12.1)
E49300-05
Copyright ? 1999, 2014, 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: The Oracle Database 12c documentation is dedicated to Mark Townsend, who was an inspiration to all who worked on this release.
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, Swati Rao, Pankaj Chand, Aman Manglik, 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 END USERS: Oracle programs, including any operating system, integrated software, any programs installed on the hardware, and/or documentation, delivered to U.S. Government end users are "commercial computer software" pursuant to the applicable Federal Acquisition Regulation and agency-specific supplemental regulations. As such, use, duplication, disclosure, modification, and adaptation of the programs, including any operating system, integrated software, any programs installed on the hardware, and/or documentation, shall be subject to license terms and license restrictions applicable to the programs. No other rights are granted to the U.S. Government.
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 ................................................................................................................................... xxi Conventions ............................................................................................................................................. xxiii
Changes in This Release for Oracle Database JDBC Developer's Guide.............. xxix
Changes in Oracle Database 12c Release 1 (12.1)................................................................................ xxix
Part I Overview
1 Introducing JDBC
Overview of Oracle JDBC Drivers........................................................................................................ 1-1 Choosing the Appropriate Driver.................................................................................................... 1-3 Feature Differences Between JDBC OCI and Thin Drivers.......................................................... 1-4
Environments and Support .................................................................................................................... 1-4 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 Verifying a JDBC Client Installation ................................................................................................... 2-1
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-4 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 Running a Query and Retrieving a Result Set Object................................................................... 2-9
iii
Processing the Result Set Object ................................................................................................... Closing the Result Set and Statement Objects............................................................................. Making Changes to the Database ................................................................................................. Committing Changes......................................................................................................................
Changing Commit Behavior................................................................................................... Working with Invisible Columns ................................................................................................. Closing the Connection .................................................................................................................. Sample: Connecting, Querying, and Processing the Results........................................................ Stored Procedure Calls in JDBC Programs....................................................................................... PL/SQL Stored Procedures ........................................................................................................... Java Stored Procedures................................................................................................................... Support for Implicit Results .......................................................................................................... Processing SQL Exceptions .................................................................................................................
2-10 2-10 2-11 2-13 2-14 2-14 2-16 2-16 2-17 2-17 2-18 2-18 2-20
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-4 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-5 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-9 The RowId Data Type........................................................................................................................ 3-9 LOB Creation ................................................................................................................................... 3-10 National Language Character Set Support.................................................................................. 3-10
Support for JDBC 4.1 Standard........................................................................................................... 3-11 setClientInfo Method ...................................................................................................................... 3-11 getObject Method ............................................................................................................................ 3-12
iv
4 Oracle Extensions
Overview of Oracle Extensions ............................................................................................................. 4-1 Features of the Oracle Extensions ........................................................................................................ 4-1
Database Management Using JDBC................................................................................................ 4-2 Support for Oracle Data Types......................................................................................................... 4-2 Support for Oracle Objects................................................................................................................ 4-3 Support for Schema Naming ............................................................................................................ 4-4 DML Returning .................................................................................................................................. 4-5 Accessing PL/SQL Associative Arrays........................................................................................... 4-5 Oracle JDBC Packages............................................................................................................................. 4-5 Package oracle.sql .............................................................................................................................. 4-5 Package oracle.jdbc ............................................................................................................................ 4-9 Oracle Character Data Types Support.................................................................................................. 4-9 SQL CHAR Data Types .................................................................................................................. 4-10 SQL NCHAR Data Types............................................................................................................... 4-10 Class oracle.sql.CHAR.................................................................................................................... 4-10 Additional Oracle Type Extensions................................................................................................... 4-12 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-18
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 DML Returning ..................................................................................................................................... 4-24 Oracle-Specific APIs........................................................................................................................ 4-25 Running DML Returning Statements........................................................................................... 4-25 Example of DML Returning .......................................................................................................... 4-25 Limitations of DML Returning...................................................................................................... 4-26 Accessing PL/SQL Associative Arrays .............................................................................................. 4-27 Overview .......................................................................................................................................... 4-27 Binding IN Parameters ................................................................................................................... 4-28 Receiving OUT Parameters............................................................................................................ 4-29 Type Mappings................................................................................................................................ 4-30
5 Features Specific to JDBC Thin
Overview of JDBC Thin Client ............................................................................................................. 5-1 Additional Features Supported ............................................................................................................. 5-1
Default Support for Native XA ........................................................................................................ 5-2 Support for Transaction Guard........................................................................................................ 5-2 Support for Application Continuity ................................................................................................ 5-2
v
Support for Applets ........................................................................................................................... 5-2 JDBC in Applets ....................................................................................................................................... 5-2
Connecting to the Database Through the Applet.......................................................................... 5-3 Connecting to a Database on a Different Host Than the Web Server ........................................ 5-4
Using the Oracle Connection Manager.................................................................................... 5-4 Using Signed Applets................................................................................................................. 5-6 Using Applets with Firewalls........................................................................................................... 5-6 Configuring a Firewall for Applets that use the JDBC Thin Driver .................................... 5-7 Writing a URL to Connect Through a Firewall ...................................................................... 5-7 Packaging Applets ............................................................................................................................. 5-8 Specifying an Applet in an HTML Page ......................................................................................... 5-9 CODE, HEIGHT, and WIDTH .................................................................................................. 5-9 CODEBASE.................................................................................................................................. 5-9 ARCHIVE.................................................................................................................................. 5-10
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
vi
Part III Connection and Security
8 Data Sources and URLs
Data Sources .............................................................................................................................................. 8-1 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-6 Using Roles for SYS Login ................................................................................................................ 8-6 Configuring Database Remote Login.............................................................................................. 8-7 Bequeath Connection and SYS Logon............................................................................................. 8-8 Properties for Oracle Performance Extensions .............................................................................. 8-8
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-4 Support for Strong Authentication....................................................................................................... 9-4 Support for OS Authentication ............................................................................................................. 9-4
Configuration Steps for Linux.......................................................................................................... 9-5 Configuration Steps for Windows ................................................................................................... 9-5 JDBC Code Using OS Authentication ............................................................................................. 9-6 Support for Data Encryption and Integrity......................................................................................... 9-7 JDBC OCI Driver Support for Encryption and Integrity.............................................................. 9-8 JDBC Thin Driver Support for Encryption and Integrity............................................................. 9-9 Setting Encryption and Integrity Parameters in Java ................................................................... 9-9 Support for SSL ..................................................................................................................................... 9-11 Managing Certificates and Wallets............................................................................................... 9-13 Keys and certificates containers .................................................................................................... 9-13 Support for Kerberos ............................................................................................................................ 9-13 Configuring Windows to Use Kerberos....................................................................................... 9-14 Configuring Oracle Database to Use Kerberos ........................................................................... 9-14 Code Example.................................................................................................................................. 9-15 Support for RADIUS ............................................................................................................................ 9-20 Configuring Oracle Database to Use RADIUS ........................................................................... 9-20 Code Example.................................................................................................................................. 9-21 Secure External Password Store ......................................................................................................... 9-22
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
vii
Part IV Data Access and Manipulation
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-10
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-9 Streaming Data Precautions .......................................................................................................... 12-9 Using Streams to Avoid Limits on setBytes and setString...................................................... 12-10 Streaming and Row Prefetching ................................................................................................. 12-10
13 Working with Oracle Object Types
Mapping Oracle Objects ...................................................................................................................... 13-1 Using the Default STRUCT Class for Oracle Objects ................................................................... 13-2
Retrieving STRUCT Objects and Attributes................................................................................ 13-3
viii
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related download
- oracle database jdbc developer s guide and reference
- universe jdbc connections dscallards
- oracle universal connection pool for jdbc developer s guide
- sql tool for oracle
- best practices for speeding and scaling oracle
- database connectivity jdbc
- oracle jdbc for rdb user guide
- java programming with oracle database 19c
- jdbc 4 1 specification oracle
- database connectivity jdbc cal poly
Related searches
- oracle database examples download
- oracle database 11g download free
- oracle database 11g sql pdf
- sample oracle database download
- testing oracle database examples
- oracle database xe
- oracle database 10g express edition download
- oracle database online practice
- oracle database 11g xe express download
- download oracle database 11
- oracle database 19c
- oracle database pdf