怎么把arraylist中的所有对象插入到数据库中
答案:2 悬赏:50 手机版
解决时间 2021-02-16 06:51
- 提问者网友:不爱我么
- 2021-02-15 17:07
public class TeaCourseAdd {
private String teacherid;
private String course[];
public String execute()
{ List teacourselist=new ArrayList();
int i;
for(i=0;i {Teacher teacher=new Teacher();
Course course=new Course();
teacher.setTeacherid(getTeacherid());
course.setCourseid(getCourse()[i]);
TeacourseId teacourseid=new TeacourseId();
teacourseid.setCourse(course);
teacourseid.setTeacher(teacher);
Teacourse teacourse=new Teacourse();
teacourse.setId(teacourseid);
teacourselist.add(teacourse);
}
for(i=0;i {
TeaCourseOp teacourseop=new TeaCourseOp();
teacourseop.addsteacourse(geta);//到这就不会了
}
return "success";
}
最佳答案
- 五星知识达人网友:忘川信使
- 2021-02-15 17:44
为了避免多次进行数据库操作,使用批处理,即如下的代码:
String sql = "insert into employee (name, city, phone) values (?, ?, ?)";
Connection connection = new getConnection();
PreparedStatement ps = connection.prepareStatement(sql);
final int batchSize = 1000;
int count = 0;
for (Employee employee: employees) {
ps.setString(1, employee.getName());
ps.setString(2, employee.getCity());
ps.setString(3, employee.getPhone());
ps.addBatch();
if(++count % batchSize == 0) {
ps.executeBatch();
}
}
ps.executeBatch(); // insert remaining records
ps.close();
connection.close();
全部回答
虽然我很聪明,但这么说真的难到我了
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯