星期五, 六月 01, 2007

java调用db2管理API——export,Import

 
import java.io.*;    
import java.lang.*;
import java.util.*;
import java.sql.*;

class AdmCmdExport
{

  public static void main(String argv[])
  {
    Connection con = null;
   
    int rows_exported;
    String msg_retrieval = null;
    String msg_removal = null;
    String sqlcode = null;
    String msg = null;
   
    CallableStatement callStmt1 = null;
    ResultSet rs1 = null;
    PreparedStatement stmt1 = null;
    ResultSet rs2 = null;
    CallableStatement callStmt2 = null;

    if (argv.length < 1)
    {
      System.out.println("\n Usage : java AdmCmdExport <path for export>");
    }
    else
    {            
      try
      {
        // initialize DB2Driver and establish database connection.
        COM.ibm.db2.jdbc.app.DB2Driver db2Driver =
          (COM.ibm.db2.jdbc.app.DB2Driver )
            Class.forName("COM.ibm.db2.jdbc.app.DB2Driver").newInstance();
        con = DriverManager.getConnection("jdbc:db2:SAMPLE");

        System.out.println("HOW TO PERFORM EXPORT USING ADMIN_CMD.\n");
        // prepare the CALL statement for OUT_LANGUAGE
        String sql = "CALL SYSPROC.ADMIN_CMD(?)";
        callStmt1 = con.prepareCall(sql);

        String param = "export to "+ argv[0] + "org_ex.ixf ";
        param = param + "of ixf messages on server select * from org" ;

        // set the imput parameter
        callStmt1.setString(1, param);
        System.out.println("CALL ADMIN_CMD('" + param + "')");
      
        // execute export by calling ADMIN_CMD
        callStmt1.execute();
        rs1 = callStmt1.getResultSet();
        // retrieve the resultset 
        if( rs1.next())
        {
          // the numbers of rows exported
          rows_exported = rs1.getInt(1);

          // retrieve the select stmt for message retrival
          // containing SYSPROC.ADMIN_GET_MSGS
          msg_retrieval = rs1.getString(2);
 
          // retrive the stmt for message cleanup
          // containing CALL of SYSPROC.ADMIN_REMOVE_MSGS
          msg_removal = rs1.getString(3);
     
          // display the output
          System.out.println("Total number of rows exported  : " + rows_exported);
          System.out.println("SQL for retrieving the messages: " + msg_retrieval);
          System.out.println("SQL for removing the messages  : " + msg_removal);
        }
     
        stmt1 = con.prepareStatement(msg_retrieval);
        System.out.println("\n" + "Executing " + msg_retrieval); 

        // message retrivel
        rs2 = stmt1.executeQuery();

        // retrieve the resultset
        while(rs2.next())
        {
          // retrieve the sqlcode
    sqlcode = rs2.getString(1);
     
          // retrieve the error message
          msg = rs2.getString(2);
          System.out.println ("Sqlcode : " +sqlcode);
          System.out.println("Msg     : " +msg);
        }

        System.out.println("\nExecuting " + msg_removal);
        callStmt2 = con.prepareCall (msg_removal);

        // executing the message retrivel
        callStmt2.execute();     
      }
      catch(Exception e)
      {
        JdbcException jdbcExc = new JdbcException(e);
        jdbcExc.handle ();
      }
      finally
      {
        try
        {
          // close the statements
          callStmt1.close();
          callStmt2.close();
          stmt1.close();

          // close the resultsets
          rs1.close();
          rs2.close();
    
          // roll back any changes to the database made by this sample
          con.rollback();

          // close the connection                                  
          con.close();
        }
        catch (Exception x)
        {
          System.out.print("\n Unable to Rollback/Disconnect ");
          System.out.println("from 'sample' database");
        }
      }
    }
  } // main
} // AdmCmdExport
 
 
import java.io.*;     //JDBC classes          
import java.lang.*;
import java.util.*;
import java.sql.*;

class AdmCmdImport
{

  public static void main(String argv[])
  {
    Connection con = null;
    CallableStatement callStmt1 = null;
    CallableStatement callStmt2 = null;
    ResultSet rs1 = null;
    ResultSet rs2 = null;
    PreparedStatement stmt1 = null;
    Statement stmt2 = null;
 
    int rows_read;
    int rows_skipped;
    int rows_loaded;
    int rows_rejected;
    int rows_deleted;
    int rows_committed;

    String msg_retrieval = null;
    String msg_removal = null;
    String sqlcode = null;
    String msg = null;
 
    if (argv.length < 1)
    {
      System.out.println("\n Usage : java AdmCmdImport <path for import>");
    }
    else
    {            
      try
      {
        // Initialize DB2Driver and establish database connection.
        COM.ibm.db2.jdbc.app.DB2Driver db2Driver =
          (COM.ibm.db2.jdbc.app.DB2Driver)
            Class.forName ("COM.ibm.db2.jdbc.app.DB2Driver").newInstance();
        con = DriverManager.getConnection("jdbc:db2:SAMPLE");

        System.out.println("HOW TO DO IMPORT USING ADMIN_CMD.\n");
        // prepare the CALL statement for OUT_LANGUAGE
        String sql = "CALL SYSPROC.ADMIN_CMD(?)";
        callStmt1 = con.prepareCall(sql);

        // argv[0] is the path for the file to be imported
        String param = "IMPORT FROM " + argv[0] + "org_ex.ixf OF IXF MESSAGES ";
        param = param + "ON SERVER CREATE INTO ORG_IMPORT" ;

        // setting the imput parameter
        callStmt1.setString(1, param);
        System.out.println("CALL ADMIN_CMD('" + param + "')");
      
        // executing import by calling ADMIN_CMD
        callStmt1.execute ();
        rs1 = callStmt1.getResultSet();
     
        // retrieving the resultset 
        if( rs1.next())
        {
          // retrieve the no of rows read
          rows_read = rs1.getInt(1);
          // retrieve the no of rows skipped
          rows_skipped = rs1.getInt(2);
          // retrieve the no of rows loaded
          rows_loaded = rs1.getInt(3);
          // retrieve the no of rows rejected
          rows_rejected = rs1.getInt(4);
          // retrieve the no of rows deleted
          rows_deleted = rs1.getInt(5);
          // retrieve the no of rows committed
          rows_committed = rs1.getInt (6);

          // retrieve the select stmt for message retrival
          // containing SYSPROC.ADMIN_GET_MSGS
          msg_retrieval = rs1.getString(7);
 
          // retrive the stmt for message cleanup
          // containing CALL of SYSPROC.ADMIN_REMOVE_MSGS
          msg_removal = rs1.getString(8);
     
          // Displaying 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);
          System.out.print("SQL for retrieving the messages: ");
          System.out.println(msg_retrieval);
          System.out.print("SQL for removing the messages  : ");
          System.out.println(msg_removal);
        }
     
        stmt1 = con.prepareStatement (msg_retrieval);
        System.out.println("\n" + "Executing " + msg_retrieval); 

        // message retrivel
        rs2 = stmt1.executeQuery();

        // retrieving the resultset
        while(rs2.next())
        {
          // retrieving the sqlcode
    sqlcode = rs2.getString(1);
     
          //retrieving the error message
          msg = rs2.getString(2);

          System.out.println("Sqlcode : " +sqlcode);
          System.out.println("Msg     : " +msg);
        }

        System.out.println("\n Executing " + msg_removal);
        callStmt2 = con.prepareCall(msg_removal);

        // executing the message retrivel
        callStmt2.execute();     

        System.out.println("\n Executing DROP TABLE ORG_IMPORT");  
        stmt2 = con.createStatement();
        stmt2.executeUpdate("DROP TABLE ORG_IMPORT");
      }
      catch(Exception e)
      {
        JdbcException jdbcExc = new JdbcException(e);
        jdbcExc.handle ();
      }
      finally
      {
        try
        {
          //closing the statements and resultset   
          callStmt1.close();
          callStmt2.close();
          stmt1.close();
          stmt2.close();
          rs1.close();
          rs2.close();
    
          // roll back any changes to the database made by this sample
          con.rollback();

          // closing the connection                                  
          con.close();
        }
        catch (Exception x)
        {
          System.out.print(x);
          System.out.print("\n Unable to Rollback/Disconnect ");
          System.out.println ("from 'sample' database");
        }
      }
    }
  } // main
} // AdmCmdImport

2 条评论:

匿名 说...

[B]NZBsRus.com[/B]
Dont Bother With Laggin Downloads Using NZB Files You Can Instantly Search Movies, PC Games, MP3s, Software and Download Them @ Dashing Rates

[URL=http://www.nzbsrus.com][B]NZB Search[/B][/URL]

匿名 说...

Admit to pass the animalistic with two backs casinos? seal of consent on this advanced [url=http://www.realcazinoz.com]casino[/url] lodestar and factitious online casino games like slots, blackjack, roulette, baccarat and more at www.realcazinoz.com .
you can also into our reported [url=http://freecasinogames2010.webs.com]casino[/url] hard up govern at http://freecasinogames2010.webs.com and be legatee to in unadulterated tangled dough !
another lecherous [url=http://www.ttittancasino.com]casino spiele[/url] locality is www.ttittancasino.com , in inquiry german gamblers, prohibit eleemosynary online casino bonus.