how to execute bulk insert statement in java (using JDBC) with db=SQL Server 2008 Express

那年仲夏 提交于 2020-01-13 19:21:05

问题


I am trying to execute a BULK INSERT statement on SQL Server 2008 Express. (It basically takes all fields in a specified file and inserts these fields into appropriate columns in a table.)

Given below is an example of the bulk insert statement--

BULK INSERT SalesHistory FROM 'c:\SalesHistoryText.txt' WITH (FIELDTERMINATOR = ',')

Given below is the Java code I am trying to use (but its not working)...Can someone tell me what I am doing wrong here or point me to a java code sample/tutorial that uses the Bulk Insert statement? --

public void insertdata(String filename)
{
    String path = System.getProperty("user.dir");
    String createString = "BULK INSERT Assignors FROM  " + path + "\\" +filename+ ".txt WITH (FIELDTERMINATOR = ',')";   
    try  
       { 
            // Load the SQLServerDriver class, build the 
            // connection string, and get a connection 
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); 
            String connectionUrl = "jdbc:sqlserver://arvind-pc\\sqlexpress;" + 
                                    "database=test01;" + 
                                    "user=sa;" + 
                                    "password=password1983"; 
            Connection con = DriverManager.getConnection(connectionUrl); 
            System.out.println("Connected."); 

            // Create and execute an SQL statement that returns some data.  
            String SQL = "BULK INSERT dbo.Assignor FROM  " + path + "\\" +filename+ ".txt WITH (FIELDTERMINATOR = ',')";  
            Statement stmt = con.createStatement();  
            ResultSet rs = stmt.executeQuery(SQL);

            // Iterate through the data in the result set and display it.  
            while (rs.next())  
            {  
               //System.out.println(rs.getString(1) + " " + rs.getString(2));
                System.out.println(" Going through data");
            }

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

回答1:


I'd guess that your SQL string is missing the single quotes around the filename. Try the following:

        String SQL = "BULK INSERT dbo.Assignor FROM '" + path + "\\" +filename+ ".txt' WITH (FIELDTERMINATOR = ',')";  

EDIT in response to your comment: I wouldn't expect there to be anything in the ResultSet following a bulk insert, in much the same way that I wouldn't expect anything in a ResultSet following an ordinary INSERT statement. These statements just insert the data they are given into a table, they don't return it as well.

If you're not getting any error message, then it looks like your bulk insert is working. If you query the table in SQLCMD or SQL Server Management Studio, do you see the data?

INSERT, UPDATE, DELETE and BULK INSERT statements are not queries, so you shouldn't be using them with the executeQuery() method. executeQuery() is only intended for running SELECT queries. I recommend using the executeUpdate(String) method instead. This method returns an int, which is normally the number of rows inserted/updated/deleted.



来源:https://stackoverflow.com/questions/7539592/how-to-execute-bulk-insert-statement-in-java-using-jdbc-with-db-sql-server-200

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!