简体   繁体   中英

BULK INSERT into Few Tables in Java

I add BULK INSERT into the application and my code was like this :

public void insertData(String filename, JTextField txt)
{

    File f = new File(txt.getText());
    String path = f.getAbsolutePath();
    String createString = "BULK INSERT dummy_import FROM ' " + path + "\\" +filename+ ".txt' WITH (FIELDTERMINATOR = ',')";
    String createStringdet = "BULK INSERT dummy_import_det FROM  " + path + "\\" +filename+ ".txt WITH (FIELDTERMINATOR = ',')";
    List <DummyImpor> dummyimpor = new ArrayList<DummyImpor>(); 
    List <DummyImporDet> dummyimpordet = new ArrayList<DummyImporDet>();
    int  jml_kemasan = 0;
    float bm = 0;
    try  
       { 
           // Create and execute an SQL statement that returns some data.  
            String SQL = "BULK INSERT dbo.dummy_import FROM ' " + path + "\\" +filename+ ".txt' WITH (FIELDTERMINATOR = ',') UNION BULK INSERT dbo.dummy_import_det FROM ' " + path + "\\" +filename+ ".txt' WITH (FIELDTERMINATOR = ',')";  
            DBConnection connect = new DBConnection();
            PreparedStatement ps = connect.getCon().prepareStatement(SQL);


            for (DummyImpor d: dummyimpor) {

                ps.setString(1, d.getSk_import());
                ps.setString(2, d.getSk_batch());
                ps.setString(3, d.getNo_pib());
ps.setInt(4, d.getJml_kemasan());


                for (DummyImporDet dt: dummyimpordet){

                    ps.setString(5, dt.getSk_import_det());
                    ps.setString(6, dt.getNo_hs());
ps.setFloat(7, dt.getBm_hs());

                }

                ps.addBatch();

            }
            ps.executeBatch(); // insert remaining records
            ps.close();

       }  
       catch(Exception e)  
       { 
            System.out.println(e.getMessage()); 
            System.exit(0);  
       } 
}

When I run it, but the data isn't imported yet while that code is used to import text file / data. Does anyone know about a solution?

This looks like a job for BCP Utility if you're using MSSQL. It will be far quicker to insert the two datasets into temp tables, then do a SELECT INTO a table with the records you want.

http://msdn.microsoft.com/en-us/library/ms162802.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM