六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 2045|回复: 3

如何用JAVA调用DB2的export

[复制链接]

升级  8.33%

69

主题

69

主题

69

主题

举人

Rank: 3Rank: 3

积分
225
 楼主| 发表于 2013-1-13 18:52:53 | 显示全部楼层 |阅读模式
在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: [IBM][CLI Driver][DB2/NT] SQL0444N  例程 "ADMIN_CMD"(特定名称 "SQL100224161758450")是用库或路径 "\SYSPROC.ADMIN_CMD" 中的代码以及不能存取的函数 "SYSPROC.ADMIN_CMD" 来实现的。原因代码:"4"。  SQLSTATE=42724)错误

以下是官网例子
  1. import java.io.*;     
  2. import java.lang.*;
  3. import java.util.*;
  4. import java.sql.*;

  5. class AdmCmdExport
  6. {

  7.   public static void main(String argv[])
  8.   {
  9.     Connection con = null;
  10.    
  11.     int rows_exported;
  12.     String msg_retrieval = null;
  13.     String msg_removal = null;
  14.     String sqlcode = null;
  15.     String msg = null;
  16.    
  17.     CallableStatement callStmt1 = null;
  18.     ResultSet rs1 = null;
  19.     PreparedStatement stmt1 = null;
  20.     ResultSet rs2 = null;
  21.     CallableStatement callStmt2 = null;

  22.     if (argv.length < 1)
  23.     {
  24.       System.out.println("\n Usage : java AdmCmdExport <path for export>");
  25.     }
  26.     else
  27.     {            
  28.       try
  29.       {
  30.         // initialize DB2Driver and establish database connection.
  31.         COM.ibm.db2.jdbc.app.DB2Driver db2Driver =
  32.           (COM.ibm.db2.jdbc.app.DB2Driver)
  33.             Class.forName("COM.ibm.db2.jdbc.app.DB2Driver").newInstance();
  34.         con = DriverManager.getConnection("jdbc:db2:SAMPLE");

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

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

  41.         // set the imput parameter
  42.         callStmt1.setString(1, param);
  43.         System.out.println("CALL ADMIN_CMD('" + param + "')");
  44.       
  45.         // execute export by calling ADMIN_CMD
  46.         callStmt1.execute();
  47.         rs1 = callStmt1.getResultSet();
  48.         // retrieve the resultset  
  49.         if( rs1.next())
  50.         {
  51.           // the numbers of rows exported
  52.           rows_exported = rs1.getInt(1);

  53.           // retrieve the select stmt for message retrival
  54.           // containing SYSPROC.ADMIN_GET_MSGS
  55.           msg_retrieval = rs1.getString(2);
  56.   
  57.           // retrive the stmt for message cleanup
  58.           // containing CALL of SYSPROC.ADMIN_REMOVE_MSGS
  59.           msg_removal = rs1.getString(3);
  60.       
  61.           // display the output
  62.           System.out.println("Total number of rows exported  : " + rows_exported);
  63.           System.out.println("SQL for retrieving the messages: " + msg_retrieval);
  64.           System.out.println("SQL for removing the messages  : " + msg_removal);
  65.         }
  66.       
  67.         stmt1 = con.prepareStatement(msg_retrieval);
  68.         System.out.println("\n" + "Executing " + msg_retrieval);  

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

  71.         // retrieve the resultset
  72.         while(rs2.next())
  73.         {
  74.           // retrieve the sqlcode
  75.     sqlcode = rs2.getString(1);
  76.       
  77.           // retrieve the error message
  78.           msg = rs2.getString(2);
  79.           System.out.println("Sqlcode : " +sqlcode);
  80.           System.out.println("Msg     : " +msg);
  81.         }

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

  84.         // executing the message retrivel
  85.         callStmt2.execute();      
  86.       }
  87.       catch(Exception e)
  88.       {
  89.         JdbcException jdbcExc = new JdbcException(e);
  90.         jdbcExc.handle();
  91.       }
  92.       finally
  93.       {
  94.         try
  95.         {
  96.           // close the statements
  97.           callStmt1.close();
  98.           callStmt2.close();
  99.           stmt1.close();

  100.           // close the resultsets
  101.           rs1.close();
  102.           rs2.close();
  103.      
  104.           // roll back any changes to the database made by this sample
  105.           con.rollback();

  106.           // close the connection                                   
  107.           con.close();
  108.         }
  109.         catch (Exception x)
  110.         {
  111.           System.out.print("\n Unable to Rollback/Disconnect ");
  112.           System.out.println("from 'sample' database");
  113.         }
  114.       }
  115.     }
  116.   } // main
  117. } // AdmCmdExport
复制代码
官网例子地址[http://publib.boulder.ibm.com/in ... mCmdExport-java.htm]
该会员没有填写今日想说内容.
回复

使用道具 举报

升级  56%

1

主题

1

主题

1

主题

童生

Rank: 1

积分
28
发表于 2013-3-2 10:13:14 | 显示全部楼层
能否详细说明一下如何实现,谢谢。
该会员没有填写今日想说内容.
回复

使用道具 举报

升级  56%

1

主题

1

主题

1

主题

童生

Rank: 1

积分
28
发表于 2013-3-2 10:14:50 | 显示全部楼层
能否详细描述,或提供实现例子,谢谢。
该会员没有填写今日想说内容.
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表