697
Db2 11 for z/OS Application Programming Guide and Reference for Java SC19-4052-06 IBM

Application Programming Guide and Reference for Java - · PDF fileA pplica tion Programming Guide and Reference for Ja va SC19-4052-06 IBM. Notes ... Java support for XML schema r

  • Upload
    lyanh

  • View
    355

  • Download
    16

Embed Size (px)

Citation preview

  • Db2 11 for z/OS

    Application Programming Guide andReference for Java

    SC19-4052-06

    IBM

  • Db2 11 for z/OS

    Application Programming Guide andReference for Java

    SC19-4052-06

    IBM

  • NotesBefore using this information and the product it supports, be sure to read the general information under Notices at theend of this information.

    Subsequent editions of this PDF will not be delivered in IBM Publications Center. Always download the latest edition fromDb2 11 for z/OS Product Documentation.

    March 23, 2018 edition

    This edition applies to Db2 11 for z/OS (product number 5615-DB2), Db2 11 for z/OS Value Unit Edition (productnumber 5697-P43), and to any subsequent releases until otherwise indicated in new editions. Make sure you areusing the correct edition for the level of the product.

    Specific changes are indicated by a vertical bar to the left of a change. A vertical bar to the left of a figure captionindicates that the figure has changed. Editorial changes that have no technical significance are not noted.

    Copyright IBM Corporation 1998, 2017.US Government Users Restricted Rights Use, duplication or disclosure restricted by GSA ADP Schedule Contractwith IBM Corp.

    http://www-01.ibm.com/support/docview.wss?uid=swg27039165

  • Contents

    About this information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ixWho should read this information . . . . . . . . . . . . . . . . . . . . . . . . . . . . ixDb2 Utilities Suite for z/OS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ixTerminology and citations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ixAccessibility features for Db2 11 for z/OS . . . . . . . . . . . . . . . . . . . . . . . . . xHow to send your comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xHow to read syntax diagrams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xi

    Chapter 1. Java application development for IBM data servers . . . . . . . . . . . . 1

    Chapter 2. Supported drivers for JDBC and SQLJ. . . . . . . . . . . . . . . . . . 3JDBC driver and database version compatibility . . . . . . . . . . . . . . . . . . . . . . . 4How to find the IBM Data Server Driver for JDBC and SQLJ version . . . . . . . . . . . . . . . . 5Db2 for z/OS and IBM Data Server Driver for JDBC and SQLJ levels . . . . . . . . . . . . . . . . 6Db2 on Linux, UNIX, and Windows systems and IBM Data Server Driver for JDBC and SQLJ levels . . . . . . 8

    Chapter 3. JDBC application programming . . . . . . . . . . . . . . . . . . . . 11Example of a simple JDBC application . . . . . . . . . . . . . . . . . . . . . . . . . . 11How JDBC applications connect to a data source . . . . . . . . . . . . . . . . . . . . . . . 13

    Connecting to a data source using the DriverManager interface with the IBM Data Server Driver for JDBC andSQLJ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15Connecting to a data source using the DataSource interface . . . . . . . . . . . . . . . . . . 23How to determine which type of IBM Data Server Driver for JDBC and SQLJ connectivity to use . . . . . 25JDBC connection objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26Creating and deploying DataSource objects . . . . . . . . . . . . . . . . . . . . . . . . 27

    Java packages for JDBC support . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28Learning about a data source using DatabaseMetaData methods. . . . . . . . . . . . . . . . . . 29

    DatabaseMetaData methods for identifying the type of data server . . . . . . . . . . . . . . . . 30Variables in JDBC applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31Comments in a JDBC application . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32JDBC interfaces for executing SQL. . . . . . . . . . . . . . . . . . . . . . . . . . . . 32

    Creating and modifying database objects using the Statement.executeUpdate method . . . . . . . . . 33Updating data in tables using the PreparedStatement.executeUpdate method . . . . . . . . . . . . 34JDBC executeUpdate methods against a Db2 for z/OS server. . . . . . . . . . . . . . . . . . 36Making batch updates in JDBC applications . . . . . . . . . . . . . . . . . . . . . . . 36Learning about parameters in a PreparedStatement using ParameterMetaData methods . . . . . . . . . 40Data retrieval in JDBC applications . . . . . . . . . . . . . . . . . . . . . . . . . . 41Calling stored procedures in JDBC applications . . . . . . . . . . . . . . . . . . . . . . 56LOBs in JDBC applications with the IBM Data Server Driver for JDBC and SQLJ . . . . . . . . . . . 61ROWIDs in JDBC with the IBM Data Server Driver for JDBC and SQLJ . . . . . . . . . . . . . . 67Update and retrieval of timestamps with time zone information in JDBC applications . . . . . . . . . 68Distinct types in JDBC applications . . . . . . . . . . . . . . . . . . . . . . . . . . 77Invocation of stored procedures with ARRAY parameters in JDBC applications . . . . . . . . . . . . 78Savepoints in JDBC applications . . . . . . . . . . . . . . . . . . . . . . . . . . . 79Retrieval of automatically generated keys in JDBC applications . . . . . . . . . . . . . . . . . 80Named parameter markers in JDBC applications . . . . . . . . . . . . . . . . . . . . . . 84Retrieving JSON documents from a ResultSet . . . . . . . . . . . . . . . . . . . . . . . 87Providing extended client information to the data source with IBM Data Server Driver for JDBC and SQLJ-onlymethods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90Providing extended client information to the data source with client info properties . . . . . . . . . . 91

    Extended parameter information with the IBM Data Server Driver for JDBC and SQLJ . . . . . . . . . . 95Using DB2PreparedStatement methods or constants to provide extended parameter information . . . . . . 96Using DB2ResultSet methods or DB2PreparedStatement constants to provide extended parameter information 97

    XML data in JDBC applications. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99XML column updates in JDBC applications . . . . . . . . . . . . . . . . . . . . . . . . 99

    Copyright IBM Corp. 1998, 2017 iii

    ||

    ||

  • XML data retrieval in JDBC applications . . . . . . . . . . . . . . . . . . . . . . . . 102Invocation of routines with XML parameters in Java applications . . . . . . . . . . . . . . . . 105Binary XML format in Java applications . . . . . . . . . . . . . . . . . . . . . . . . 107Java support for XML schema registration and removal . . . . . . . . . . . . . . . . . . . 108

    Bidirectional (BiDi) text layout transformation . . . . . . . . . . . . . . . . . . . . . . . 110Inserting data from file reference variables into tables in JDBC applications. . . . . . . . . . . . . . 112Transaction control in JDBC applications . . . . . . . . . . . . . . . . . . . . . . . . . 114

    IBM Data Server Driver for JDBC and SQLJ isolation levels . . . . . . . . . . . . . . . . . . 114Committing or rolling back JDBC transactions . . . . . . . . . . . . . . . . . . . . . . 115Default JDBC autocommit modes . . . . . . . . . . . . . . . . . . . . . . . . . . . 116

    Exceptions and warnings under the IBM Data Server Driver for JDBC and SQLJ . . . . . . . . . . . . 116Handling an SQLException under the IBM Data Server Driver for JDBC and SQLJ . . . . . . . . . . 119Handling an SQLWarning under the IBM Data Server Driver for JDBC and SQLJ. . . . . . . . . . . 122Retrieving information from a BatchUpdateException . . . . . . . . . . . . . . . . . . . . 123

    Memory use for IBM Data Server Driver for IBM Data Server Driver for JDBC and SQLJ type 2 connectivity onDb2 for z/OS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125Disconnecting from data sources in JDBC applications. . . . . . . . . . . . . . . . . . . . . 126

    Chapter 4. SQLJ application programming . . . . . . . . . . . . . . . . . . . . 127Example of a simple SQLJ application . . . . . . . . . . . . . . . . . . . . . . . . . . 127Connecting to a data source using SQLJ . . . . . . . . . . . . . . . . . . . . . . . . . 129

    SQLJ connection technique 1: JDBC DriverManager interface . . . . . . . . . . . . . . . . . 129SQLJ connection technique 2: JDBC DriverManager interface . . . . . . . . . . . . . . . . . 131SQLJ connection technique 3: JDBC DataSource interface . . . . . . . . . . . . . . . . . . . 132SQLJ connection technique 4: JDBC DataSource interface . . . . . . . . . . . . . . . . . . . 134SQLJ connection technique 5: Use a previously created connection context . . . . . . . . . . . . . 135SQLJ connection technique 6: Use the default connection . . . . . . . . . . . . . . . . . . . 136

    Java packages for SQLJ support . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136Variables in SQLJ applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137Indicator variables in SQLJ applications . . . . . . . . . . . . . . . . . . . . . . . . . 138Comments in an SQLJ application . . . . . . . . . . . . . . . . . . . . . . . . . . . 142SQL statement execution in SQLJ applications . . . . . . . . . . . . . . . . . . . . . . . 142

    Creating and modifying database objects in an SQLJ application . . . . . . . . . . . . . . . . 143Performing positioned UPDATE and DELETE operations in an SQLJ application . . . . . . . . . . . 143Data retrieval in SQLJ applications . . . . . . . . . . . . . . . . . . . . . . . . . . 153Calling stored procedures in SQLJ applications . . . . . . . . . . . . . . . . . . . . . . 164LOBs in SQLJ applications with the IBM Data Server Driver for JDBC and SQLJ . . . . . . . . . . . 166SQLJ and JDBC in the same application . . . . . . . . . . . . . . . . . . . . . . . . 168Controlling the execution of SQL statements