SQLJ Developer's Guide
Total Page:16
File Type:pdf, Size:1020Kb
Oracle® Database SQLJ Developer's Guide Release 18c E83918-02 May 2018 Oracle Database SQLJ Developer's Guide, Release 18c E83918-02 Copyright © 1999, 2018, Oracle and/or its affiliates. All rights reserved. Primary Author: Tanmay Choudhury Contributing Authors: Tulika Das, Venkatasubramaniam Iyer, Brian Wright, Janice Nygard Contributors: Krishna Mohan, Amit Bande, Sumit Sahu, Amoghavarsha Ramappa, Dhilipkumar Gopal, Quan Wang, Angela Barone, Ekkehard Rohwedder, Brian Becker, Alan Thiesen, Lei Tang, Julie Basu, Pierre Dufour, Jerry Schwarz, Risto Lakinen, Cheuk Chau, Vishu Krishnamurthy, Rafiul Ahad, Jack Melnick, Tim Smith, Thomas Pfaeffle, Tom Portfolio, Ellen Barnes, 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, then 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 about 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 unless otherwise set forth in an applicable agreement between you and Oracle. 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, except as set forth in an applicable agreement between you and Oracle. Contents Preface Audience xv Related Documents xv Documentation Accessibility xvii Conventions xvii 1 Changes in This Release for Oracle SQLJ Developer’s Guide 2 Getting Started Assumptions and Requirements 2-1 Assumptions About Your Environment 2-1 Requirements for Using the Oracle SQLJ Implementation 2-2 SQLJ Environment 2-2 Environment Considerations 2-3 SQLJ Backward Compatibility 2-3 Checking the Installation and Configuration 2-4 Check for Availability of SQLJ and Demo Applications 2-4 Check for Installed Directories and Files 2-4 Set the Path and Classpath 2-4 Verify Installation of the sqljutl Package 2-5 Testing the Setup 2-6 Set Up the Run-Time Connection 2-7 Create a Table to Verify the Database 2-7 Verify the JDBC Driver 2-8 Verify the SQLJ Translator and Run Time 2-8 Verify the SQLJ Translator Connection to the Database 2-9 3 Introduction to SQLJ Overview of SQLJ 3-1 Overview of SQLJ Components 3-2 iii SQLJ Translator Functionality 3-2 SQLJ Run Time 3-3 Overview of Oracle Extensions to the SQLJ Standard 3-3 SQLJ Type Extensions 3-4 SQLJ Functionality Extensions 3-5 Basic Translation Steps and Run-Time Processing 3-6 SQLJ Translation Steps 3-6 Summary of Translator Input and Output 3-8 Translator Input 3-9 Translator Output 3-9 Output File Locations 3-10 SQLJ Run-Time Processing 3-10 SQLJ Sample Code 3-11 SQLJ Version of the Sample Code 3-11 JDBC Version of the Sample Code 3-13 Alternative Deployment Scenarios 3-14 Running SQLJ in Applets 3-14 General Development and Deployment Considerations 3-15 General End User Considerations 3-15 Java Environment and the Java Plug-In 3-15 Alternative Development Scenarios 3-16 SQLJ Globalization Support 3-17 SQLJ in Oracle JDeveloper 10g and Other IDEs 3-17 Windows Considerations 3-17 4 Key Programming Considerations Selection of the JDBC Driver 4-1 Overview of Oracle JDBC Drivers 4-1 Driver Selection for Translation 4-4 Driver Selection and Registration for Run Time 4-4 Connection Considerations 4-5 Single Connection or Multiple Connections Using DefaultContext 4-6 Closing Connections 4-9 Multiple Connections Using Declared Connection Context Classes 4-10 More About the Oracle Class 4-11 More About the DefaultContext Class 4-12 Connection for Translation 4-15 Connection for Customization 4-15 NULL-Handling 4-16 Wrapper Classes for NULL-Handling 4-16 iv Examples of NULL-Handling 4-17 Exception-Handling Basics 4-18 SQLJ and JDBC Exception-Handling Requirements 4-18 Processing Exceptions 4-19 Using SQLException Subclasses 4-20 Basic Transaction Control 4-21 Overview of Transactions 4-21 Automatic Commits Versus Manual Commits 4-22 Specifying Auto-Commit as You Define a Connection 4-22 Modifying Auto-Commit in an Existing Connection 4-23 Using Manual COMMIT and ROLLBACK 4-23 Effect of Commits and Rollbacks on Iterators and Result Sets 4-24 Using Savepoints 4-24 Summary: First Steps in SQLJ Code 4-26 Oracle-Specific Code Generation (No Profiles) 4-31 Environment Requirements for Oracle-Specific Code Generation 4-31 Code Considerations and Limitations with Oracle-Specific Code Generation 4-32 SQLJ Usage Changes with Oracle-Specific Code Generation 4-33 Advantages and Disadvantages of Oracle-Specific Code Generation 4-34 ISO Standard Code Generation 4-35 Environment Requirements for ISO Standard Code Generation 4-35 SQLJ Translator and SQLJ Run Time 4-36 SQLJ Profiles 4-36 Overview of Profiles 4-37 Binary Portability 4-37 SQLJ Translation Steps 4-38 Summary of Translator Input and Output 4-39 Translator Input 4-39 Translator Output 4-39 Output File Locations 4-41 SQLJ Run-Time Processing 4-41 Deployment Scenarios 4-42 Oracle-Specific Code Generation Versus ISO Standard Code Generation 4-44 Requirements and Restrictions for Naming 4-44 Java Namespace: Local Variable and Class Naming Restrictions 4-44 SQLJ Namespace 4-46 SQL Namespace 4-46 File Name Requirements and Restrictions 4-46 Considerations for SQLJ in the Middle Tier 4-47 v 5 Basic Language Features Overview of SQLJ Declarations 5-1 Rules for SQLJ Declarations 5-2 Iterator Declarations 5-3 Connection Context Declarations 5-3 Declaration IMPLEMENTS Clause 5-4 Declaration WITH Clause 5-5 Standard WITH Clause Usage 5-5 Oracle-Specific WITH Clause Usage 5-7 Example: Returnability 5-7 Overview of SQLJ Executable Statements 5-8 Rules for SQLJ Executable Statements 5-9 SQLJ Clauses 5-9 Specifying Connection Context Instances and Execution Context Instances 5-11 Executable Statement Examples 5-11 PL/SQL Blocks in Executable Statements 5-12 Java Host, Context, and Result Expressions 5-13 Overview of Host Expressions 5-14 Basic Host Expression Syntax 5-14 Examples of Host Expressions 5-16 Overview of Result Expressions and Context Expressions 5-18 Evaluation of Java Expressions at Run Time 5-18 Examples of Evaluation of Java Expressions at Run Time (ISO Code Generation) 5-20 Restrictions on Host Expressions 5-25 Single-Row Query Results: SELECT INTO Statements 5-25 SELECT INTO Syntax 5-26 Examples of SELECT INTO Statements 5-26 Examples with Host Expressions in SELECT-List 5-27 SELECT INTO Error Conditions 5-27 Multirow Query Results: SQLJ Iterators 5-28 Iterator Concepts 5-28 Overview of Strongly Typed Iterators 5-29 Overview of Weakly Typed Iterators 5-31 General Steps in Using an Iterator 5-31 Named, Positional, and Result Set Iterators 5-31 Using Named Iterators 5-32 Using Positional Iterators 5-36 Using Iterators and Result Sets as Host Variables 5-39 Using Iterators and Result Sets as Iterator Columns 5-41 Assignment Statements (SET) 5-44 vi Stored Procedure and Function Calls