新建数据库 UserDB,新建表 user,表结构如下: user 表 字段名称 字段类型 备注 id Int 用户编号(主键、自增) username Varchar(20) 用户名称 password Varchar(20) 用户密码通过 JDBC连接数据库,编写 userlist.jsp,显示所有用户信息编写 addUser.jsp,实现用户信息的添加功能,在 UserController.java这个 Servlet中实现用户信息的删除功能编写 userUpdate.jsp,实现用户信息的更新功能。
时间: 2024-02-23 07:00:27 浏览: 158
mysql下创建字段并设置主键的php代码
5星 · 资源好评率100%
以下是实现上述要求的代码示例:
#### 1. 创建 UserDB 数据库和 user 表
```sql
CREATE DATABASE UserDB;
USE UserDB;
CREATE TABLE user (
id INT PRIMARY KEY AUTO_INCREMENT,
username VARCHAR(20),
password VARCHAR(20)
);
```
#### 2. 编写 userlist.jsp 显示所有用户信息
```jsp
<%@ page import="java.sql.*" %>
<%@ page language="java" %>
<%@ page contentType="text/html; charset=UTF-8" %>
<html>
<head>
<title>User List</title>
</head>
<body>
<h1>User List</h1>
<%
String url = "jdbc:mysql://localhost:3306/UserDB";
String user = "root";
String password = "password";
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(url, user, password);
String sql = "SELECT * FROM user";
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
%>
<table border="1">
<tr>
<th>ID</th>
<th>Username</th>
<th>Password</th>
</tr>
<% while (rs.next()) { %>
<tr>
<td><%= rs.getInt("id") %></td>
<td><%= rs.getString("username") %></td>
<td><%= rs.getString("password") %></td>
</tr>
<% } %>
</table>
<%
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (rs != null) rs.close();
if (pstmt != null) pstmt.close();
if (conn != null) conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
%>
</body>
</html>
```
#### 3. 编写 addUser.jsp 实现用户信息的添加功能
```jsp
<%@ page import="java.sql.*" %>
<%@ page language="java" %>
<%@ page contentType="text/html; charset=UTF-8" %>
<html>
<head>
<title>Add User</title>
</head>
<body>
<h1>Add User</h1>
<form method="post" action="UserController">
<label for="username">Username:</label>
<input type="text" id="username" name="username" required><br>
<label for="password">Password:</label>
<input type="password" id="password" name="password" required><br>
<button type="submit">Add User</button>
</form>
</body>
</html>
```
#### 4. 在 UserController.java 这个 Servlet 中实现用户信息的删除功能
```java
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class UserController extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String action = request.getParameter("action");
if (action == null) {
// do nothing
} else if (action.equals("add")) {
String username = request.getParameter("username");
String password = request.getParameter("password");
String url = "jdbc:mysql://localhost:3306/UserDB";
String user = "root";
String pass = "password";
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection(url, user, pass);
String sql = "INSERT INTO user (username, password) VALUES (?, ?)";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, username);
pstmt.setString(2, password);
pstmt.executeUpdate();
pstmt.close();
conn.close();
response.sendRedirect("userlist.jsp");
} catch (Exception e) {
e.printStackTrace();
}
} else if (action.equals("delete")) {
int id = Integer.parseInt(request.getParameter("id"));
String url = "jdbc:mysql://localhost:3306/UserDB";
String user = "root";
String pass = "password";
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection(url, user, pass);
String sql = "DELETE FROM user WHERE id=?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, id);
pstmt.executeUpdate();
pstmt.close();
conn.close();
response.sendRedirect("userlist.jsp");
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
```
#### 5. 编写 userUpdate.jsp 实现用户信息的更新功能
```jsp
<%@ page import="java.sql.*" %>
<%@ page language="java" %>
<%@ page contentType="text/html; charset=UTF-8" %>
<html>
<head>
<title>Update User</title>
</head>
<body>
<h1>Update User</h1>
<%
int id = Integer.parseInt(request.getParameter("id"));
String url = "jdbc:mysql://localhost:3306/UserDB";
String user = "root";
String password = "password";
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(url, user, password);
String sql = "SELECT * FROM user WHERE id=?";
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, id);
rs = pstmt.executeQuery();
rs.next();
%>
<form method="post" action="UserController">
<input type="hidden" name="action" value="update">
<input type="hidden" name="id" value="<%= id %>">
<label for="username">Username:</label>
<input type="text" id="username" name="username" value="<%= rs.getString("username") %>" required><br>
<label for="password">Password:</label>
<input type="password" id="password" name="password" value="<%= rs.getString("password") %>" required><br>
<button type="submit">Update User</button>
</form>
<%
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (rs != null) rs.close();
if (pstmt != null) pstmt.close();
if (conn != null) conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
%>
</body>
</html>
```
以上就是实现用户信息增删改查的完整代码示例。注意在 web.xml 中配置 Servlet 映射。
阅读全文