我在SQL中有以下表格:
+------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+-------+
| PERSON_ID | int(8) | NO | PRI | NULL | |
| LAST_NAME | varchar(256) | NO | | NULL | |
| FIRST_NAME | varchar(256) | NO | | NULL | |
| STREET | varchar(256) | NO | | NULL | |
| CITY | varchar(256) | NO | | NULL | |
+------------+--------------+------+-----+---------+-------+
和此文件:Person.data;
<1>,史密斯,约翰,老街99号,伦敦 2,Rossi,Antonio,P.zza Croce 17,Roma现在我想用文件中的记录填充Person表。
我在google上查了一下,即使是在Stackoverflow上,但我发现了不同的理念,即:
1)您可以将文件直接存储到表中;
2)您需要获取文件中的String并在将它们存储到表后将其拆分为逗号(,);
3)在从文件创建ArrayList并将其存储到表之后;
请你澄清一下吗?
按照我的代码:
public class MysqlCon {
public static void main(String args[]) {
Scanner input;
String line = "";
String cvsSplitBy = ",";
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/Test?useSSL=false", "root",
"db09");
// here sono is database name, root is username and password
java.sql.Statement stmt = con.createStatement();
try {
FileReader fileReader = new FileReader("/Users/Xim/Documents/workspace/DBDemo/src/Person.data");
input = new Scanner(fileReader);
BufferedReader reader = new BufferedReader(fileReader);
while ((line = reader.readLine()) != null) {
System.out.println(line);
// use comma as separator
String[] country = line.split(cvsSplitBy);
System.out.println(country[0]);
}
reader.close();
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
con.close();
} catch (Exception e) {
System.out.println(e);
}
}
}
答案 0 :(得分:1)
我修改了它,现在它正常工作:
while ((line = reader.readLine()) != null) {
String[] tmp = line.split(coma);
person_id = tmp[0];
last_name = tmp[1];
first_name = tmp[2];
street = tmp[3];
city = tmp[4];
System.out.println(person_id + "\t" + last_name + "\t" + first_name + "\t" + street +"\t" + city );
String sql = "INSERT INTO Person (PERSON_ID,LAST_NAME,FIRST_NAME,STREET,CITY) "
+ "values ('" + person_id + "','" + last_name + "','" + first_name + "','" + street + "','" + city +"')";
ps = con.prepareStatement(sql);
ps.executeUpdate();
}
reader.close();
答案 1 :(得分:0)
String sql = "INSERT INTO PERSONS(LAST_NAME, FIRST_NAME, STREET, CITY) "
+ "VALUES(?, ?, ?, ?)";
try (Connection con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/Test?useSSL=false",
"root", "rumpelstielchen");
PreparedStatement stmt = con.prepareStatement(sql)) {
Path file = Paths.get(
"/Users/Xim/Documents/workspace/DBDemo/src/Person.data");
Files.lines(path) // (path, StandardCharsets.UTF_8)
.map(String::split("\\s*,\\s*", 4))
.filter(words -> words.length == 4)
.forEach(words -> {
for (int i = 0; i < 4; ++i) {
stmt.setString(i + 1, words[i]);
}
stmt.executeUpdate();
});
} catch (SQLException | IOException e) {
...
}
这假设您进行PERSON_ID AUTOINCREMENT,因此数据库会填写ID本身(最好)。
它使用PreparedStatement来逃避撇号等。
尝试使用资源是一种丑陋的语法,可以自动关闭连接和预处理语句,即使在异常(或返回)时也是如此。
我已经使用java 8的流读取了该文件,因为它更短。
Split会处理逗号(\\s*
)周围的空格。
这些是导入的基础知识。
在更复杂的情况下,可以使用getGeneratedKeys检索生成的主键PERSON_ID。还有可能的改进(相关提交,字符集很重要,可以批量处理)。