书店管理系统的Java后台代码
时间: 2023-07-09 09:36:45 浏览: 80
书店管理系统的Java后台代码可以分为不同的模块,例如用户管理、商品管理、订单管理等。下面是一个简单的用户管理模块的代码示例:
```java
// User.java
public class User {
private int id;
private String username;
private String password;
private String email;
// 构造函数、getter、setter等省略
}
// UserDao.java
public class UserDao {
// 连接数据库等操作省略
// 查询所有用户
public List<User> findAll() throws SQLException {
List<User> users = new ArrayList<>();
String sql = "SELECT * FROM users";
try (PreparedStatement stmt = conn.prepareStatement(sql);
ResultSet rs = stmt.executeQuery()) {
while (rs.next()) {
User user = new User();
user.setId(rs.getInt("id"));
user.setUsername(rs.getString("username"));
user.setPassword(rs.getString("password"));
user.setEmail(rs.getString("email"));
users.add(user);
}
}
return users;
}
// 添加用户
public void add(User user) throws SQLException {
String sql = "INSERT INTO users (username, password, email) VALUES (?, ?, ?)";
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setString(1, user.getUsername());
stmt.setString(2, user.getPassword());
stmt.setString(3, user.getEmail());
stmt.executeUpdate();
}
}
// 更新用户
public void update(User user) throws SQLException {
String sql = "UPDATE users SET username=?, password=?, email=? WHERE id=?";
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setString(1, user.getUsername());
stmt.setString(2, user.getPassword());
stmt.setString(3, user.getEmail());
stmt.setInt(4, user.getId());
stmt.executeUpdate();
}
}
// 删除用户
public void delete(int id) throws SQLException {
String sql = "DELETE FROM users WHERE id=?";
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setInt(1, id);
stmt.executeUpdate();
}
}
}
// UserController.java
public class UserController {
private UserDao userDao = new UserDao();
// 查询所有用户
public void findAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
List<User> users = userDao.findAll();
request.setAttribute("users", users);
request.getRequestDispatcher("/user/list.jsp").forward(request, response);
}
// 添加用户
public void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
User user = new User();
user.setUsername(request.getParameter("username"));
user.setPassword(request.getParameter("password"));
user.setEmail(request.getParameter("email"));
userDao.add(user);
response.sendRedirect(request.getContextPath() + "/user");
}
// 更新用户
public void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
User user = new User();
user.setId(Integer.parseInt(request.getParameter("id")));
user.setUsername(request.getParameter("username"));
user.setPassword(request.getParameter("password"));
user.setEmail(request.getParameter("email"));
userDao.update(user);
response.sendRedirect(request.getContextPath() + "/user");
}
// 删除用户
public void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int id = Integer.parseInt(request.getParameter("id"));
userDao.delete(id);
response.sendRedirect(request.getContextPath() + "/user");
}
}
```
以上代码仅供参考,实际项目中需要根据具体需求进行修改和完善。同时需要注意的是,书店管理系统一般还需要考虑安全性、性能等方面的问题。
阅读全文