查询书籍功能

以下主要介绍查询书籍操作主要如何进行操作的,GUI编程省略。

核心代码如下: 

查询按钮添加监听器:

button_1 = new JButton("查询");
			button_1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					int index = comboBox.getSelectedIndex();
					if(index==0){
						String bookName = textField_1.getText();
						Book book = new Book();
						book.setBookName(bookName);
						putDates(book);
					}else{
						String authoerName = textField_1.getText();
						Book book = new Book();
						book.setAuthor(authoerName);
						putDates(book);
					}
				}
			});
int index = comboBox.getSelectedIndex();

通过你选择书籍名称还是书籍作者选项来获取索引,0代表书籍名称,1代表书籍作者 

 

comboBox = new JComboBox();
			comboBox.setFont(new Font("幼圆", Font.BOLD, 15));
			comboBox.setBounds(123, 26, 109, 24);
			comboBox.addItem("书籍名称");
			comboBox.addItem("书籍作者");
			panel_2.add(comboBox);

 然后进入if如果是书籍名称怎么查否则就是根据书籍作者查

String bookName = textField_1.getText();
Book book = new Book();
book.setBookName(bookName);
putDates(book);

把书籍名字给book对象,再把book对象交给putDates()方法,随后执行putDates()方法

putDates()方法:

//从数据库获取书籍信息
	private void putDates(Book book) {
		//表格对象
		DefaultTableModel model = (DefaultTableModel) BookTable.getModel();
		//清空表格
		model.setRowCount(0);
		Connection con = null;
		try {
			con = dbUtil.getConnection();
			// 将图书状态设置为1
			book.setStatus(1);
			// 调用bookDao的list方法,传入连接对象con和图书对象book,获取图书列表并将其存储在list对象中。
			ResultSet list = bookDao.list(con, book);
			
			while (list.next()) {
				Vector rowData = new Vector();
				rowData.add(list.getInt("id"));
				rowData.add(list.getString("book_name"));
				rowData.add(list.getString("type_name"));
				rowData.add(list.getString("author"));
				rowData.add(list.getString("remark"));
				//把数据添加到表格中
				model.addRow(rowData);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				dbUtil.closeCon(con);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
while (list.next()) {
				Vector rowData = new Vector();
				rowData.add(list.getInt("id"));
				rowData.add(list.getString("book_name"));
				rowData.add(list.getString("type_name"));
				rowData.add(list.getString("author"));
				rowData.add(list.getString("remark"));
				//把数据添加到表格中
				model.addRow(rowData);
			}

当list.next()返回ture,就会继续执行循环内的代码。

如果list是有数据的就会执行循环内代码,循环内代码就是把list中的数据往rowDate中加,直到list中的数据被遍历完,结束循环

dao层中BookDao类中list()函数

// 图书信息查询
	public ResultSet list(Connection con,Book book)throws Exception{
		// 字符缓冲流sb
		StringBuffer sb=new StringBuffer("select b.*,bt.type_name from book b,book_type bt where b.type_id=bt.id");
		//判断书籍名称是否为空
		if(!toolUtil.isEmpty(book.getBookName())){
			//拼接查询语句
			sb.append(" and b.book_name like '%"+book.getBookName()+"%'");
		}
		//判断书籍作者是否为空
		if(!toolUtil.isEmpty(book.getAuthor())){
			//拼接查询语句
			sb.append(" and b.author like '%"+book.getAuthor()+"%'");
		}
		//判断书籍类型是否为空
		if(book.getBookTypeId()!=null && book.getBookTypeId()!=0){
			//拼接查询语句
			sb.append(" and b.type_id="+book.getBookTypeId());
		}
		//判断书籍状态是否为空
		if(book.getStatus()!=null){
			//拼接查询语句
			sb.append(" and b.status="+book.getStatus());
		}
		//判断书籍id是否为空
		if(book.getBookId() != null){
			//拼接查询语句
			sb.append(" and b.id="+book.getBookId());
		}
		//排序
		sb.append(" ORDER BY b.status");
		//创建预编译的sql语句
		PreparedStatement pstmt=(PreparedStatement) con.prepareStatement(sb.toString());
		//执行查询
		return pstmt.executeQuery();
	}

最后执行查询然后返回查询结果到上面list对象

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值