//  Source File Name: Inpsrv.java  1.3
//  
//   Licensed Materials -- Property of IBM 
//  
//   (c) Copyright International Business Machines Corporation, 1998. 
//       All Rights Reserved. 
//  
//   US Government Users Restricted Rights - 
//   Use, duplication or disclosure restricted by 
//   GSA ADP Schedule Contract with IBM Corp. 

//   Sample Program Inpsrv - JDBC Input Stored Procedure 

//   Steps to run the sample: 
//   (1) create and populate the SAMPLE database (db2sampl) 
//   (2) (n)make Inpsrv 
//   (3) (n)make Inpcli 
//   (4) run Inpcli 

//   NOTES: (1) The jdk11_path database manager configuration parameter must 
//              be set 
//          (2) The CLASSPATH and shared library path environment variables 
//              must be set, as for any JDBC application. 
//          (3) Visit http://www.software.ibm.com/data/db2/java 
//              for current DB2 Java information 

//  For more information about this sample, refer to the README file. 

//  For more information on Programming in Java, refer to the  
//  "Programming in Java" section of the Application Development Guide. 

//  For more information on creating stored procedures, refer to the  
//  "Writing Stored Procedures" section of the Application Development Guide. 

//  For more information on building and running Java programs for DB2, 
//  refer to the "Building Java Applets and Applications" section of the  
//  Application Building Guide. 

//  For more information on the SQL language, refer to the SQL Reference. 

//   Class Inpsrv contains one method: 
//   (1) inputStoredProcedure: stored procedure body 

import java.sql.*;              //  JDBC classes 
import COM.ibm.db2.jdbc.app.*;  //  DB2 UDB JDBC classes 
import COM.ibm.db2.app.*;       //  StoredProc and associated classes 


// ///// 
//  Java stored procedure is in this class 
// ///// 
class Inpsrv extends StoredProc
{
  //   stored procedure body  
  public void inputStoredProcedure(String tblName,    /* :rk.1:erk. */     
                                  String presid1,
                                  String presid2,
                                  String presid3) throws Exception
  {
    try
    {
        //  get caller's connection to the database; inherited from StoredProc 
        Connection con = getConnection ();
        con.setAutoCommit(false);

        String tableStmt = "CREATE TABLE ";
        String insertStmt = "INSERT INTO ";
        String insertData = null;
  
        int cntr = 0;
        String tableName = null;
        String[] dataItems = new String[3];
        int numOfData = 0;

        int SqlCode = 0;

        //  Assign the data to local variables  
        tableName = tblName;
        numOfData = 3;
        dataItems[0] = presid1;
        dataItems[1] = presid2;
        dataItems[2] = presid3;

        //  Create President Table  
        //  - For simplicity, we'll ignore any errors from the               
        //    CREATE TABLE so that you can run this program even when the    
        //    table already exists due to a previous run.   
        Statement stmt = con.createStatement ();
        tableStmt = tableStmt + tableName;
        tableStmt = tableStmt + " (name CHAR(20))";   /* :rk.2:erk. */
        try
        {
          stmt.executeUpdate (tableStmt);
        }
        catch (Exception e)
        {
          //  ignore this error 
        }
        stmt.close ();

        //  Insert the three presidents.  
        insertStmt = insertStmt + tableName;
        insertStmt = insertStmt + " VALUES (?)";      /* :rk.3:erk. */
        PreparedStatement insertPresidents = con.prepareStatement(insertStmt);

        for (cntr = 0; cntr < numOfData; cntr++)
        {
          insertData = dataItems[cntr];
          insertPresidents.setString(1, insertData);
          insertPresidents.executeUpdate();             /* :rk.4:erk. */

        }
    } 
    catch (Exception e)
    {
      throw e; 
    }
  }
}