1

我有两张表学生(id,name,city),teacher(id,name,salary)。并且有几行需要插入 Mysql DB。

INSERT INTO student VALUES ('12', 'Tom', 'New York');
INSERT INTO student VALUES ('13', 'Jack', 'New York');
INSERT INTO teacher VALUES ('01', 'Joy', '42000');
INSERT INTO teacher VALUES ('02', 'Ryan', '39000');

连接器是 JAVA 中的 JDBC,我可以编写一个查询来完成它。

4

1 回答 1

8

使用PreparedStatement和批量插入:

List<Student> students = ...
Connection con = ...
String insertSql = "INSERT INTO student VALUES (?, ?, ?)";
PreparedStatement pstmt = con.prepareStatement(insertSql);
for (Student student : students) {
    pstmt.setString(1, student.getId()); //not sure if String or int or long
    pstmt.setString(2, student.getName());
    pstmt.setString(3, student.getCity());
    pstmt.addBatch();
}
pstmt.executeBatch();
//close resources...

与您Teacher的 s 类似。

更多信息:

于 2013-09-25T19:17:08.783 回答