Parsing data into tables

后端 未结 2 1331

I have created a connection with Mysql and java program via jdbc. Now I want to populate the tables in the mysql database. How do I parse the data into the tables from the java

2条回答
  •  粉色の甜心
    2021-01-28 11:50

    An easy solution: read a single line an use the content as a part of an SQL INSERT command:

    List lines = getAllLinesFromFile(file);
    for (String line: lines) {
      String query = "INSERT INTO \"TABLE\" (COL1, ..., COL9) VALUES("+line+");";
      stmt.executeUpdate(query);
    }
    

    Replace TABLE with your actual tablename and COL1, ..., COL9 with an enumeration of your column names. There may be solutions with a better database performance (like using prepared statements) but the algorithm is easy and sufficient to get some data into the database.

提交回复
热议问题