JDBC Bulk execution of SQL insert operations

Source: Internet
Author: User

  1. Package com.file;
  2. Import Java.io.BufferedReader;
  3. Import Java.io.FileReader;
  4. Import java.util.ArrayList;
  5. Public class Resolvfile {
  6. public static string readfilecontent (String filepath) {
  7. //1. Reading each row of records, saving to list
  8. Arraylist<string> records = new arraylist<string> ();
  9. try {
  10. BufferedReader br = new BufferedReader (new FileReader (filepath));
  11. String Arecord;
  12. While ((Arecord = Br.readline ()) =null) {
  13. Records.add (Arecord); //save each row of records read to list
  14. }
  15. Br.close (); //Close stream after run
  16. } catch (Exception e) {
  17. E.printstacktrace ();
  18. }
  19. //2. Process each record into an SQL statement or Save as an object (a. Remove the semicolon before and after the field B. stitching into SQL or saving as an object)
  20. arraylist<string> recordlist = new arraylist<string> (); Used to save the generated SQL or object
  21. For (int i = 0;i<records.size (); i++) {
  22. String record = Records.get (i);
  23. string[] Recarray = Minusquotation (Record.split (","));
  24. //Splicing SQL statements or saving as objects
  25. String recordsql = Getrecordsql (Recarray);
  26. if (null!=recordsql) {
  27. Recordlist.add (Recordsql);
  28. }
  29. }
  30. //3. Bulk execution of SQL or saving objects
  31. Batchexecutesql (recordlist);
  32. return null;
  33. }
  34. public static int batchexecutesql (arraylist<string> sqllist) {
  35. System.out.println ("can then execute SQL statements or save objects");
  36. System.out.println ("======== bulk Execute SQL statement ==========");
  37. System.out.println ("Add all statements to statment Stat");
  38. For (int i = 0;i<sqllist.size (); i++) {
  39. String string = Sqllist.get (i);
  40. System.out.println ("by Stat.addbatch (SQL) to add the statement" +i+": '" +string+"'");
  41. }
  42. System.out.println ("Execute all SQL statements by Stat.executebatch ()");
  43. System.out.println ("======== bulk Execute SQL statement end ==========");
  44. //int count = Stat.executebatch ();
  45. //return count;//Returns the number of statements executed
  46. return sqllist.size ();
  47. }
  48. //Generate SQL for each record
  49. public static String Getrecordsql (string[] recarray) {
  50. if (null==recarray) {
  51. return null;
  52. }
  53. String recordsql = "INSERT INTO TableName (sms,no,time) VALUES ('" +recarray[0]+"', '" +recarray[2]+"  ', ' "+recarray[5]+");
  54. return recordsql;
  55. }
  56. /** 
  57. * Remove the quotation marks at the beginning and end of each element in the array
  58. * @param recarray the array to be processed
  59. * @return-processed array
  60. */
  61. public Static string[] Minusquotation (string[] recarray) {
  62. For (int i = 0; i < recarray.length; i++) {
  63. String str = recarray[i];
  64. if (null!=str) {
  65. if (str.indexof ( "\" ") = =0)
  66. str = str.substring (1,str.length ()); Remove the semicolon from the beginning
  67. if (str.lastindexof ("\" ") = = (Str.length ()-1))
  68. str = str.substring (0,str.length ()-1); //Remove the last semicolon
  69. }
  70. Recarray[i] = str;
  71. }
  72. return recarray;
  73. }
  74. public static void Main (string[] args) {
  75. String filepath = "E:\\sxysms\\smstest.txt";
  76. Readfilecontent (filepath);
  77. }
  78. }

JDBC Bulk execution of SQL insert operations

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.