In Java, Jacob calls DTS to import and export data.
Keywords: Java Jacob DTS Import and Export
Step 1:
Download Jacob
Http://danadler.com/jacob/jacobBin_17.zip
Decompress the package and put the DLL file in the % java_home %/JRE/bin directory. Put the JAR file in the % java_home %/JRE/lib/EXT directory without configuring environment variables.
Step 2:
Create a data transmission file
Create a data transmission task using the SQL Server Import and Export tool and save it as a storage structure file, for example, D:/temp/DTS/dts_test.dts
Step 3:
Use Java to call the DTs file to execute data import and export Java code
- PackageDBF;
- ImportJava. Text. simpledateformat;
- ImportJava. util. date;
- ImportCom. Jacob. ActiveX .*;
- ImportCom.jacb.com .*;
- Public ClassExportdts {
- Public VoidExportdbf (){
- Simpledateformat localtime =NewSimpledateformat ("yyyy-mm-dd hh: mm: SS ");
- String importfilename = "D:/temp/DBF"; // The Path to store the exported DBF
- String dtspath = "D:/temp/DTS/dts_test.dts"; // the path of the DTs File
- Try{
- System. Out. println ("START:" + localtime. Format (NewDate ()));
- Activexcomponent APP =NewActivexcomponent ("DTS. package2 ");
- Dispatch. calln (app, "loadfromstoragefile ",NewVariant [] {NewVariant (dtspath ),NewVariant ("")});
- // Reset the path
- Dispatch dtsconns = Dispatch. Get (app, "Connections"). todispatch ();
- Dispatch dtsconn = Dispatch. calln (dtsconns, "item ",NewVariant [] {NewVariant (2)}). todispatch ();
- Dispatch. Put (dtsconn, "datasource ",NewString (importfilename ));
- // Execute
- Dispatch. calln (app, "execute ",NewVariant [] {});
- // Obtain the execution result
- Dispatch dtssteps = Dispatch. Get (app, "Steps"). todispatch ();
- Dispatch dtsstep = Dispatch. calln (dtssteps, "item ",NewVariant [] {NewVariant (1)}). todispatch ();
- Variant resultvalue = Dispatch. Get (dtsstep, "executionresult ");
- If(Resultvalue. toint () = 0 ){
- System. Out. println ("exported successfully ");
- }Else{
- System. Out. println ("Export failed ");
- }
- System. Out. println ("end:" + localtime. Format (NewDate ()));
- }Catch(Exception ex ){
- Ex. printstacktrace ();
- }
- }