查询书籍功能

以下主要介绍查询书籍操作主要如何进行操作的,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对象

  • 8
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,您可以使用以下的 Java 代码实现根据作者查询图书的功能: ```java import java.sql.*; public class BookQueryByAuthor { public static void main(String[] args) { Connection conn = null; PreparedStatement pstmt = null; ResultSet rs = null; try { // 加载 JDBC 驱动 Class.forName("com.mysql.jdbc.Driver"); // 建立数据库连接 conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/books", "root", "password"); // 创建 PreparedStatement 对象 pstmt = conn.prepareStatement("SELECT book_id, book_name, author FROM book WHERE author = ?"); // 设置参数 pstmt.setString(1, "张三"); // 执行 SQL 查询语句 rs = pstmt.executeQuery(); // 输出查询结果 while (rs.next()) { int bookId = rs.getInt("book_id"); String bookName = rs.getString("book_name"); String author = rs.getString("author"); System.out.println("编号:" + bookId + ",书名:" + bookName + ",作者:" + author); } } catch (ClassNotFoundException | SQLException e) { e.printStackTrace(); } finally { // 关闭资源 try { if (rs != null) rs.close(); if (pstmt != null) pstmt.close(); if (conn != null) conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } } ``` 在上面的代码中,我们使用了 JDBC 连接 MySQL 数据库,并且使用 PreparedStatement 对象执行了一条带参数的 SQL 查询语句,查询指定作者的图书信息,并将查询结果输出到控制台上。需要注意的是,您需要将代码中的数据库连接 URL、用户名和密码修改为您自己的数据库连接信息,以及将 `setString(1, "张三")` 中的作者名修改为您想要查询的作者名。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值