sue 发表于 2013-1-13 18:52:53

如何用JAVA调用DB2的export

在DB2 V8.17中,如何用JAVA调用DB2的export

安哥网络 发表于 2016-12-25 13:18:11

需要用代码备份db2的表,java不能直接执行,google发现要用ADMIN_CMD这个存储过程执行export,db2v8.1要打到补丁9以上才会有 ADMIN_CMD,高版本不用打补丁.db2v8.1不打补丁会报(COM.ibm.db2.jdbc.DB2Exception: SQL0444N例程 "ADMIN_CMD"(特定名称 "SQL100224161758450")是用库或路径 "\SYSPROC.ADMIN_CMD" 中的代码以及不能存取的函数 "SYSPROC.ADMIN_CMD" 来实现的。原因代码:"4"。SQLSTATE=42724)错误

以下是官网例子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 + "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官网例子地址[http://publib.boulder.ibm.com/infocenter/db2luw/v9/index.jsp?topic=/com.ibm.db2.udb.apdv.sample.doc/doc/java_jdbc/s-AdmCmdExport-java.htm]

lmy 发表于 2013-3-2 10:13:14

能否详细说明一下如何实现,谢谢。

lmy 发表于 2013-3-2 10:14:50

能否详细描述,或提供实现例子,谢谢。
页: [1]
查看完整版本: 如何用JAVA调用DB2的export