javabean+数据库+jsp代码实现图书查询功能
时间: 2023-12-27 12:02:30 浏览: 202
图书资料查询系统(java jsp)
假设有一个图书馆的数据库,其中有一张名为books的表,表中有以下字段:id(图书编号)、name(图书名称)、author(图书作者)、publisher(图书出版社)、price(图书价格)。
以下是基于JavaBean和JSP实现图书查询功能的代码:
1. Book.java
```java
public class Book {
private int id;
private String name;
private String author;
private String publisher;
private double price;
// getters and setters
}
```
2. BookDAO.java
```java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class BookDAO {
private String jdbcURL;
private String jdbcUsername;
private String jdbcPassword;
private Connection jdbcConnection;
public BookDAO(String jdbcURL, String jdbcUsername, String jdbcPassword) {
this.jdbcURL = jdbcURL;
this.jdbcUsername = jdbcUsername;
this.jdbcPassword = jdbcPassword;
}
private void connect() throws SQLException {
if (jdbcConnection == null || jdbcConnection.isClosed()) {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
throw new SQLException(e);
}
jdbcConnection = DriverManager.getConnection(jdbcURL, jdbcUsername, jdbcPassword);
}
}
private void disconnect() throws SQLException {
if (jdbcConnection != null && !jdbcConnection.isClosed()) {
jdbcConnection.close();
}
}
public List<Book> searchBooks(String keyword) throws SQLException {
List<Book> books = new ArrayList<>();
String sql = "SELECT * FROM books WHERE name LIKE ? OR author LIKE ? OR publisher LIKE ?";
connect();
PreparedStatement statement = jdbcConnection.prepareStatement(sql);
statement.setString(1, "%" + keyword + "%");
statement.setString(2, "%" + keyword + "%");
statement.setString(3, "%" + keyword + "%");
ResultSet resultSet = statement.executeQuery();
while (resultSet.next()) {
Book book = new Book();
book.setId(resultSet.getInt("id"));
book.setName(resultSet.getString("name"));
book.setAuthor(resultSet.getString("author"));
book.setPublisher(resultSet.getString("publisher"));
book.setPrice(resultSet.getDouble("price"));
books.add(book);
}
resultSet.close();
statement.close();
disconnect();
return books;
}
}
```
3. search.jsp
```html
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>图书查询</title>
</head>
<body>
<h1>图书查询</h1>
<form action="search.jsp" method="get">
<label for="keyword">关键字:</label>
<input type="text" name="keyword" id="keyword">
<input type="submit" value="查询">
</form>
<hr>
<% String keyword = request.getParameter("keyword");
if (keyword != null && !keyword.trim().isEmpty()) {
BookDAO bookDAO = new BookDAO("jdbc:mysql://localhost:3306/library", "root", "password");
List<Book> books = bookDAO.searchBooks(keyword);
if (books.isEmpty()) { %>
<p>未找到相关图书。</p>
<% } else { %>
<table border="1">
<tr>
<th>编号</th>
<th>名称</th>
<th>作者</th>
<th>出版社</th>
<th>价格</th>
</tr>
<% for (Book book : books) { %>
<tr>
<td><%= book.getId() %></td>
<td><%= book.getName() %></td>
<td><%= book.getAuthor() %></td>
<td><%= book.getPublisher() %></td>
<td><%= book.getPrice() %></td>
</tr>
<% } %>
</table>
<% }
} %>
</body>
</html>
```
在浏览器中访问search.jsp,输入关键字并点击查询按钮,即可查询到相关图书。
阅读全文