Incremental import of text data to MySQL

Source: Internet
Author: User

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

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.