我有两个表学生(身份证,姓名,城市),老师(身份证,姓名,工资)。 插入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,我可以写一个查询来完成它。
答案 0 :(得分:7)
使用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
相似。
更多信息: