JDBC: Use batch to process multiple SQL statements (sxt)

Source: Internet
Author: User

JDBC: Use batch to process multiple SQL statements (sxt)

 

Demo

 

  1. Import java. SQL .*;
  2. Public class testbatch
  3. {
  4. /**
  5. * @ Use batch to process multiple SQL statements
  6. */
  7. Public static void main (string [] ARGs)
  8. {
  9. // 1. Do not use preparedstatement
  10. /*
  11. * Connection conn = NULL; statement stmt = NULL; string url =
  12. * "JDBC: mysql: // localhost: 3306/College"; string user = "root"; string
  13. * Password = "123456 ";
  14. *
  15. * Try {class. forname ("com. MySQL. JDBC. Driver"); Conn =
  16. * Drivermanager. getconnection (URL, user, password); stmt =
  17. * Conn. createstatement (); stmt. addbatch ("insert into user
  18. * Values (301, 'go1', '20170', '0') "); stmt. addbatch (" insert into user
  19. * Values (302, 'go2 ', '000000', '00') "); stmt. addbatch (" insert into user
  20. * Values (303, 'go3', '000000', '000 ')");
  21. *
  22. * Stmt.exe cutebatch ();
  23. *} Catch (classnotfoundexception e) {// todo auto-generated catch
  24. * Block E. printstacktrace ();} catch (sqlexception e) {// todo
  25. * Auto-generated Catch Block E. printstacktrace ();} finally {try {
  26. * If (stmt! = NULL) {stmt. Close (); stmt = NULL;} If (Conn! = NULL ){
  27. * Conn. Close (); Conn = NULL;
  28. *}
  29. *} Catch (sqlexception e) {// todo auto-generated Catch Block
  30. * E. printstacktrace ();}}
  31. *}
  32. */
  33. // 2. Use preparedstatement for processing
  34. Connection conn = NULL;
  35. Preparedstatement pstmt = NULL;
  36. String url = "JDBC: mysql: // localhost: 3306/College ";
  37. String user = "root ";
  38. String Password = "123456 ";
  39. Try
  40. {
  41. Class. forname ("com. MySQL. JDBC. Driver ");
  42. Conn = drivermanager. getconnection (URL, user, password );
  43. Pstmt = conn. preparestatement ("insert into user values (?,?,?,?) ");
  44. Pstmt. setint (1,401 );
  45. Pstmt. setstring (2, "goo1 ");
  46. Pstmt. setstring (3, "A1 ");
  47. Pstmt. setstring (4, "K1 ");
  48. Pstmt. addbatch ();
  49. Pstmt. setint (1,402 );
  50. Pstmt. setstring (2, "goo2 ");
  51. Pstmt. setstring (3, "A2 ");
  52. Pstmt. setstring (4, "K2 ");
  53. Pstmt. addbatch ();
  54. Pstmt. setint (1,403 );
  55. Pstmt. setstring (2, "goo3 ");
  56. Pstmt. setstring (3, "A3 ");
  57. Pstmt. setstring (4, "K3 ");
  58. Pstmt. addbatch ();
  59. Pstmt.exe cutebatch ();
  60. } Catch (classnotfoundexception E)
  61. {
  62. // Todo auto-generated Catch Block
  63. E. printstacktrace ();
  64. } Catch (sqlexception E)
  65. {
  66. // Todo auto-generated Catch Block
  67. E. printstacktrace ();
  68. } Finally
  69. {
  70. Try
  71. {
  72. If (pstmt! = NULL)
  73. {
  74. Pstmt. Close ();
  75. Pstmt = NULL;
  76. }
  77. If (Conn! = NULL)
  78. {
  79. Conn. Close ();
  80. Conn = NULL;
  81. }
  82. } Catch (sqlexception E)
  83. {
  84. // Todo auto-generated Catch Block
  85. E. printstacktrace ();
  86. }
  87. }
  88. }
  89. }

 

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.