9、图书修改,删除功能

1、添加图书修改,删除的Dao代码

/**
 * 图书Dao类
 * @author Administrator
 *
 */
public class BookDao {

	/**
	 * 图书添加
	 * @param con
	 * @param book
	 * @return
	 * @throws Exception 
	 */
	public int add(Connection con,Book book) throws Exception{
		String sql="insert into t_book values(null,?,?,?,?,?,?)";
		PreparedStatement pstmt=con.prepareStatement(sql);
		pstmt.setString(1, book.getBookName());
		pstmt.setString(2, book.getAuthor());
		pstmt.setString(3, book.getSex());
		pstmt.setFloat(4, book.getPrice());
		pstmt.setInt(5, book.getBookTypeId());
		pstmt.setString(6, book.getBookDesc());
		pstmt.setInt(7, book.getId());
		return pstmt.executeUpdate();
		
	}
	
	/**
	 * 图书信息查询
	 * @param con
	 * @param book
	 * @return
	 * @throws Exception
	 */
	public ResultSet list(Connection con,Book book)throws Exception{
		StringBuffer sb=new StringBuffer("select * from t_book b,t_bookType bt where b.bookTypeId=bt.id");
		if(StringUtil.isNotEmpty(book.getBookName())){
			sb.append(" and b.bookName like '%"+book.getBookName()+"%'");
		}
		if(StringUtil.isNotEmpty(book.getAuthor())){
			sb.append(" and b.author like '%"+book.getAuthor()+"%'");
		}
		if(book.getBookTypeId()!=null && book.getBookTypeId()!=-1){
			sb.append(" and b.bookTypeId="+book.getBookTypeId());
		}
		PreparedStatement pstmt=con.prepareStatement(sb.toString());
		return pstmt.executeQuery();
	}
-----------------------------------------------------------------------------------	
	/**
	 * 图书信息删除
	 * @param con
	 * @param id
	 * @return
	 * @throws Exception
	 */
	public int delete(Connection con,String id)throws Exception{
		String sql="delete  from t_book where id=?";
		PreparedStatement pstmt=con.prepareStatement(sql);
		pstmt.setString(1, id);
		return pstmt.executeUpdate();
	}
	
	/**
	 * 产品信息修改
	 * @param con
	 * @param book
	 * @return
	 * @throws Exception
	 */
	public int update(Connection con,Book book)throws Exception{
		String sql="update t_book set bookName=?,author=?,sex=?,price=?,bookDesc=?,bookTypeId=? where id=? ";
		PreparedStatement pstmt=con.prepareStatement(sql);
		pstmt.setString(1, book.getBookName());
		pstmt.setString(2, book.getAuthor());
		pstmt.setString(3, book.getSex());
		pstmt.setFloat(4, book.getPrice());
		pstmt.setString(5, book.getBookDesc());
		pstmt.setInt(6, book.getBookTypeId());
		return pstmt.executeUpdate();
	}
	----------------------------------------------------------------------------
}

2、在“图书管理-修改”窗体新建图书修改删除组件

在BookManageInterFrm窗体上添加JLabel,JTextField,JComboBox,JTextArea,JButtonJRadioButton组件
在这里插入图片描述
在这里插入图片描述
重命名组件:
在这里插入图片描述

实现bookTable的行点击事件
在这里插入图片描述

@Override
public void mousePressed(MouseEvent e) {
	bookTableMousePressed(e);
}

调用方法:

this.fillBookType("modify");

修改fillBookType方法,为modify时:

/**
	 * 初始化下拉框
	 */
	 private void fillBookType(String type){
		 Connection con=null;
		 BookType bookType=null;
		 try{
			 con=dbUtil.getCon();
			 ResultSet rs=bookTypeDao.list(con, new BookType());
			 if("search".equals(type)){
				 bookType=new BookType();
				 bookType.setBookTypeName("请选择");
				 bookType.setId(-1);
				 this.s_bookTypeJcb.addItem(bookType);
			 }
			 while(rs.next()){
				 bookType=new BookType();
				 bookType.setBookTypeName(rs.getString("bookTypeName"));
				 bookType.setId(rs.getInt("id"));
				 if("search".equals(type)){
					 this.s_bookTypeJcb.addItem(bookType);
				 }else if("modify".equals(type)){
-----------------------------------------------------------------------------------
					 this.bookTypeJcb.addItem(bookType);
------------------------------------------------------------------------------------------
				 }
			 }
		 }catch(Exception e){
			 e.printStackTrace();
		 }finally{
			 try {
				dbUtil.close(con);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		 }
	 }
	 
/**
	 * 表格行点击处理
	 * @param e
	 */
	private void bookTableMousePressed(MouseEvent e) {
		int row=this.bookTable.getSelectedRow();//获取选中的行
		this.idTxt.setText(bookTable.getValueAt(row, 0)+"");
		this.bookNameTxt.setText((String)bookTable.getValueAt( row, 1));
		this.authorTxt.setText((String)bookTable.getValueAt(row, 2));
		String sex=(String)bookTable.getValueAt(row, 3);
		if("男".equals(sex)){
			this.manJrb.setSelected(true);
		}else if("女".equals(sex)){
			this.femaleJrb.setSelected(true);
		}
		this.priceTxt.setText((Float)bookTable.getValueAt(row, 4)+"");
		this.bookDescTxt.setText((String)bookTable.getValueAt(row, 5));
		String bookTypeName=(String)this.bookTable.getValueAt(row, 6);
		//遍历下拉框
		int n=this.bookTypeJcb.getItemCount();
		for(int i=0;i<n;i++){
			BookType item=(BookType)this.bookTypeJcb.getItemAt(i);
			if(item.getBookTypeName().equals(bookTypeName)){
				this.bookTypeJcb.setSelectedIndex(i);
			}
		}
	}

在这里插入图片描述

3、实现修改功能:

修改按钮新建事件

public void actionPerformed(ActionEvent e) {
	bookUpdateActionPerformed(e);
}

封装一个重置表单的方法

/**
	 * 重置表单
	 */
	private void resetValue() {
		this.idTxt.setText("");
		this.bookNameTxt.setText("");
		this.authorTxt.setText("");
		this.priceTxt.setText("");
		this.manJrb.setSelected(true);
		this.bookDescTxt.setText("");
		if(this.bookTypeJcb.getItemCount()>0){
			this.bookTypeJcb.setSelectedIndex(0);
		}
	}
/**
 * 图书修改事件处理
 */
private void bookUpdateActionPerformed(ActionEvent evt) {
	String id=this.idTxt.getText();
	if(StringUtil.isEmpty(id)){
		JOptionPane.showMessageDialog(null, "请选择图书!");
		return;
	}
	String bookName=this.bookNameTxt.getText();
	String author=this.authorTxt.getText();
	String price=this.priceTxt.getText();
	String bookDesc=this.bookDescTxt.getText();
	
	if(StringUtil.isEmpty(bookName)){
		JOptionPane.showMessageDialog(null, "图书名为空!");
		return;
	}
	if(StringUtil.isEmpty(author)){
		JOptionPane.showMessageDialog(null, "作者为空!");
		return;
	}
	if(StringUtil.isEmpty(price)){
		JOptionPane.showMessageDialog(null, "价格为空!");
		return;
	}
	
	String sex="";
	if(manJrb.isSelected()){
		sex="男";
	}else if(femaleJrb.isSelected()){
		sex="女";
	}
	
	//获取下拉框
	BookType bookType=(BookType)bookTypeJcb.getSelectedItem();
	int bookTypeId=bookType.getId();
	
	Book book=new Book(Integer.parseInt(id),bookName,author,sex,Float.parseFloat(price),bookTypeId,bookDesc);
	Connection con=null;
	try{
		con=dbUtil.getCon();
		int addNum=bookDao.update(con, book);
		if(addNum==1){
			JOptionPane.showMessageDialog(null, "修改成功!");
			resetValue();
			this.fillTable(new Book());//刷新表单
		}else{
			JOptionPane.showMessageDialog(null, "修改失败!");
		}
	}catch(Exception e){
		e.printStackTrace();
		JOptionPane.showMessageDialog(null, "修改失败!");
	}finally{
		try {
			dbUtil.close(con);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

4、实现删除功能

删除按钮新建事件

public void actionPerformed(ActionEvent e) {
		bookDeleteActionPerformed(e);
	}
/**
	 * 图书删除事件处理
	 * @param e
	 */
	private void bookDeleteActionPerformed(ActionEvent evt) {
		String id=this.idTxt.getText();
		if(StringUtil.isEmpty(id)){
			JOptionPane.showMessageDialog(null, "请选择图书!");
			return;
		}
		int n=JOptionPane.showConfirmDialog(null, "确认删除");
		if(n==0){
			Connection con=null;
		try{
			con=dbUtil.getCon();
			int deleteNum=bookDao.delete(con, id);
			if(deleteNum==1){
				JOptionPane.showMessageDialog(null, "删除成功!");
				resetValue();
				this.fillTable(new Book());
			}else{
				JOptionPane.showMessageDialog(null, "删除失败!");
			}
		}catch(Exception e){
			e.printStackTrace();
			JOptionPane.showMessageDialog(null, "删除失败!");
		}finally{
			try {
				dbUtil.close(con);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		}
	}

5、删除图书类别时做判断,图书类别下是否存在图书

/**
 * 图书Dao类
 * @author Administrator
 *
 */
public class BookDao {

	/**
	 * 图书添加
	 * @param con
	 * @param book
	 * @return
	 * @throws Exception 
	 */
	public int add(Connection con,Book book) throws Exception{
		String sql="insert into t_book values(null,?,?,?,?,?,?)";
		PreparedStatement pstmt=con.prepareStatement(sql);
		pstmt.setString(1, book.getBookName());
		pstmt.setString(2, book.getAuthor());
		pstmt.setString(3, book.getSex());
		pstmt.setFloat(4, book.getPrice());
		pstmt.setInt(5, book.getBookTypeId());
		pstmt.setString(6, book.getBookDesc());
		return pstmt.executeUpdate();
		
	}
	
	/**
	 * 图书信息查询
	 * @param con
	 * @param book
	 * @return
	 * @throws Exception
	 */
	public ResultSet list(Connection con,Book book)throws Exception{
		StringBuffer sb=new StringBuffer("select * from t_book b,t_bookType bt where b.bookTypeId=bt.id");
		if(StringUtil.isNotEmpty(book.getBookName())){
			sb.append(" and b.bookName like '%"+book.getBookName()+"%'");
		}
		if(StringUtil.isNotEmpty(book.getAuthor())){
			sb.append(" and b.author like '%"+book.getAuthor()+"%'");
		}
		if(book.getBookTypeId()!=null && book.getBookTypeId()!=-1){
			sb.append(" and b.bookTypeId="+book.getBookTypeId());
		}
		PreparedStatement pstmt=con.prepareStatement(sb.toString());
		return pstmt.executeQuery();
	}
	
	/**
	 * 图书信息删除
	 * @param con
	 * @param id
	 * @return
	 * @throws Exception
	 */
	public int delete(Connection con,String id)throws Exception{
		String sql="delete  from t_book where id=?";
		PreparedStatement pstmt=con.prepareStatement(sql);
		pstmt.setString(1, id);
		return pstmt.executeUpdate();
	}
	
	/**
	 * 产品信息修改
	 * @param con
	 * @param book
	 * @return
	 * @throws Exception
	 */
	public int update(Connection con,Book book)throws Exception{
		String sql="update t_book set bookName=?,author=?,sex=?,price=?,bookDesc=?,bookTypeId=? where id=? ";
		PreparedStatement pstmt=con.prepareStatement(sql);
		pstmt.setString(1, book.getBookName());
		pstmt.setString(2, book.getAuthor());
		pstmt.setString(3, book.getSex());
		pstmt.setFloat(4, book.getPrice());
		pstmt.setString(5, book.getBookDesc());
		pstmt.setInt(6, book.getBookTypeId());
		pstmt.setInt(7, book.getId());
		return pstmt.executeUpdate();
	}
--------------------------------------------------------------------------------	
	/**
	 * 指定图书类别下是否存在图书
	 * @param con
	 * @param bookTypeId
	 * @return
	 * @throws Exception
	 */
	public boolean existBookByBookTypeId(Connection con,String bookTypeId)throws Exception{
		String sql="select * from t_book where bookTypeId=?";
		PreparedStatement pstmt=con.prepareStatement(sql);
		pstmt.setString(1, bookTypeId);
		ResultSet rs=pstmt.executeQuery();
		return rs.next();
		
	}
-----------------------------------------------------------------------------
}

在BookTypeManageInterFrm 图书类别删除事件处理里加判断

/**
	 * 图书类别删除事件处理
	 * @param e
	 */
	private void bookTypeDeleteActionEvent(ActionEvent evt) {
		String id=idTxt.getText();
		if(StringUtil.isEmpty(id)){
			JOptionPane.showMessageDialog(null, "请选择要删除的记录");
			return;
		}
		int n=JOptionPane.showConfirmDialog(null, "是否删除该记录");
		if(n==0){
			Connection con=null;
			try{
				con=dbUtil.getCon();
----------------------------------------------------------------------------------------
				boolean flag=bookDao.existBookByBookTypeId(con, id);
				if(flag){
					JOptionPane.showMessageDialog(null, "当前图书类存在图书,不可删除");
					return;
				}
---------------------------------------------------------------------------------------
				int deleteNum=bookTypeDao.delete(con, id);
				if(deleteNum==1){
					JOptionPane.showMessageDialog(null, "删除成功");
					this.resetValue();
					this.fillTable(new BookType());
				}else{
					JOptionPane.showMessageDialog(null, "删除失败");
				}
			}catch(Exception e){
				e.printStackTrace();
			}finally{
				try {
					dbUtil.close(con);
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		
	}

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值