新建数据库 UserDB,新建表 user,表结构如下: User 表 字段名称 字段类型 备注 user_id int 编号(主键) user_name char(10) 姓名 user_sex char(2) 性别 男/女 user_age int 年龄 通过 JDBC 连接数据库,编写 user_list.jsp,显示所有用户信息,student_list.jsp 编写 user_add.jsp,实现用户信息的添加功能。编写 user_del.jsp,实现用户信息的删除功能。 编写 user_update.jsp,实现用户信息的更新功能
时间: 2024-02-22 14:00:23 浏览: 84
好的,我会尽力帮您解决这个问题。首先,您需要确保已经安装了适当的数据库和JDBC驱动程序。然后,您可以按照以下步骤进行操作:
1. 在数据库中创建 UserDB 数据库,然后在该数据库中创建 user 表,表结构如下:
```
CREATE TABLE user (
user_id INT PRIMARY KEY,
user_name CHAR(10),
user_sex CHAR(2),
user_age INT
);
```
2. 在 JSP 文件中编写 JDBC 代码,连接到数据库并执行相应的操作。例如,以下是 user_list.jsp 的代码:
```
<%@ page import="java.sql.*" %>
<%
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost/UserDB", "username", "password");
stmt = conn.createStatement();
rs = stmt.executeQuery("SELECT * FROM user");
out.println("<table>");
out.println("<tr><th>ID</th><th>Name</th><th>Sex</th><th>Age</th></tr>");
while (rs.next()) {
int id = rs.getInt("user_id");
String name = rs.getString("user_name");
String sex = rs.getString("user_sex");
int age = rs.getInt("user_age");
out.println("<tr><td>" + id + "</td><td>" + name + "</td><td>" + sex + "</td><td>" + age + "</td></tr>");
}
out.println("</table>");
} catch (Exception e) {
e.printStackTrace();
} finally {
if (rs != null) rs.close();
if (stmt != null) stmt.close();
if (conn != null) conn.close();
}
%>
```
3. 编写 user_add.jsp,实现用户信息的添加功能。例如:
```
<%@ page import="java.sql.*" %>
<%
Connection conn = null;
PreparedStatement pstmt = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost/UserDB", "username", "password");
pstmt = conn.prepareStatement("INSERT INTO user (user_id, user_name, user_sex, user_age) VALUES (?, ?, ?, ?)");
pstmt.setInt(1, Integer.parseInt(request.getParameter("user_id")));
pstmt.setString(2, request.getParameter("user_name"));
pstmt.setString(3, request.getParameter("user_sex"));
pstmt.setInt(4, Integer.parseInt(request.getParameter("user_age")));
int result = pstmt.executeUpdate();
if (result > 0) {
out.println("User added successfully.");
} else {
out.println("Failed to add user.");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (pstmt != null) pstmt.close();
if (conn != null) conn.close();
}
%>
```
4. 编写 user_del.jsp,实现用户信息的删除功能。例如:
```
<%@ page import="java.sql.*" %>
<%
Connection conn = null;
PreparedStatement pstmt = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost/UserDB", "username", "password");
pstmt = conn.prepareStatement("DELETE FROM user WHERE user_id = ?");
pstmt.setInt(1, Integer.parseInt(request.getParameter("user_id")));
int result = pstmt.executeUpdate();
if (result > 0) {
out.println("User deleted successfully.");
} else {
out.println("Failed to delete user.");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (pstmt != null) pstmt.close();
if (conn != null) conn.close();
}
%>
```
5. 编写 user_update.jsp,实现用户信息的更新功能。例如:
```
<%@ page import="java.sql.*" %>
<%
Connection conn = null;
PreparedStatement pstmt = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost/UserDB", "username", "password");
pstmt = conn.prepareStatement("UPDATE user SET user_name = ?, user_sex = ?, user_age = ? WHERE user_id = ?");
pstmt.setString(1, request.getParameter("user_name"));
pstmt.setString(2, request.getParameter("user_sex"));
pstmt.setInt(3, Integer.parseInt(request.getParameter("user_age")));
pstmt.setInt(4, Integer.parseInt(request.getParameter("user_id")));
int result = pstmt.executeUpdate();
if (result > 0) {
out.println("User updated successfully.");
} else {
out.println("Failed to update user.");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (pstmt != null) pstmt.close();
if (conn != null) conn.close();
}
%>
```
以上就是实现用户信息管理的基本步骤,您可以根据需要进行修改和扩展。同时请注意,这里的代码只是示例,您需要根据实际情况进行修改和完善。
阅读全文