JSP+Servlet+JavaBean实现数据库的增删改查代码
时间: 2023-08-15 14:03:58 浏览: 119
好的,以下是JSP+Servlet+JavaBean实现数据库的增删改查的代码示例:
1. JavaBean类
```
public class User {
private int id;
private String name;
private int age;
private String gender;
// 构造方法
public User() {}
// setter和getter方法
public void setId(int id) {
this.id = id;
}
public int getId() {
return id;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setAge(int age) {
this.age = age;
}
public int getAge() {
return age;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getGender() {
return gender;
}
}
```
2. DAO层
```
public class UserDAO {
// 数据库连接信息
private String url = "jdbc:mysql://localhost:3306/test";
private String username = "root";
private String password = "123456";
// 增加用户
public void addUser(User user) {
try {
// 加载MySQL驱动程序
Class.forName("com.mysql.jdbc.Driver");
// 获取数据库连接
Connection conn = DriverManager.getConnection(url, username, password);
// 创建PreparedStatement对象
PreparedStatement ps = conn.prepareStatement("INSERT INTO user(name, age, gender) VALUES (?, ?, ?)");
// 设置参数
ps.setString(1, user.getName());
ps.setInt(2, user.getAge());
ps.setString(3, user.getGender());
// 执行SQL语句
ps.executeUpdate();
// 关闭连接和PreparedStatement对象
ps.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
// 删除用户
public void deleteUser(int id) {
try {
// 加载MySQL驱动程序
Class.forName("com.mysql.jdbc.Driver");
// 获取数据库连接
Connection conn = DriverManager.getConnection(url, username, password);
// 创建PreparedStatement对象
PreparedStatement ps = conn.prepareStatement("DELETE FROM user WHERE id=?");
// 设置参数
ps.setInt(1, id);
// 执行SQL语句
ps.executeUpdate();
// 关闭连接和PreparedStatement对象
ps.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
// 修改用户
public void updateUser(User user) {
try {
// 加载MySQL驱动程序
Class.forName("com.mysql.jdbc.Driver");
// 获取数据库连接
Connection conn = DriverManager.getConnection(url, username, password);
// 创建PreparedStatement对象
PreparedStatement ps = conn.prepareStatement("UPDATE user SET name=?, age=?, gender=? WHERE id=?");
// 设置参数
ps.setString(1, user.getName());
ps.setInt(2, user.getAge());
ps.setString(3, user.getGender());
ps.setInt(4, user.getId());
// 执行SQL语句
ps.executeUpdate();
// 关闭连接和PreparedStatement对象
ps.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
// 查询用户
public List<User> getUsers() {
List<User> userList = new ArrayList<>();
try {
// 加载MySQL驱动程序
Class.forName("com.mysql.jdbc.Driver");
// 获取数据库连接
Connection conn = DriverManager.getConnection(url, username, password);
// 创建Statement对象
Statement stmt = conn.createStatement();
// 执行SQL语句
ResultSet rs = stmt.executeQuery("SELECT * FROM user");
// 处理结果集
while (rs.next()) {
User user = new User();
user.setId(rs.getInt("id"));
user.setName(rs.getString("name"));
user.setAge(rs.getInt("age"));
user.setGender(rs.getString("gender"));
userList.add(user);
}
// 关闭连接和Statement对象
rs.close();
stmt.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
return userList;
}
}
```
3. Servlet类
```
public class UserServlet extends HttpServlet {
// 创建UserDAO对象
private UserDAO userDao = new UserDAO();
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 处理GET请求
List<User> userList = userDao.getUsers();
// 将userList存入request对象中
request.setAttribute("userList", userList);
// 转发到user.jsp页面
RequestDispatcher rd = request.getRequestDispatcher("user.jsp");
rd.forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 处理POST请求
String action = request.getParameter("action");
if ("add".equals(action)) {
// 添加用户
User user = new User();
user.setName(request.getParameter("name"));
user.setAge(Integer.parseInt(request.getParameter("age")));
user.setGender(request.getParameter("gender"));
userDao.addUser(user);
} else if ("delete".equals(action)) {
// 删除用户
int id = Integer.parseInt(request.getParameter("id"));
userDao.deleteUser(id);
} else if ("update".equals(action)) {
// 修改用户
User user = new User();
user.setId(Integer.parseInt(request.getParameter("id")));
user.setName(request.getParameter("name"));
user.setAge(Integer.parseInt(request.getParameter("age")));
user.setGender(request.getParameter("gender"));
userDao.updateUser(user);
}
// 重定向到UserServlet,处理GET请求
response.sendRedirect(request.getContextPath() + "/UserServlet");
}
}
```
4. JSP页面
```
<html>
<head>
<title>用户管理</title>
</head>
<body>
<h1>用户管理</h1>
<table border="1">
<thead>
<tr>
<th>ID</th>
<th>姓名</th>
<th>年龄</th>
<th>性别</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<c:forEach items="${userList}" var="user">
<tr>
<td>${user.id}</td>
<td>${user.name}</td>
<td>${user.age}</td>
<td>${user.gender}</td>
<td>
<form method="post" action="UserServlet">
<input type="hidden" name="id" value="${user.id}">
<input type="hidden" name="action" value="delete">
<input type="submit" value="删除">
</form>
<form method="post" action="UserServlet">
<input type="hidden" name="id" value="${user.id}">
<input type="hidden" name="action" value="update">
<input type="submit" value="修改">
</form>
</td>
</tr>
</c:forEach>
</tbody>
</table>
<hr>
<h2>添加用户</h2>
<form method="post" action="UserServlet">
姓名:<input type="text" name="name"><br>
年龄:<input type="text" name="age"><br>
性别:<input type="text" name="gender"><br>
<input type="hidden" name="action" value="add">
<input type="submit" value="添加">
</form>
</body>
</html>
```
以上代码仅为示例,具体实现还需要根据具体需求进行调整。
阅读全文