//*************************************************************************** // (c) Copyright IBM Corp. 2007 All rights reserved. // // The following sample of source code ("Sample") is owned by International // Business Machines Corporation or one of its subsidiaries ("IBM") and is // copyrighted and licensed, not sold. You may use, copy, modify, and // distribute the Sample in any form without payment to IBM, for the purpose of // assisting you in the development of your applications. // // The Sample code is provided to you on an "AS IS" basis, without warranty of // any kind. IBM HEREBY EXPRESSLY DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR // IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. Some jurisdictions do // not allow for the exclusion or limitation of implied warranties, so the above // limitations or exclusions may not apply to you. IBM shall not be liable for // any damages you suffer as a result of using, copying, modifying or // distributing the Sample, even if IBM has been advised of the possibility of // such damages. //*************************************************************************** // // SOURCE FILE NAME: SetIntegrity.sqlj // // SAMPLE: How to perform online SET INTEGRITY on a table. // // This sample: // 1. Availability of table during SET INTEGRITY after LOAD utility. // 2. Availability of table during SET INTEGRITY after adding a new // partition is added to the table via the ALTER ATTACH. // 3. Shows how SET INTEGRITY statement will generate the proper // values for both generated columns and identity values whenever // a partition which violates the constraint is attached a data // partitioned table. // // This sample should be run using the following steps: // 1.Compile the program with the following command: // sqlj SetIntegrity.sqlj Util.sqlj // // 2.The sample should be run using the following command // java SetIntegrity <path for dummy file> // The fenced user id must be able to create or overwrite files in // the directory specified.This directory must // be a full path on the server. The dummy file 'dummy.del' must // exist before the sample is run. // // SQL Statements USED: // ALTER TABLE // EXPORT // IMPORT // INSERT // LOAD // SELECT // SET INTEGRITY // // JAVA 2 CLASSES USED: // Statement // ResultSet // // Classes used from Util.java are: // Db // Data // sqljException // // PREQUISITES : 1. Create the pre-requisite tablespaces and tables by running the command: // SetIntegrityScrpt // Alternatively,you can run the command: // db2 -tvf SetIntegrity_setup.db2 // // EXECUTION : 1. Compile the sample using: // bldsqlj SetIntegrity <userid> <password> <server_name> <port_number> <db_name> // 2. Run the sample as: // java SetIntegrity // 3. Perform a clean up once the sample has been executed using: // db2 -tvf SetIntegrity_cleanup.db2 // // // Output will vary depending on the JDBC driver connectivity used. //*************************************************************************** // // For more information on the sample programs, see the README file. // // For information on developing JDBC applications, see the Application // Development Guide. // // For information on using SQL statements, see the SQL Reference. // // For the latest information on programming, compiling, and running DB2 // applications, visit the DB2 Information Center at // http://publib.boulder.ibm.com/infocenter/db2luw/v9r7/index.jsp //************************************************************************** import java.lang.*; import java.sql.*; import sqlj.runtime.*; import sqlj.runtime.ref.*; class SetIntegrity { public static void main(String argv[]) { if (argv.length < 1) { System.out.println("\n Usage : java SetIntegrity" + " <path for dummy file>"); } else { try { String path = argv[0]; Connection con; DefaultContext ctx; // initialize DB2Driver and establish database connection. Class.forName("com.ibm.db2.jcc.DB2Driver").newInstance(); con = DriverManager.getConnection("jdbc:db2:SAMPLE"); ctx = new DefaultContext(con); DefaultContext.setDefaultContext(ctx); System.out.println( "\nTHIS SAMPLE SHOWS HOW TO PERFORM SET INTEGRITY ON A TABLE. \n"); System.out.println("The DMS tablespaces have been created using the \n" + "setup script SetIntegrity_setup.db2 \n"); System.out.println( "****************************************************"+ "\nTHE FOLLOWING SCENARIO SHOWS THE AVAILABILITY OF\n " + " TABLE DURING SET INTEGRITY AFTER LOAD UTILITY\n" + "*****************************************************"); //Shows how SET INTEGRITY can be performed on a partitioned table partitionedTbCreate(con, path); System.out.println( "*****************************************************"+ "\nTHE FOLLOWING SCENARIO SHOWS THE AVAILABILITY OF " + "\n TABLE DURING SET INTEGRITY ALONG WITH GENERATE" + "\n IDENTITY CLAUSE AFTER LAOD\n" + "*****************************************************\n"); createtb_Temp(con, path); createptb_Temp(con, path); System.out.println( "\n*******************************************************"+ "\nTHE FOLLOWING SCENARIO SHOWS THE AVAILABILITY OF " + "\n TABLE DURING SET INTEGRITY AFTER ATTACH via ALTER" + "\n*****************************************************"); // alter a table alterTable(con, path); // disconnect from the 'sample' database con.close(); } catch (Exception e) { SqljException sqljExc = new SqljException(e); sqljExc.handle(); } } } // main // The function shows how SET INTEGRITY can be performed on // a partitioned table. static void partitionedTbCreate(Connection con, String path) throws SQLException { try { System.out.println("\nThe partitioned table 'fact_table1' is created in the\n" + "setup script SetIntegrity_setup.db2 using the command \n" + "\n 'CREATE TABLE fact_table1 (max INTEGER NOT NULL, CONSTRAINT CC CHECK (max>0)) \n" + " PARTITION BY RANGE (max) \n" + " (PART part1 STARTING FROM (-1) ENDING (3) IN tbspace1, \n" + " PART part2 STARTING FROM (4) ENDING (6) IN tbspace2, \n" + " PART part3 STARTING FROM (7) ENDING (9) IN tbspace3)' \n"); System.out.println( "\n-----------------------------------------------------------" + "\nUSE THE SQL STATEMENT:\n" + " INSERT INTO \n" + "TO INSERT DATA IN A TABLE \n" + "\nExecute the statement:\n" + " INSERT INTO fact_table1 VALUES (1), (2), (3)"); // insert data into the table fact_table1 #sql {INSERT INTO fact_table1 VALUES (1), (2), (3)}; #sql {COMMIT}; System.out.println("\nThe temporary table temp_table1 is created in the \n" + "setup script SetIntegrity_setup.db2 using the command \n" + "\n 'CREATE TABLE temp_table1 (max INT)' \n"); System.out.println( "INSERT INTO temp_table1 VALUES(4), (5), (6), (7), (0), (-1)"); // insert data into temp_table1 #sql {INSERT INTO temp_table1 VALUES(4), (5), (6), (7), (0), (-1)}; #sql {COMMIT}; exportData(con, path, "temp_table1"); loadData(con, path, "fact_table1"); System.out.println( "\nThe temporary table 'fact_exception' to hold exceptions thrown \n" + "by SET INTEGRITY statement is created in the setup script \n" + "SetIntegrity_setup.db2 using the command \n" + "\n 'CREATE TABLE fact_exception (max INTEGER NOT NULL)' \n"); System.out.println( "\nUSE THE SQL STATEMENT\n" + " SET INTEGRITY\n" + "TO TABLE OUT OF CHECK PENDING STATE:\n"); System.out.println( "Execute the statement:" + "SET INTEGRITY FOR fact_table1 ALLOW READ ACCESS\n" + " IMMEDIATE CHECKED FOR EXCEPTION IN fact_table1\n" + " USE fact_exception"); #sql {SET INTEGRITY FOR fact_table1 ALLOW READ ACCESS IMMEDIATE CHECKED FOR EXCEPTION IN fact_table1 USE fact_exception}; #sql {COMMIT}; } catch (Exception e) { SqljException sqljExc = new SqljException(e); sqljExc.handle(); } // display the contents of 'fact_table1'. try { int max = 0; System.out.println( "---------------------------------------------------------\n"); System.out.println(" SELECT * FROM fact_table1"); System.out.println( " MAX\n" + " ------"); Statement stmt = con.createStatement(); // perform a SELECT against the "fact_table1" table. ResultSet rs = stmt.executeQuery("SELECT * FROM fact_table1"); // retrieve and display the result from the SELECT statement while (rs.next()) { max = rs.getInt(1); System.out.println( " " + Data.format(max, 3)); } rs.close(); stmt.close(); } catch (Exception e) { SqljException sqljExc = new SqljException(e); sqljExc.handle(); } // display the contents of exception table. try { int max = 0; System.out.println(); System.out.println(" SELECT * FROM fact_exception"); System.out.println( " MAX\n" + " ------"); Statement stmt = con.createStatement(); // perform a SELECT against the "fact_exception" table. ResultSet rs1 = stmt.executeQuery("SELECT * FROM fact_exception"); // retrieve and display the result from the SELECT statement while (rs1.next()) { max = rs1.getInt(1); System.out.println( " " + Data.format(max, 3)); } rs1.close(); stmt.close(); System.out.println( "-----------------------------------------------------------"); } catch (Exception e) { SqljException sqljExc = new SqljException(e); sqljExc.handle(); } } // partitionedTbCreate // exports data to a temporary table. static void exportData(Connection con, String path, String tablename) throws SQLException { try { String sql = ""; String param = ""; CallableStatement callStmt1 = null; ResultSet rs = null; Statement stmt = con.createStatement(); int rows_exported = 0; System.out.println( "\nUSE THE SQL STATEMENT:\n" + " EXPORT \n" + "TO EXPORT TABLE DATA INTO A FILE \n" + "\nExecute the statement:\n" + " EXPORT TO dummy.del OF DEL SELECT * FROM " + tablename); // export data into a dummy file sql = "CALL SYSPROC.ADMIN_CMD(?)"; callStmt1 = con.prepareCall(sql); // 'path' is the path for the file to which the data is to be exported param = "EXPORT TO " + path + "/dummy.del OF DEL SELECT * FROM " + tablename; // set the input parameter callStmt1.setString(1, param); System.out.println(); // execute export by calling ADMIN_CMD callStmt1.execute(); rs = callStmt1.getResultSet(); // retrieve the resultset if (rs.next()) { // the numbers of rows exported rows_exported = rs.getInt(1); // display the output System.out.println ("Total number of rows exported : " + rows_exported); } } catch (Exception e) { SqljException sqljExc = new SqljException(e); sqljExc.handle(); } } // exportData // load data from temporary table into base table. static void loadData(Connection con, String path, String tablename) throws SQLException { try { String sql = ""; String param = ""; CallableStatement callStmt1 = null; ResultSet rs = null; Statement stmt = con.createStatement(); int rows_read = 0; int rows_skipped = 0; int rows_loaded = 0; int rows_rejected = 0; int rows_deleted = 0; int rows_committed = 0; System.out.println( "\nUSE THE SQL STATEMENT:\n" + " LOAD \n" + "TO LOAD THE DATA INTO THE TABLE \n" + "\nExecute the statement:\n" + " LOAD FROM dummy.del OF DEL INSERT INTO " + tablename); // Load data from file sql = "CALL SYSPROC.ADMIN_CMD(?)"; callStmt1 = con.prepareCall(sql); // 'path' is the path of the file from which the data is to be loaded param = "LOAD FROM " + path + "/dummy.del OF DEL INSERT INTO " + tablename; // set the input parameter callStmt1.setString(1, param); // execute import by calling ADMIN_CMD callStmt1.execute(); rs = callStmt1.getResultSet(); // retrieve the resultset if (rs.next()) { // retrieve the no of rows read rows_read = rs.getInt(1); // retrieve the no of rows skipped rows_skipped = rs.getInt(2); // retrieve the no of rows loaded rows_loaded = rs.getInt(3); // retrieve the no of rows rejected rows_rejected = rs.getInt(4); // retrieve the no of rows deleted rows_deleted = rs.getInt(5); // retrieve the no of rows committed rows_committed = rs.getInt(6); // display the resultset System.out.print("\nTotal number of rows read : "); System.out.println(rows_read); System.out.print("Total number of rows skipped : "); System.out.println( rows_skipped); System.out.print("Total number of rows loaded : "); System.out.println(rows_loaded); System.out.print("Total number of rows rejected : "); System.out.println(rows_rejected); System.out.print("Total number of rows deleted : "); System.out.println(rows_deleted); System.out.print("Total number of rows committed : "); System.out.println(rows_read); } } catch (Exception e) { SqljException sqljExc = new SqljException(e); sqljExc.handle(); } } // loadData // displays the contents of a table. static void showData(Connection con, String tablename) throws SQLException { try { int max = 0; int min = 0; System.out.println( "\n-----------------------------------------------------------\n" + "USE THE SQL STATEMENT:\n" + " SELECT\n" + "ON fact_table TABLE\n" + tablename); } catch (Exception e) { SqljException sqljExc = new SqljException(e); sqljExc.handle(); } try { Statement stmt = con.createStatement(); ResultSet rs; int min = 0; int max = 0; System.out.println( "Execute the statement:\n" + "SELECT * FROM " + tablename); System.out.println( " MIN MAX \n" + " ----- ------"); // perform a SELECT against the "fact_table" table in the sample database rs = stmt.executeQuery( "SELECT * FROM " + tablename); // retrieve and display the result from the SELECT statement while (rs.next()) { min = rs.getInt(1); max = rs.getInt(2); System.out.println( " " + Data.format(min, 2) + " " + Data.format(max, 7)); } rs.close(); } catch (Exception e) { SqljException sqljExc = new SqljException(e); sqljExc.handle(); } } // showData // creates a temporary table and inserts data into it. This also shows // SET INTEGRITY operation on 'fact_table3' with FORCE GENERATED clause // to it. static void createptb_Temp(Connection con, String path) throws SQLException { try { System.out.println( "\nA partitioned table 'fact_table3' with GENERATE IDENTITY clause \n" + "and a temporary table 'temp_table3' are created in the setup script\n" + "SetIntegrity_setup.db2 using the commands \n" + "\n 'CREATE TABLE fact_table3 (min SMALLINT NOT NULL, \n" + " max SMALLINT GENERATED ALWAYS AS IDENTITY, \n" + " CONSTRAINT CC CHECK (min>0)) \n" + " PARTITION BY RANGE (min) \n" + " (PART part1 STARTING FROM (1) ENDING (3) IN tbspace1, \n" + " PART part2 STARTING FROM (4) ENDING (6) IN tbspace2, \n" + " PART part3 STARTING FROM (7) ENDING (9) IN tbspace3)' \n" + "\n 'CREATE TABLE temp_table3 (max INTEGER)' \n"); System.out.println( "INSERT INTO temp_table3 VALUES(1), (2), (3), (4), (5), (6), (7), (8), (9)"); //insert data into temp_table3 #sql {INSERT INTO temp_table3 VALUES(1), (2), (3), (4), (5), (6), (7), (8), (9)}; #sql {COMMIT}; exportData(con, path, "temp_table3"); loadData(con, path, "fact_table3"); System.out.println( "\nUSE THE SQL STATEMENT:\n" + " SET INTEGRITY \n" + "To bring the table out of check pending state\n"); System.out.println( "SET INTEGRITY FOR fact_table3 IMMEDIATE CHECKED FORCE GENERATED"); #sql {SET INTEGRITY FOR fact_table3 IMMEDIATE CHECKED FORCE GENERATED}; #sql {COMMIT}; } catch (Exception e) { SqljException sqljExc = new SqljException(e); sqljExc.handle(); } showData(con, "fact_table3"); } // createptb_Temp // Exports data from temporary table 'attach' into 'dummy.del'. Performs LOAD // to load data from 'dummy.del' into temorary table 'attach_part'. Partition // is added to 'fact_table4' and SET INTEGRITY is performed on 'fact_table4' // to bring the table out of check pending state. static void alterTable(Connection con, String path) throws SQLException { System.out.println( "\nA partitioned table 'fact_table4' with GENERATE IDENTITY clause \n" + "and temporary tables 'attach_part' and 'attach' are created \n" + "in the setup script SetIntegrity_setup.db2 using the commands \n" + "\n 'CREATE TABLE fact_table4 (min SMALLINT NOT NULL, \n" + " max SMALLINT GENERATED ALWAYS AS IDENTITY, \n" + " CONSTRAINT CC CHECK (min>0)) \n" + " PARTITION BY RANGE (min) \n" + " (PART part1 STARTING FROM (1) ENDING (3) IN tbspace1, \n" + " PART part2 STARTING FROM (4) ENDING (6) IN tbspace2, \n" + " PART part3 STARTING FROM (7) ENDING (9) IN tbspace3)' \n" + "\n 'CREATE TABLE attach_part (min SMALLINT NOT NULL, \n" + " max SMALLINT GENERATED ALWAYS AS IDENTITY, \n" + " CONSTRAINT CC CHECK (min>0)) IN tbspace1' \n" + "\n 'CREATE TABLE attach(min SMALLINT NOT NULL)' \n"); exportData(con, path, "temp_table3"); loadData(con, path, "fact_table4"); // insert data into attach table #sql {INSERT INTO attach VALUES (10), (11), (12)}; #sql {COMMIT}; try { String sql = ""; String param = ""; CallableStatement callStmt1 = null; ResultSet rs = null; Statement stmt = con.createStatement(); int rows_exported = 0; System.out.println( "\nUSE THE SQL STATEMENT:\n" + " EXPORT \n" + "TO EXPORT TABLE DATA INTO A FILE \n" + "\nExecute the statement:\n" + " EXPORT TO dummy.del OF DEL SELECT * FROM attach"); // export data into a dummy file sql = "CALL SYSPROC.ADMIN_CMD(?)"; callStmt1 = con.prepareCall(sql); // 'path' is the path for the file to which the data is to be exported param = "EXPORT TO " + path + "/dummy.del OF DEL SELECT * FROM attach" ; // set the input parameter callStmt1.setString(1, param); System.out.println(); // execute export by calling ADMIN_CMD callStmt1.execute(); rs = callStmt1.getResultSet(); // retrieve the resultset if (rs.next()) { // the numbers of rows exported rows_exported = rs.getInt(1); // display the output System.out.println ("Total number of rows exported : " + rows_exported); } } catch (Exception e) { SqljException sqljExc = new SqljException(e); sqljExc.handle(); } try { String sql = ""; String param = ""; CallableStatement callStmt1 = null; ResultSet rs = null; Statement stmt = con.createStatement(); int rows_read = 0; int rows_skipped = 0; int rows_loaded = 0; int rows_rejected = 0; int rows_deleted = 0; int rows_committed = 0; System.out.println( "\nUSE THE SQL STATEMENT:\n" + " LOAD \n" + "TO LOAD THE DATA INTO THE TABLE \n" + "\n Execute the statement:\n" + " LOAD FROM dummy.del OF DEL INSERT INTO attach_part"); // Load data from file sql = "CALL SYSPROC.ADMIN_CMD(?)"; callStmt1 = con.prepareCall(sql); // 'path' is the path of the file from which the data is to be loaded param = "LOAD FROM " + path + "/dummy.del OF DEL INSERT INTO attach_part" ; // set the input parameter callStmt1.setString(1, param); // execute import by calling ADMIN_CMD callStmt1.execute(); rs = callStmt1.getResultSet(); // retrieve the resultset if (rs.next()) { // retrieve the no of rows read rows_read = rs.getInt(1); // retrieve the no of rows skipped rows_skipped = rs.getInt(2); // retrieve the no of rows loaded rows_loaded = rs.getInt(3); // retrieve the no of rows rejected rows_rejected = rs.getInt(4); // retrieve the no of rows deleted rows_deleted = rs.getInt(5); // retrieve the no of rows committed rows_committed = rs.getInt(6); // display the resultset System.out.print("\nTotal number of rows read : "); System.out.println(rows_read); System.out.print("Total number of rows skipped : "); System.out.println( rows_skipped); System.out.print("Total number of rows loaded : "); System.out.println(rows_loaded); System.out.print("Total number of rows rejected : "); System.out.println(rows_rejected); System.out.print("Total number of rows deleted : "); System.out.println(rows_deleted); System.out.print("Total number of rows committed : "); System.out.println(rows_read); } } catch (Exception e) { SqljException sqljExc = new SqljException(e); sqljExc.handle(); } System.out.println( "\nUSE THE SQL STATEMENT\n" + " ALTER TABLE\n" + "TO ATTACH PARTITION TO A TABLE\n" + "\nExecute the statement:\n" + " ALTER TABLE fact_table4 ATTACH PARTITION part4\n" + " STARTING FROM (10) ENDING AT (12)\n" + " FROM TABLE attach_part\n"); #sql {ALTER TABLE fact_table4 ATTACH PARTITION part4 STARTING FROM (10) ENDING AT (12) FROM TABLE attach_part}; #sql {COMMIT}; // The following SET INTEGRITY statement will check the table fact_table2 // for constraint violations and at the same time the GENERATE IDENTITY // along with INCREMENTAL options will generate new identity values // for attached rows only. System.out.println( "\nUSE THE SQL STATEMENT\n" + " SET INTEGRITY \n" + "TO BRING TABLE OUT OF CHECK PENDING STATE\n\n" + "Execute the statement:\n" + " SET INTEGRITY FOR fact_table4 GENERATE IDENTITY\n" + " IMMEDIATE CHECKED INCREMENTAL;"); #sql {SET INTEGRITY FOR fact_table4 GENERATE IDENTITY IMMEDIATE CHECKED INCREMENTAL}; showData(con, "fact_table4"); } // alterTable // Inserts data into it temporary table temp_table2. Data is // exported from 'temp_table2' to 'dummy.del' and later loaded into // 'fact_table2'. SET INTEGRITY with GENERATE IDENTITY clause is performed // on 'fact_table2' to generate new identity values for all rows currently // in the table and all loaded rows. static void createtb_Temp(Connection con, String path) throws SQLException { try { System.out.println( "\nA partitioned table 'fact_table2' with GENERATE IDENTITY clause \n" + "and temporary table 'temp_table2' are created in the setup script\n" + "SetIntegrity_setup.db2 using the commands \n" + "\n 'CREATE TABLE fact_table2 (min SMALLINT NOT NULL, \n" + " max SMALLINT GENERATED ALWAYS AS IDENTITY, \n" + " CONSTRAINT CC CHECK (min>0)) \n" + " PARTITION BY RANGE (min) \n" + " (PART part1 STARTING FROM (1) ENDING (3) IN tbspace1, \n" + " PART part2 STARTING FROM (4) ENDING (6) IN tbspace2, \n" + " PART part3 STARTING FROM (7) ENDING (9) IN tbspace3)' \n" + "\n 'CREATE TABLE temp_table2 (min SMALLINT NOT NULL)' \n"); System.out.println( "\nExecute the statements:\n" + " INSERT INTO temp_table2 VALUES (1), (2), (3), (4), (5)\n" + " INSERT INTO temp_table2 VALUES (6), (7), (8), (9)"); // insert data into temp_table2 #sql {INSERT INTO temp_table2 VALUES (1), (2), (3), (4), (5), (6), (7), (8), (9)}; exportData(con, path, "temp_table2"); loadData(con, path, "fact_table2"); // The following SET INTEGRITY statement will check the table fact_table2 for // constraint violations and at the same time the GENERATE IDENTITY along with // NOT INCREMENTAL options will generate new identity values for all rows // currently in the table and all loaded rows System.out.println( "\nUSE THE SQL STATEMENT\n" + " SET INTEGRITY\n" + "TO TABLE OUT OF CHECK PENDING STATE:\n" + "\nExecute the statement:" + "\n SET INTEGRITY FOR fact_table2 GENERATE IDENTITY \n" + " IMMEDIATE CHECKED NOT INCREMENTAL \n"); #sql {SET INTEGRITY FOR fact_table2 GENERATE IDENTITY IMMEDIATE CHECKED NOT INCREMENTAL}; #sql {COMMIT}; showData(con, "fact_table2"); } catch (Exception e) { SqljException sqljExc = new SqljException(e); sqljExc.handle(); } } // createtb_Temp } // SetIntegrity