Implementation ideas:
The implementation of Java read TXT file content in the memory, the in-memory data and the MySQL database inside a table data field to make a comparison, if the comparison in-memory data in MySQL is not processed, if it does not exist the data into the MySQL database
Step 1, read the text data to a string return value Step 2, query the MySQL table data to a string return value
Step 3, in-memory text data and read MySQL data to do the comparison
/** * Implement read file information * * @param fileName * @return */public static string Readfilebylines (String filename) {string result = ""; file = new file (fileName); String tempstring = null; BufferedReader reader = null;try {reader = new BufferedReader (new FileReader (file)); while (tempstring = Reader.readline ( )) = null) {result + = tempstring + "\ n";} Reader.close ();} catch (IOException e) {e.printstacktrace ();} finally {if (reader! = null) {try {reader.close ();} catch (IOException E1) {} }}return result;}
public static String URL = null; public static String username = null; public static String password = NULL; public static Connection Conn; public static Statement stmt; public static ResultSet RS; public static String FileName; public static String tempstring = null; public static String PATH = "/dbconfig.properties"; private static properties properties; static {try {InputStream is = DBlUtils.class.getResourceAsStream (PATH); Properties = new properties (); Properties.load (IS); url = properties.getproperty ("Jdbc.url"); Username = Properties.getproperty ("Jdbc.username"); Password = properties.getproperty ("Jdbc.password"); filename = properties.getproperty ("filename"); SYSTEM.OUT.PRINTLN ("FileName:" + filename); if (is! = null) is.close (); } catch (IOException e) {e.printstacktrace (); } }
Querying MySQL database data
/** * Query MySQL database data and get content * * @param sql */public static string Querydatas (String sql) {string querydatas = null;try { conn = drivermanager.getconnection (URL, username, password); Conn.setautocommit (false); stmt = Conn.preparestatement (" Load data local infile ' "+" into table loadtest fields terminated by ', ' "); StringBuilder sb = new StringBuilder (); InputStream is = new Bytearrayinputstream (sb.tostring (). GetBytes ());(( com.mysql.jdbc.Statement) stmt). Setlocalinfileinputstream (IS); ResultSet rs = stmt.executequery (SQL); ResultSetMetaData RSMD = Rs.getmetadata (); int columnCount = Rsmd.getcolumncount ();//Output column name for (int i = 1; I <= Columncou nt i++) {if (Rsmd.getcolumnname (i). StartsWith ("content")) {System.out.print (Rsmd.getcolumnname (i));}} System.out.println ();//output MySQL data while (Rs.next ()) {Querydatas = rs.getstring (2);}} catch (Exception e) {e.printstacktrace ();} return Querydatas;}
The data read by the text is compared with the data from the MySQL query:
Incremental import of text data to MySQL