会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132360个问题
JAVA 全系列/第三阶段:数据库编程/SQL 语言 1516楼

老师批量删除要怎么做呢?我用了同样的方法貌似行不通 哪里出了问题?

//批量删除
	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.


JAVA 全系列/第三阶段:数据库编程/JDBC技术(旧) 1517楼
JAVA 全系列/第三阶段:数据库编程/JDBC技术(旧) 1520楼
JAVA 全系列/第三阶段:数据库编程/MySQL数据库的使用 1524楼
JAVA 全系列/第三阶段:数据库编程/MySQL数据库的使用 1525楼
JAVA 全系列/第三阶段:数据库编程/MySQL数据库的使用 1526楼
JAVA 全系列/第三阶段:数据库编程/Oracle 数据库的使用 1527楼
JAVA 全系列/第三阶段:数据库编程/SQL 语言 1529楼
JAVA 全系列/第三阶段:数据库编程/Oracle 数据库的使用 1530楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637