写了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) {} } %>返回学生列表
继续添加
删除学生信息
<% 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) {} } %>返回学生列表
编辑学生信息
<% 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) {} } %>修改学生信息
<% 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) {} } %>返回学生列表
学生信息列表
添加学生 <% 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() + " | ||||
浙公网安备 33010602011771号