老师批量删除要怎么做呢?我用了同样的方法貌似行不通 哪里出了问题?
//批量删除
public void deleteBatch(List<Departments> list) {
Connection conn = null;
PreparedStatement ps = null;
try {
conn = JdbcUtil.getConnection();
ps = conn.prepareStatement("delete from departments where department_name=?");
for(int i=0;i<list.size();i++) {
ps.setString(1, list.get(i).getDepartmentName());
//批处理
ps.addBatch();
}
int[] arr =ps.executeBatch();
for (int i = 0; i < arr.length; i++) {
System.out.println(i);
}
} catch (Exception e) {
// TODO: handle exception
}finally {
JdbcUtil.closeResoure(ps, conn, null);
}
}
public static void main(String[] args) {
PreparedStatementDemon psd = new PreparedStatementDemon();
//psd.insertDepartments("人力资源2", 12);
//psd.updateDepartments("科研部", 7, 10);
/*Departments dept = psd.selectDepartmentsId(9);
if (dept !=null) {
System.out.println(dept.getDepartmentId()+" "+dept.getDepartmentName()+" "+ dept.getLocationId());
}
*/
/*List<Departments> list = psd.selectDepartmentsByLike("部");
for (Departments dept : list) {
System.out.println(dept.getDepartmentId()+" "+dept.getDepartmentName()+" "+dept.getLocationId());
}*/
/*List<Departments> list = new ArrayList<>();
for(int i=1;i<=10;i++) {
Departments dept = new Departments();
dept.setDepartmentName("Dota部"+i);
dept.setLocationId(10+i);
list.add(dept);
}
psd.addBatch(list);*/
List<Departments> list = new ArrayList<>();
for(int i=1;i<=10;i++) {
Departments dept = new Departments();
dept.setDepartmentName("Dota部"+i);
list.add(dept);
}
psd.addBatch(list);
}
结果:
Thu Feb 20 18:18:13 CST 2020 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.