Efficient forwarding of bulk data inserts

Source: Internet
Author: User

  1. Public static void Insert () {
  2. //Open time
  3. Long begin = new Date (). GetTime ();
  4. //SQL prefix
  5. String prefix = "INSERT into Tb_big_data (count, Create_time, Random) VALUES";
  6. try {
  7. //Save SQL suffix
  8. StringBuffer suffix = new StringBuffer ();
  9. //SET transaction as non-auto-commit
  10. Conn.setautocommit (false);
  11. //Statement st = Conn.createstatement ();
  12. //Better than St,pst
  13. PreparedStatement PST = conn.preparestatement ("");
  14. //Outer loop, total number of commit transactions
  15. For (int i = 1; I <= ; i++) {
  16. //Time -to-commit step
  17. For (int j = 1; j <= 10000; j + +) {
  18. //Build SQL suffix
  19. Suffix.append ("(" + j * i + ", sysdate ()," + I * J
  20. * Math.random () + "),");
  21. }
  22. //Build Full SQL
  23. String sql = prefix + suffix.substring (0, Suffix.length ()- 1);
  24. //Add Execute SQL
  25. Pst.addbatch (SQL);
  26. //Perform actions
  27. Pst.executebatch ();
  28. //Commit a transaction
  29. Conn.commit ();
  30. //Clear the last added data
  31. suffix = new StringBuffer ();
  32. }
  33. //First-class connection
  34. Pst.close ();
  35. Conn.close ();
  36. } catch (SQLException e) {
  37. E.printstacktrace ();
  38. }
  39. //End time
  40. Long end = new Date (). GetTime ();
  41. //Time-consuming
  42. System.out.println ("Cast:" + (End-begin)/ + + "MS");
  43. }

Efficient forwarding of bulk data inserts

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.