lifei111

导航

 

写了WEB.
6)add.jsp文件代码
<%@ page contentType="text/html; charset=utf-8" errorPage="error.jsp"%>

添加学生信息

添加学生信息

学号:
姓名:
性别:
生日:
返回列表

7)addsave.jsp文件代码
<%@ page contentType="text/html; charset=utf-8" import="java.sql.*" errorPage="error.jsp"%>

添加学生结果

添加学生信息

<% request.setCharacterEncoding("utf-8"); String sno = request.getParameter("sno"); String name = request.getParameter("name"); String gender = request.getParameter("gender"); String birthday = request.getParameter("birthday"); Connection conn = null; PreparedStatement pstmt = null; try { // 加载驱动 Class.forName("com.mysql.jdbc.Driver"); // 建立连接(请修改成你的数据库密码) String url = "jdbc:mysql://localhost:3306/studentdb?useSSL=false&characterEncoding=utf8"; String user = "root"; String password = "141178"; // 改成你的MySQL密码 conn = DriverManager.getConnection(url, user, password); // 插入数据 String sql = "INSERT INTO student(sno, name, gender, birthday) VALUES(?, ?, ?, ?)"; pstmt = conn.prepareStatement(sql); pstmt.setString(1, sno); pstmt.setString(2, name); pstmt.setString(3, gender); pstmt.setDate(4, java.sql.Date.valueOf(birthday)); int result = pstmt.executeUpdate(); if(result > 0) { out.println("

添加成功!

"); } else { out.println("

添加失败!

"); } } catch(SQLException e) { if(e.getMessage().contains("Duplicate")) { out.println("

学号已存在,请勿重复添加!

"); } else { out.println("

数据库错误:" + e.getMessage() + "

"); } } catch(Exception e) { out.println("

系统错误:" + e.getMessage() + "

"); } finally { if(pstmt != null) try { pstmt.close(); } catch(SQLException e) {} if(conn != null) try { conn.close(); } catch(SQLException e) {} } %>
返回学生列表
继续添加
8)del.jsp文件代码 <%@ page contentType="text/html; charset=utf-8" import="java.sql.*" errorPage="error.jsp"%> 删除学生

删除学生信息

<% String sno = request.getParameter("sno"); Connection conn = null; PreparedStatement pstmt = null; try { Class.forName("com.mysql.jdbc.Driver"); String url = "jdbc:mysql://localhost:3306/studentdb?useSSL=false&characterEncoding=utf8"; String user = "root"; String password = "141178"; // 改成你的MySQL密码 conn = DriverManager.getConnection(url, user, password); String sql = "DELETE FROM student WHERE sno=?"; pstmt = conn.prepareStatement(sql); pstmt.setString(1, sno); int result = pstmt.executeUpdate(); if(result > 0) { out.println("

删除成功!

"); } else { out.println("

删除失败,学生不存在!

"); } } catch(Exception e) { out.println("

删除错误:" + e.getMessage() + "

"); } finally { if(pstmt != null) try { pstmt.close(); } catch(SQLException e) {} if(conn != null) try { conn.close(); } catch(SQLException e) {} } %>
返回学生列表
9)edit.jsp文件代码 <%@ page contentType="text/html; charset=utf-8" import="java.sql.*" errorPage="error.jsp"%> 编辑学生信息

编辑学生信息

<% String sno = request.getParameter("sno"); String name = "", gender = "", birthday = ""; Connection conn = null; PreparedStatement pstmt = null; ResultSet rs = null; try { Class.forName("com.mysql.jdbc.Driver"); String url = "jdbc:mysql://localhost:3306/studentdb?useSSL=false&characterEncoding=utf8"; String user = "root"; String password = "141178"; // 改成你的MySQL密码 conn = DriverManager.getConnection(url, user, password); String sql = "SELECT * FROM student WHERE sno = ?"; pstmt = conn.prepareStatement(sql); pstmt.setString(1, sno); rs = pstmt.executeQuery(); if(rs.next()) { name = rs.getString("name"); gender = rs.getString("gender"); birthday = rs.getDate("birthday").toString(); } else { out.println("

学生不存在!

"); out.println("返回列表"); return; } } catch(Exception e) { out.println("

查询错误:" + e.getMessage() + "

"); out.println("返回列表"); return; } finally { if(rs != null) try { rs.close(); } catch(SQLException e) {} if(pstmt != null) try { pstmt.close(); } catch(SQLException e) {} if(conn != null) try { conn.close(); } catch(SQLException e) {} } %>
学号:
姓名:
性别:
生日:
返回列表
10)editsave.jsp文件代码 <%@ page contentType="text/html; charset=utf-8" import="java.sql.*" errorPage="error.jsp"%> 修改学生结果

修改学生信息

<% request.setCharacterEncoding("utf-8"); String sno = request.getParameter("sno"); String name = request.getParameter("name"); String gender = request.getParameter("gender"); String birthday = request.getParameter("birthday"); Connection conn = null; PreparedStatement pstmt = null; try { Class.forName("com.mysql.jdbc.Driver"); String url = "jdbc:mysql://localhost:3306/studentdb?useSSL=false&characterEncoding=utf8"; String user = "root"; String password = "141178"; // 改成你的MySQL密码 conn = DriverManager.getConnection(url, user, password); String sql = "UPDATE student SET name=?, gender=?, birthday=? WHERE sno=?"; pstmt = conn.prepareStatement(sql); pstmt.setString(1, name); pstmt.setString(2, gender); pstmt.setDate(3, java.sql.Date.valueOf(birthday)); pstmt.setString(4, sno); int result = pstmt.executeUpdate(); if(result > 0) { out.println("

修改成功!

"); } else { out.println("

修改失败!

"); } } catch(Exception e) { out.println("

修改错误:" + e.getMessage() + "

"); } finally { if(pstmt != null) try { pstmt.close(); } catch(SQLException e) {} if(conn != null) try { conn.close(); } catch(SQLException e) {} } %>
返回学生列表
11)error.jsp文件代码 <%@ page contentType="text/html; charset=utf-8" isErrorPage="true"%> 系统错误

系统出现错误

错误信息:<%= exception.getMessage() %>

请联系管理员或检查数据库配置

返回首页
12)index.jsp文件代码 <%@ page contentType="text/html; charset=utf-8" import="java.sql.*" errorPage="error.jsp"%> 学生信息管理系统

学生信息列表

添加学生 <% Connection conn = null; Statement stmt = null; ResultSet rs = null; try { Class.forName("com.mysql.jdbc.Driver"); String url = "jdbc:mysql://localhost:3306/studentdb?useSSL=false&characterEncoding=utf8"; String user = "root"; String password = "141178"; // 改成你的MySQL密码 conn = DriverManager.getConnection(url, user, password); stmt = conn.createStatement(); rs = stmt.executeQuery("SELECT * FROM student"); while(rs.next()) { String sno = rs.getString("sno"); String name = rs.getString("name"); String gender = rs.getString("gender"); Date birthday = rs.getDate("birthday"); %> <% } } catch(Exception e) { out.println(""); } finally { if(rs != null) try { rs.close(); } catch(SQLException e) {} if(stmt != null) try { stmt.close(); } catch(SQLException e) {} if(conn != null) try { conn.close(); } catch(SQLException e) {} } %>
学号 姓名 性别 生日 操作
<%=sno%> <%=name%> <%=gender%> <%=birthday%> 编辑 删除
查询错误:" + e.getMessage() + "
13)style.css文件代码 body { font-family: "微软雅黑", Arial, sans-serif; background-color: #f0f0f0; } .container { width: 80%; margin: 30px auto; background-color: white; padding: 30px; border-radius: 10px; box-shadow: 0 0 10px rgba(0,0,0,0.1); } h2 { color: #333; border-bottom: 2px solid #4CAF50; padding-bottom: 10px; } table { border-collapse: collapse; margin-top: 15px; } th { background-color: #4CAF50; color: white; padding: 10px; text-align: left; } td { padding: 10px; border-bottom: 1px solid #ddd; } a { color: #4CAF50; text-decoration: none; margin: 0 5px; } a:hover { text-decoration: underline; } input[type="text"], input[type="date"], select { width: 80%; padding: 8px; margin: 5px 0; border: 1px solid #ddd; border-radius: 4px; } input[type="submit"], input[type="reset"] { background-color: #4CAF50; color: white; padding: 8px 20px; border: none; cursor: pointer; border-radius: 4px; margin-right: 10px; } input[type="submit"]:hover, input[type="reset"]:hover { background-color: #45a049; }
posted on 2026-06-14 23:23  猪头小呆呆  阅读(3)  评论(0)    收藏  举报