Parsing data into tables
I h开发者_C百科ave 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 code?
I have two input data files.The format of file is like:
"AAH196","17:13:00","02:49:00",287,166.03,"Austin","TX","Virginia Beach","VA"
"AAH3727","21:38:00","03:04:00",273,176.44,"Los Angeles","CA","Colorado Springs","CO"
You can use the LOAD DATA INFILE
SQL command.
An easy solution: read a single line an use the content as a part of an SQL INSERT command:
List<String> 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.
精彩评论