Java reads the local TXT file and inserts the database

Source: Internet
Author: User

  1. Package com.cniia.ny.web.control.configManage;


  2. Import Java.io.BufferedReader;
  3. Import Java.io.File;
  4. Import Java.io.FileReader;
  5. Import java.io.IOException;
  6. Import java.sql.Connection;
  7. Import Java.sql.DriverManager;
  8. Import java.sql.PreparedStatement;
  9. Import Java.sql.ResultSet;
  10. Import java.sql.SQLException;
  11. Import java.util.ArrayList;
  12. Import java.util.List;
  13. Import Java.util.Scanner;


  14. public class Insertintodb {
  15. Read Text operations
  16. public static string[] Writetodat (String path) {
  17. File File = new file (path);
  18. list<string> list = new arraylist<string> ();
  19. String []strings = null;
  20. try {
  21. BufferedReader bw = new BufferedReader (new FileReader (file));
  22. String line = null;
  23. Because I do not know a few rows of data, it is first saved in the list collection
  24. while (line = Bw.readline ()) = null) {
  25. List.add (line);
  26. }
  27. Bw.close ();
  28. } catch (IOException e) {
  29. E.printstacktrace ();
  30. }
  31. Determining the length of an array
  32. strings = new String[list.size ()];
  33. for (int i=0;i<list.size (); i++) {
  34. string s = (string) list.get (i);
  35. Strings[i] = s;
  36. }
  37. return strings;
  38. }
  39. Connecting to a database
  40. public static Connection getconnection (String data,string user,string pwd) {

  41. Connection conn = null;
  42. try {
  43. Class.forName ("Com.mysql.jdbc.Driver");
  44. conn = Drivermanager.getconnection ("jdbc:mysql://127.0.0.1:3306/" + Data + "? Characterencoding=utf-8", user, PWD);
  45. } catch (Exception e) {
  46. E.printstacktrace ();
  47. }
  48. Return conn;
  49. }
  50. Insert a database, only for one field
  51. public static Boolean Insertinto (String data,string table,string field,string user,string pwd,string []str) {
  52. try {
  53. Connection conn = getconnection (DATA,USER,PWD);
  54. Conn.setautocommit (FALSE);
  55. String sql = "INSERT into" + Table + "(" + Field + ") VALUES (?);";
  56. PreparedStatement pstmt = conn.preparestatement (sql);
  57. for (int i = 0; i < str.length; i++) {
  58. Pstmt.setstring (1, str[i]);
  59. Pstmt.executeupdate ();
  60. Conn.commit ();
  61. }
  62. Conn.commit ();
  63. return true;
  64. } catch (SQLException e) {
  65. TODO auto-generated Catch block
  66. E.printstacktrace ();
  67. return false;
  68. }

  69. }
  70. public static string[] query (String data,string table,string field,string user,string pwd) {
  71. try {
  72. String []STR = null;
  73. int i = 0;
  74. Connection conn = getconnection (DATA,USER,PWD);
  75. String sql = "Select" + Field + "from" + Table + ";";
  76. PreparedStatement pstmt = conn.preparestatement (sql);
  77. ResultSet rs = Pstmt.executequery ();
  78. while (Rs.next ()) {
  79. Str[i] = rs.getstring (field);
  80. i++;
  81. }
  82. return str;
  83. } catch (SQLException e) {
  84. TODO auto-generated Catch block
  85. E.printstacktrace ();
  86. return null;
  87. }

  88. }
  89. public static void Main (string[] args) {
  90. String []STR2 = null;
  91. String []STR = null;
  92. String []STR3 = null;
  93. Boolean flage1 = true;
  94. Boolean flage2 = true;
  95. Boolean flage3 = true;
  96. System.out.println ("Please enter the absolute path of the target file:");
  97. Scanner sc = new Scanner (system.in);
  98. String path = Sc.next ();
  99. while (Flage3) {
  100. if (Path!=null&&!path.equals ("")) {
  101. Flage3 = false;
  102. str = writetodat (path);
  103. }else {
  104. SYSTEM.OUT.PRINTLN ("Input cannot be empty");
  105. }
  106. }
  107. System.out.println ("Please enter the database name of the desired connection, table name, field name (only one), user name, password," # "separated:");
  108. String stra = Sc.next ();
  109. while (Flage1) {
  110. if (Stra!=null&&!stra.equals ("")) {
  111. Flage1 = false;
  112. while (Flage2) {
  113. if (Stra.indexof ("#") >=0) {
  114. Flage2 = false;
  115. STR2 = Stra.split ("#");
  116. }else {
  117. SYSTEM.OUT.PRINTLN ("You have entered a wrong format, please re-enter");
  118. }
  119. }
  120. }else {
  121. SYSTEM.OUT.PRINTLN ("Input cannot be empty");
  122. }
  123. }
  124. Boolean B = Insertinto (str2[0], str2[1], str2[2], str2[3], str2[4],str);
  125. if (b) {
  126. System.out.println ("Insert succeeded!");
  127. }else {
  128. System.out.println ("Insert failed!");
  129. }
  130. STR3 = Query (Str2[0], str2[1], str2[2], str2[3], str2[4]);
  131. for (int i = 0; i < str3.length; i++) {
  132. System.out.println (Str3[i]);
  133. }
  134. }

  135. }
Copy Code

Java reads the local TXT file and inserts the database

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.