java中通過jacob調用dts進行資料匯入匯出

來源:互聯網
上載者:User
java中通過jacob調用dts進行資料匯入匯出

關鍵字: java jacob dts 匯入匯出

第一步:
下載jacob
http://danadler.com/jacob/jacobBin_17.zip
解壓後將dll檔案放到%JAVA_HOME%/jre/bin目錄下,將jar檔案放到%JAVA_HOME%/jre/lib/ext目錄下,不用再配置環境變數

第二步:
建立dts檔案
直接用sqlserver2000的匯入匯出工具建立一個dts任務,並儲存為儲存結構檔案,放到比如d:/temp/dts/dts_test.dts

第三步:
通過java調用dts檔案執行資料匯入匯出 Java代碼

  1. package dbf;   
  2.   
  3. import java.text.SimpleDateFormat;   
  4. import java.util.Date;   
  5. import com.jacob.activeX.*;   
  6. import com.jacob.com.*;   
  7.   
  8. public class ExportDts {   
  9.     public void exportdbf() {   
  10.   
  11.         SimpleDateFormat localTime  =   new  SimpleDateFormat("yyyy-MM-dd HH:mm:ss");   
  12.            
  13.         String importFileName = "D:/temp/dbf"; //這裡匯出的DBF要存放的路徑   
  14.         String DtsPath = "D:/temp/dts/dts_test.dts";  //這裡是DTS檔案的路徑   
  15.            
  16.         try {   
  17.             System.out.println("start:" + localTime.format(new Date()));   
  18.                
  19.             ActiveXComponent app = new ActiveXComponent("DTS.Package2");   
  20.        
  21.   
  22.             Dispatch.callN(app, "LoadFromStorageFile",new Variant[] {new Variant(DtsPath),new Variant("")});   
  23.                
  24.             //重新設定路徑   
  25.             Dispatch DTSconns = Dispatch.get(app,"Connections").toDispatch();   
  26.             Dispatch DTSconn = Dispatch.callN(DTSconns,"Item",new Variant[]{new Variant(2)}).toDispatch();   
  27.             Dispatch.put(DTSconn,"DataSource",new String(importFileName));   
  28.                
  29.             //執行   
  30.             Dispatch.callN(app, "Execute", new Variant[] {});   
  31.                
  32.             //得到執行的結果   
  33.             Dispatch DTSsteps = Dispatch.get(app,"Steps").toDispatch();   
  34.             Dispatch DTSstep = Dispatch.callN(DTSsteps,"Item",new Variant[]{new Variant(1)}).toDispatch();   
  35.             Variant ResultValue = Dispatch.get(DTSstep,"ExecutionResult");   
  36.                
  37.             if(ResultValue.toInt()==0){   
  38.                 System.out.println("匯出成功");   
  39.        
  40.             }else{   
  41.                 System.out.println("匯出失敗");   
  42.        
  43.             }   
  44.             System.out.println("end:" + localTime.format(new Date()));   
  45.         } catch (Exception ex) {   
  46.            ex.printStackTrace();   
  47.         }   
  48.     }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.