以下是一个简单的Java Web图书管理系统的代码示例,实现了基本的用户登录、添加书籍、查询书籍、修改书籍信息等功能。
1.数据库表结构
CREATE TABLE `book` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL,
`author` varchar(50) NOT NULL,
`price` float NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4;
2.后端接口代码(使用Servlet和JSP)
(1)用户登录
LoginServlet.java
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 获取用户名和密码
String username = request.getParameter("username");
String password = request.getParameter("password");
// 判断用户名和密码是否正确
if ("admin".equals(username) && "123456".equals(password)) {
// 登录成功,将用户信息存入session中
HttpSession session = request.getSession();
session.setAttribute("username", username);
// 跳转到图书列表页面
response.sendRedirect(request.getContextPath() + "/bookList.jsp");
} else {
// 登录失败,返回登录页面并显示错误信息
request.setAttribute("error", "用户名或密码错误");
request.getRequestDispatcher("/login.jsp").forward(request, response);
}
}
}
login.jsp
<form action="${pageContext.request.contextPath}/login" method="post">
用户名:<input type="text" name="username"><br>
密码:<input type="password" name="password"><br>
<input type="submit" value="登录">
</form>
(2)添加书籍
AddBookServlet.java
@WebServlet("/addBook")
public class AddBookServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 获取书籍信息
String name = request.getParameter("name");
String author = request.getParameter("author");
float price = Float.parseFloat(request.getParameter("price"));
// 将书籍信息插入到数据库中
Connection conn = null;
PreparedStatement ps = null;
try {
conn = DBUtils.getConnection();
String sql = "INSERT INTO book (name, author, price) VALUES (?, ?, ?)";
ps = conn.prepareStatement(sql);
ps.setString(1, name);
ps.setString(2, author);
ps.setFloat(3, price);
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtils.close(conn, ps, null);
}
// 跳转到图书列表页面
response.sendRedirect(request.getContextPath() + "/bookList.jsp");
}
}
addBook.jsp
<form action="${pageContext.request.contextPath}/addBook" method="post">
书名:<input type="text" name="name"><br>
作者:<input type="text" name="author"><br>
价格:<input type="text" name="price"><br>
<input type="submit" value="提交">
</form>
(3)查询书籍
BookListServlet.java
@WebServlet("/bookList")
public class BookListServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 查询数据库中所有的书籍信息
List<Book> bookList = new ArrayList<>();
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
conn = DBUtils.getConnection();
String sql = "SELECT * FROM book";
stmt = conn.createStatement();
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.setPrice(rs.getFloat("price"));
bookList.add(book);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtils.close(conn, stmt, rs);
}
// 将查询结果存入request中,跳转到图书列表页面
request.setAttribute("bookList", bookList);
request.getRequestDispatcher("/bookList.jsp").forward(request, response);
}
}
bookList.jsp
<table>
<tr>
<th>ID</th>
<th>书名</th>
<th>作者</th>
<th>价格</th>
<th>操作</th>
</tr>
<c:forEach items="${bookList}" var="book">
<tr>
<td>${book.id}</td>
<td>${book.name}</td>
<td>${book.author}</td>
<td>${book.price}</td>
<td>
<a href="${pageContext.request.contextPath}/editBook?id=${book.id}">编辑</a>
<a href="${pageContext.request.contextPath}/deleteBook?id=${book.id}">删除</a>
</td>
</tr>
</c:forEach>
</table>
<a href="${pageContext.request.contextPath}/addBook.jsp">添加书籍</a>
(4)修改书籍信息
EditBookServlet.java
@WebServlet("/editBook")
public class EditBookServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 根据id查询数据库中的书籍信息
int id = Integer.parseInt(request.getParameter("id"));
Book book = null;
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
conn = DBUtils.getConnection();
String sql = "SELECT * FROM book WHERE id = ?";
ps = conn.prepareStatement(sql);
ps.setInt(1, id);
rs = ps.executeQuery();
if (rs.next()) {
book = new Book();
book.setId(rs.getInt("id"));
book.setName(rs.getString("name"));
book.setAuthor(rs.getString("author"));
book.setPrice(rs.getFloat("price"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtils.close(conn, ps, rs);
}
// 将查询结果存入request中,跳转到编辑页面
request.setAttribute("book", book);
request.getRequestDispatcher("/editBook.jsp").forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 获取书籍信息
int id = Integer.parseInt(request.getParameter("id"));
String name = request.getParameter("name");
String author = request.getParameter("author");
float price = Float.parseFloat(request.getParameter("price"));
// 更新数据库中的书籍信息
Connection conn = null;
PreparedStatement ps = null;
try {
conn = DBUtils.getConnection();
String sql = "UPDATE book SET name=?, author=?, price=? WHERE id=?";
ps = conn.prepareStatement(sql);
ps.setString(1, name);
ps.setString(2, author);
ps.setFloat(3, price);
ps.setInt(4, id);
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtils.close(conn, ps, null);
}
// 跳转到图书列表页面
response.sendRedirect(request.getContextPath() + "/bookList");
}
}
editBook.jsp
<form action="${pageContext.request.contextPath}/editBook" method="post">
<input type="hidden" name="id" value="${book.id}">
书名:<input type="text" name="name" value="${book.name}"><br>
作者:<input type="text" name="author" value="${book.author}"><br>
价格:<input type="text" name="price" value="${book.price}"><br>
<input type="submit" value="提交">
</form>
3.前端界面
参考上述示例代码中的login.jsp、addBook.jsp和bookList.jsp。
以上是一个简单的Java Web图书管理系统的代码示例,仅供参考。实际开发中,还需要考虑安全性、性能优化等方面的问题。