Loading a Db2 for z/OS table by using an IBM Data Server Driver for JDBC and SQLJ method

The DB2Connection.zLoad method lets you load a Db2 for z/OS table from a Java application.

Procedure

To load a Db2 for z/OS from a Java application, follow these steps:

  1. Cast the Connection object for the connection to the data server to a DB2Connection object.
  2. Specify the LOAD statement, the name of the file that contains the input data in variables, and optionally, the utility ID for the load job.

    You can specify the LOAD statement in a variable, or in a file.

    • To specify the LOAD statement in a variable, use a form of the zLoad method without the isLoadstmtInFile variable, or specify a form of the zLoad method with isLoadstmtInFile set to false.
    • To specify the LOAD statement in a file, use a form of the zLoad method with isLoadstmtInFile set to true. Provide the complete path of the file that contains the LOAD statement in the loadstmtOrFile variable.
  3. Issue the DB2Connection.zLoad method.
  4. Retrieve the return code and error messages from the LoadResult object that the DB2Connection.zLoad method returned.

Examples

Example: Use a LOAD statement from a variable to load data from file customer.data into table MYID.CUSTOMER_DATA.

The numbers to the right of statements correspond to the previously described steps.


import java.sql.*;                        // JDBC base
import com.ibm.db2.jcc.*;                 // IBM Data Server Driver for JDBC
                                          // and SQLJ implementation of JDBC
...
DB2Connection db2conn = (DB2Connection)con;                                 1 
String loadstmt = "TEMPLATE SORTIN DSN &JO..&ST..SORTIN.T&TIME. " +         2 
 "UNIT SYSVIO SPACE(10,10) CYL DISP(NEW,DELETE,DELETE) " +
 "TEMPLATE SORTOUT DSN &JO..&ST..SORTOUT.T&TIME. UNIT SYSVIO " +
 "SPACE(10,10) CYL DISP(NEW,DELETE,DELETE) LOAD DATA INDDN "   +
 "SYSCLIEN WORKDDN(SORTIN) REPLACE PREFORMAT LOG(NO) REUSE " +
 "NOCOPYPEND FORMAT DELIMITED UNICODE " +
 "INTO TABLE MYID.CUSTOMER_DATA NUMRECS 30000";
                                          // LOAD statement text is 
                                          // in a string
String dataFilename = "C:\\customer.data";
                                          // Name of the file that contains 
                                          // the input data
String utilid = "ZLOADTEST1";
                                          // Identifier for this run of the
                                          // LOAD utility
LoadResult lr = db2conn.zLoad(loadstmt, dataFilename, utilid);              3 
int returnCode = lr.getReturnCode();                                        4 
String loadMessage = lr.getMessage();

Example: Use a LOAD statement from a file to load data from file customer.data into table MYID.CUSTOMER_DATA.

The numbers to the right of statements correspond to the previously described steps.


import java.sql.*;                        // JDBC base
import com.ibm.db2.jcc.*;                 // IBM Data Server Driver for JDBC
                                          // and SQLJ implementation of JDBC
...
DB2Connection db2conn = (DB2Connection)con;                                 1 
String loadstmtOrFile="C:\\Program Files\\IBM\\SQLLIB\\loadStatement.txt";  2 
                                          // Name of the file that contains 
                                          // the LOAD statement
Boolean isLoadstmtInFile=true;
                                          // Indicate that the LOAD statement
                                          // is in a file
String dataFilename="C:\\Customer.delimiter.large”;
                                          // Name of the file that contains 
                                          // the input data
String utilid="ZLOADTEST2”;
                                          // Identifier for this run of the
                                          // LOAD utility
LoadResult lr =  
  db2conn.zLoad(loadstmtOrFile, isLoadstmtInFile, filename, utilid);        3 
int returnCode = lr.getReturnCode();                                        4 
String loadMessage = lr.getMessage();