请用JSP写一个后端界面,要求实现功能对于MySql用户表的各种信息实现增删改查
时间: 2024-02-19 14:58:32 浏览: 49
很抱歉,我作为一个语言模型并不能直接提供代码,以下是一个简单的JSP页面示例,您可以根据自己的需要进行修改和扩展:
```jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<!DOCTYPE html>
<html>
<head>
<title>User Management</title>
</head>
<body>
<h1>User Management</h1>
<%
// 连接数据库
String url = "jdbc:mysql://localhost:3306/db_name";
String username = "username";
String password = "password";
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(url, username, password);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
// 处理用户请求
String action = request.getParameter("action");
if (action != null) {
if (action.equals("add")) {
String name = request.getParameter("name");
String email = request.getParameter("email");
String sql = "INSERT INTO user (name, email) VALUES (?, ?)";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, name);
ps.setString(2, email);
ps.executeUpdate();
} else if (action.equals("delete")) {
String id = request.getParameter("id");
String sql = "DELETE FROM user WHERE id = ?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, id);
ps.executeUpdate();
} else if (action.equals("update")) {
String id = request.getParameter("id");
String name = request.getParameter("name");
String email = request.getParameter("email");
String sql = "UPDATE user SET name = ?, email = ? WHERE id = ?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, name);
ps.setString(2, email);
ps.setString(3, id);
ps.executeUpdate();
}
}
// 查询用户列表
String sql = "SELECT * FROM user";
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
%>
<!-- 显示用户列表 -->
<h2>User List</h2>
<table border="1">
<tr>
<th>ID</th>
<th>Name</th>
<th>Email</th>
<th>Edit</th>
<th>Delete</th>
</tr>
<%
while (rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
String email = rs.getString("email");
%>
<tr>
<td><%=id%></td>
<td><%=name%></td>
<td><%=email%></td>
<td><a href="edit.jsp?id=<%=id%>&name=<%=name%>&email=<%=email%>">Edit</a></td>
<td><a href="index.jsp?action=delete&id=<%=id%>">Delete</a></td>
</tr>
<%
}
%>
</table>
<!-- 添加用户表单 -->
<h2>Add User</h2>
<form method="post" action="index.jsp?action=add">
Name: <input type="text" name="name"><br>
Email: <input type="text" name="email"><br>
<input type="submit" value="Add">
</form>
<!-- 编辑用户表单 -->
<%
String id = request.getParameter("id");
if (id != null) {
String name = request.getParameter("name");
String email = request.getParameter("email");
%>
<h2>Edit User</h2>
<form method="post" action="index.jsp?action=update">
<input type="hidden" name="id" value="<%=id%>">
Name: <input type="text" name="name" value="<%=name%>"><br>
Email: <input type="text" name="email" value="<%=email%>"><br>
<input type="submit" value="Update">
</form>
<%
}
%>
<%
// 关闭数据库连接
rs.close();
stmt.close();
conn.close();
%>
</body>
</html>
```
请注意,这只是一个简单的示例,需要根据您的实际情况进行修改和扩展。此外,为了安全起见,建议使用 Prepared Statement 来防止 SQL 注入攻击。
阅读全文