java中通过jacob调用dts进行数据导入导出

时间:2024-01-09 11:16:20

在一个项目中需要金蝶软件对接,但是业务服务器和财务服务器相隔很远(中间经过好几台服务器,有内网也有外网),从一个内网向另一个内网中传输时,需要外网辅助,因为不让原始数据受污染,使用了DTS数据同步到另一台服务器,然后再上传至外网。

sqlserver下 调用dts

EXEC master.dbo.xp_cmdshell 'dtsrun  /S /E /N"MyPack" /A"begintime":8=2013-06-01 /A"endtime":8=2013-08-20' 
create table dtsSync(
beginDate datetime,
endDate datetime,
execDate datetime primary key
)
insert into dtsSync values('2013-08-01', '2013-08-23', getdate()); declare @beginDate varchar(10), @endDate varchar(10), @command varchar(255)
select @beginDate=convert(varchar(10), max(endDate) + 1,120) from dtsSync
set @endDate=convert(varchar(10),getdate(),120)
set @command='dtsrun /S /E /N"MyPack" /A"begintime":8=' + @beginDate + ' /A"endtime":8=' + @endDate
print @command
EXEC master.dbo.xp_cmdshell @command
select @endDate=convert(varchar(10),max(dt_signDate),120) from rpt_xiaoshou
insert into dtsSync values(@beginDate, @endDate, getdate())

第一步: 
下载jacob 
http://danadler.com/jacob/jacobBin_17.zip 
解压后将dll文件放到%JAVA_HOME%\jre\bin

和 %JAVA_HOME%\bin(如果不放:java.lang.UnsatisfiedLinkError: no jacob in java.library.path)

目录下,将jar文件放到%JAVA_HOME%\jre\lib\ext目录下(各种报错,还是放到工程中吧),不用再配置环境变量

第二步: 
创建dts文件 
直接用sqlserver2000的导入导出工具创建一个dts任务,并保存为存储结构文件,放到比如d:\temp\dts\dts_test.dts

第三步: 
通过java调用dts文件执行数据导入导出

package dbf;

import java.text.SimpleDateFormat;
import java.util.Date;
import com.jacob.activeX.*;
import com.jacob.com.*; public class ExportDts {
public void exportdbf() { SimpleDateFormat localTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String importFileName = "D:/temp/dbf"; //这里导出的DBF要存放的路径
String DtsPath = "D:/temp/dts/dts_test.dts"; //这里是DTS文件的路径 try {
System.out.println("start:" + localTime.format(new Date())); ActiveXComponent app = new ActiveXComponent("DTS.Package2"); Dispatch.callN(app, "LoadFromStorageFile",new Variant[] {new Variant(DtsPath),new Variant("")}); //重新设置路径
Dispatch DTSconns = Dispatch.get(app,"Connections").toDispatch();
Dispatch DTSconn = Dispatch.callN(DTSconns,"Item",new Variant[]{new Variant(2)}).toDispatch();
Dispatch.put(DTSconn,"DataSource",new String(importFileName)); //执行
Dispatch.callN(app, "Execute", new Variant[] {}); //得到执行的结果
Dispatch DTSsteps = Dispatch.get(app,"Steps").toDispatch();
Dispatch DTSstep = Dispatch.callN(DTSsteps,"Item",new Variant[]{new Variant(1)}).toDispatch();
Variant ResultValue = Dispatch.get(DTSstep,"ExecutionResult"); if(ResultValue.toInt()==0){
System.out.println("导出成功"); }else{
System.out.println("导出失败"); }
System.out.println("end:" + localTime.format(new Date()));
} catch (Exception ex) {
ex.printStackTrace();
}
}
}