批量删除,可作为JDBC DAO层模板代码

该代码展示了如何在DAO层使用JDBC进行批量删除操作,具体针对流向单主表。通过循环构建SQL的占位符,并利用PreparedStatement设置参数,执行删除流向单的SQL语句,如果出现SQLException则抛出DaoException。
摘要由CSDN通过智能技术生成
/**
	 * 删除 流向单主表,支持批量删除
	 * @param flowCardNOs 流向单号
	 * @throws DaoException Dao层异常
	 */
	@Override
	public void delFlowCardMaster(String[] flowCardNOs) throws DaoException {
		
		// 表示 流向单号 的占位符
		StringBuilder flowCardNOsMarks = new StringBuilder();
		
		// 最终的效果 ?,?,?
		for (int i = 0; i < flowCardNOs.length; i++) {
			flowCardNOsMarks.append("?");
			
			if (i < flowCardNOs.length - 1) {
				flowCardNOsMarks.append(",");
			}
		}
		
		// 删除 流向单 的 sql
		/*
		delete from t_flow_card_master
		where Flow_card_no in ('');
		*/
		
		StringBuffer sql = new StringBuffer();
		
		sql.append("delete from t_flow_card_master ")
			.append("where Flow_card_no in (")
			.append(flowCardNOsMarks)
			.append(")");
		
		// JDBC
		PreparedStatement preparedStatement = null;
		
		Connection connection = ConnectionManager.getConnection();
		
以下是使用JDBC调用数据接口的图书管理系统DAO的示例代码: ``` import java.sql.*; import java.util.ArrayList; import java.util.List; public class BookDao { // 数据库连接信息 private final String JDBC_DRIVER = "com.mysql.jdbc.Driver"; private final String DB_URL = "jdbc:mysql://localhost:3306/bookdb"; private final String USER = "root"; private final String PASS = "123456"; // 查询所有图书信息 public List<Book> getAllBooks() { Connection conn = null; Statement stmt = null; ResultSet rs = null; List<Book> bookList = new ArrayList<>(); try { // 注册 JDBC 驱动器 Class.forName(JDBC_DRIVER); // 打开连接 conn = DriverManager.getConnection(DB_URL, USER, PASS); // 执行查询 stmt = conn.createStatement(); String sql = "SELECT * FROM book"; rs = stmt.executeQuery(sql); // 处理结果集 while (rs.next()) { Book book = new Book(); book.setId(rs.getInt("id")); book.setName(rs.getString("name")); book.setAuthor(rs.getString("author")); book.setPublisher(rs.getString("publisher")); book.setPrice(rs.getDouble("price")); book.setNumber(rs.getInt("number")); bookList.add(book); } } catch (SQLException se) { se.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } finally { // 关闭资源 try { if (rs != null) rs.close(); if (stmt != null) stmt.close(); if (conn != null) conn.close(); } catch (SQLException se) { se.printStackTrace(); } } return bookList; } // 添加图书信息 public boolean addBook(Book book) { Connection conn = null; PreparedStatement pstmt = null; boolean result = false; try { // 注册 JDBC 驱动器 Class.forName(JDBC_DRIVER); // 打开连接 conn = DriverManager.getConnection(DB_URL, USER, PASS); // 执行插入 String sql = "INSERT INTO book(name, author, publisher, price, number) VALUES (?, ?, ?, ?, ?)"; pstmt = conn.prepareStatement(sql); pstmt.setString(1, book.getName()); pstmt.setString(2, book.getAuthor()); pstmt.setString(3, book.getPublisher()); pstmt.setDouble(4, book.getPrice()); pstmt.setInt(5, book.getNumber()); int count = pstmt.executeUpdate(); // 处理结果 if (count > 0) { result = true; } } catch (SQLException se) { se.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } finally { // 关闭资源 try { if (pstmt != null) pstmt.close(); if (conn != null) conn.close(); } catch (SQLException se) { se.printStackTrace(); } } return result; } // 更新图书信息 public boolean updateBook(Book book) { Connection conn = null; PreparedStatement pstmt = null; boolean result = false; try { // 注册 JDBC 驱动器 Class.forName(JDBC_DRIVER); // 打开连接 conn = DriverManager.getConnection(DB_URL, USER, PASS); // 执行更新 String sql = "UPDATE book SET name=?, author=?, publisher=?, price=?, number=? WHERE id=?"; pstmt = conn.prepareStatement(sql); pstmt.setString(1, book.getName()); pstmt.setString(2, book.getAuthor()); pstmt.setString(3, book.getPublisher()); pstmt.setDouble(4, book.getPrice()); pstmt.setInt(5, book.getNumber()); pstmt.setInt(6, book.getId()); int count = pstmt.executeUpdate(); // 处理结果 if (count > 0) { result = true; } } catch (SQLException se) { se.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } finally { // 关闭资源 try { if (pstmt != null) pstmt.close(); if (conn != null) conn.close(); } catch (SQLException se) { se.printStackTrace(); } } return result; } // 删除图书信息 public boolean deleteBook(int id) { Connection conn = null; PreparedStatement pstmt = null; boolean result = false; try { // 注册 JDBC 驱动器 Class.forName(JDBC_DRIVER); // 打开连接 conn = DriverManager.getConnection(DB_URL, USER, PASS); // 执行删除 String sql = "DELETE FROM book WHERE id=?"; pstmt = conn.prepareStatement(sql); pstmt.setInt(1, id); int count = pstmt.executeUpdate(); // 处理结果 if (count > 0) { result = true; } } catch (SQLException se) { se.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } finally { // 关闭资源 try { if (pstmt != null) pstmt.close(); if (conn != null) conn.close(); } catch (SQLException se) { se.printStackTrace(); } } return result; } } ``` 这里仅提供了一个简单的示例,实际开发中还需要根据具体情况进行修改和完善。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值