Guest User

Untitled

a guest
Aug 22nd, 2018
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.76 KB | None | 0 0
  1. how to execute bulk insert statement in java (using JDBC) with db=SQL Server 2008 Express
  2. BULK INSERT SalesHistory FROM 'c:SalesHistoryText.txt' WITH (FIELDTERMINATOR = ',')
  3.  
  4. public void insertdata(String filename)
  5. {
  6. String path = System.getProperty("user.dir");
  7. String createString = "BULK INSERT Assignors FROM " + path + "\" +filename+ ".txt WITH (FIELDTERMINATOR = ',')";
  8. try
  9. {
  10. // Load the SQLServerDriver class, build the
  11. // connection string, and get a connection
  12. Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
  13. String connectionUrl = "jdbc:sqlserver://arvind-pc\sqlexpress;" +
  14. "database=test01;" +
  15. "user=sa;" +
  16. "password=password1983";
  17. Connection con = DriverManager.getConnection(connectionUrl);
  18. System.out.println("Connected.");
  19.  
  20. // Create and execute an SQL statement that returns some data.
  21. String SQL = "BULK INSERT dbo.Assignor FROM " + path + "\" +filename+ ".txt WITH (FIELDTERMINATOR = ',')";
  22. Statement stmt = con.createStatement();
  23. ResultSet rs = stmt.executeQuery(SQL);
  24.  
  25. // Iterate through the data in the result set and display it.
  26. while (rs.next())
  27. {
  28. //System.out.println(rs.getString(1) + " " + rs.getString(2));
  29. System.out.println(" Going through data");
  30. }
  31.  
  32. }
  33. catch(Exception e)
  34. {
  35. System.out.println(e.getMessage());
  36. System.exit(0);
  37. }
  38. }
  39.  
  40. String SQL = "BULK INSERT dbo.Assignor FROM '" + path + "\" +filename+ ".txt' WITH (FIELDTERMINATOR = ',')";
Add Comment
Please, Sign In to add comment