- Package com.cniia.ny.web.control.configManage;
- Import Java.io.BufferedReader;
- Import Java.io.File;
- Import Java.io.FileReader;
- Import java.io.IOException;
- Import java.sql.Connection;
- Import Java.sql.DriverManager;
- Import java.sql.PreparedStatement;
- Import Java.sql.ResultSet;
- Import java.sql.SQLException;
- Import java.util.ArrayList;
- Import java.util.List;
- Import Java.util.Scanner;
- public class Insertintodb {
- Read Text operations
- public static string[] Writetodat (String path) {
- File File = new file (path);
- list<string> list = new arraylist<string> ();
- String []strings = null;
- try {
- BufferedReader bw = new BufferedReader (new FileReader (file));
- String line = null;
- Because I do not know a few rows of data, it is first saved in the list collection
- while (line = Bw.readline ()) = null) {
- List.add (line);
- }
- Bw.close ();
- } catch (IOException e) {
- E.printstacktrace ();
- }
- Determining the length of an array
- strings = new String[list.size ()];
- for (int i=0;i<list.size (); i++) {
- string s = (string) list.get (i);
- Strings[i] = s;
- }
- return strings;
- }
- Connecting to a database
- public static Connection getconnection (String data,string user,string pwd) {
- Connection conn = null;
- try {
- Class.forName ("Com.mysql.jdbc.Driver");
- conn = Drivermanager.getconnection ("jdbc:mysql://127.0.0.1:3306/" + Data + "? Characterencoding=utf-8", user, PWD);
- } catch (Exception e) {
- E.printstacktrace ();
- }
- Return conn;
- }
- Insert a database, only for one field
- public static Boolean Insertinto (String data,string table,string field,string user,string pwd,string []str) {
- try {
- Connection conn = getconnection (DATA,USER,PWD);
- Conn.setautocommit (FALSE);
- String sql = "INSERT into" + Table + "(" + Field + ") VALUES (?);";
- PreparedStatement pstmt = conn.preparestatement (sql);
- for (int i = 0; i < str.length; i++) {
- Pstmt.setstring (1, str[i]);
- Pstmt.executeupdate ();
- Conn.commit ();
- }
- Conn.commit ();
- return true;
- } catch (SQLException e) {
- TODO auto-generated Catch block
- E.printstacktrace ();
- return false;
- }
- }
- public static string[] query (String data,string table,string field,string user,string pwd) {
- try {
- String []STR = null;
- int i = 0;
- Connection conn = getconnection (DATA,USER,PWD);
- String sql = "Select" + Field + "from" + Table + ";";
- PreparedStatement pstmt = conn.preparestatement (sql);
- ResultSet rs = Pstmt.executequery ();
- while (Rs.next ()) {
- Str[i] = rs.getstring (field);
- i++;
- }
- return str;
- } catch (SQLException e) {
- TODO auto-generated Catch block
- E.printstacktrace ();
- return null;
- }
- }
- public static void Main (string[] args) {
- String []STR2 = null;
- String []STR = null;
- String []STR3 = null;
- Boolean flage1 = true;
- Boolean flage2 = true;
- Boolean flage3 = true;
- System.out.println ("Please enter the absolute path of the target file:");
- Scanner sc = new Scanner (system.in);
- String path = Sc.next ();
- while (Flage3) {
- if (Path!=null&&!path.equals ("")) {
- Flage3 = false;
- str = writetodat (path);
- }else {
- SYSTEM.OUT.PRINTLN ("Input cannot be empty");
- }
- }
- System.out.println ("Please enter the database name of the desired connection, table name, field name (only one), user name, password," # "separated:");
- String stra = Sc.next ();
- while (Flage1) {
- if (Stra!=null&&!stra.equals ("")) {
- Flage1 = false;
- while (Flage2) {
- if (Stra.indexof ("#") >=0) {
- Flage2 = false;
- STR2 = Stra.split ("#");
- }else {
- SYSTEM.OUT.PRINTLN ("You have entered a wrong format, please re-enter");
- }
- }
- }else {
- SYSTEM.OUT.PRINTLN ("Input cannot be empty");
- }
- }
- Boolean B = Insertinto (str2[0], str2[1], str2[2], str2[3], str2[4],str);
- if (b) {
- System.out.println ("Insert succeeded!");
- }else {
- System.out.println ("Insert failed!");
- }
- STR3 = Query (Str2[0], str2[1], str2[2], str2[3], str2[4]);
- for (int i = 0; i < str3.length; i++) {
- System.out.println (Str3[i]);
- }
- }
- }
Copy Code
Java reads the local TXT file and inserts the database