index.jsp
<%@ page language="java" import="java.sql.*" pageEncoding="utf-8"%> <%@ page errorPage="error.jsp"%> <html> <head> <title>学生信息管理系统</title> <link rel="stylesheet" type="text/css" href="css/style.css"> </head> <body> <h1>学生信息管理系统</h1> <a href="add.jsp">添加学生信息</a> <br /> <br /> <table style=" 50%;"> <tr> <th>学号</th> <th>姓名</th> <th>性别</th> <th>出生日期</th> <th>地址</th> <th>管理</th> </tr> <% try { Class.forName("com.mysql.cj.jdbc.Driver"); Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/web_lab03?&useSSL=false&serverTimezone=UTC", "root", ""); //使用Statement对象 Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery("select * from student"); /* PreparedStatement stmt = con.prepareStatement("select * from bookinfo"); ResultSet rs = stmt.executeQuery(); */ while (rs.next()) { String uid = rs.getString(1); out.println("<tr><td>" + rs.getString(1) + "</td><td>" + rs.getString(2) + "</td><td>" + rs.getString(3) + "</td><td>" + rs.getString(4) + "</td><td>" + rs.getString(5) + "</td><td><a href='edit.jsp?uid=" + uid + "'>修改</a> <a href='del.jsp?uid=" + uid + "'>删除</a></td></tr>"); } rs.close(); stmt.close(); con.close(); } catch (Exception e) { out.println("Exception:" + e.getMessage()); } %> </table> </body> </html>
add.jsp
<%@ page contentType="text/html; charset=utf-8" import="java.sql.*" errorPage="error.jsp"%> <html> <head> <title>添加学生信息</title> <link rel="stylesheet" type="text/css" href="css/style.css"> </head> <body> <form name="info" action="addsave.jsp" method="post" onsubmit="return check()"> <h2>添加学生信息</h2> <table style=" 50%"> <tr> <th width="30%">学号:</th> <td width="70%"><input name="uid" type="text"></td> </tr> <tr> <th>姓名:</th> <td><input name="name" type="text"></td> </tr> <tr> <th>性别:</th> <td> <input type="radio" name="sex" value="男">男 <input type="radio" name="sex" value="女">女 </td> </tr> <tr> <th>出生日期:</th> <td><input type="date" name="birthday"></td> </tr> <tr> <th>地址:</th> <td><input name="address" type="text"></td> </tr> <tr> <td colspan="2"> <input type="submit" name="submit" value="提交"> <input type="reset" value="重置"> </td> </tr> </table> </form> </body> <script type="text/javascript"> function check(){ if(info.name.value==""||info.name.value==null){ alert("请输入姓名!"); return false; } if(info.sex.value==""||info.sex.value==null){ alert("请选择性别!"); return false; } if(info.birthday.value==""||info.birthday.value==null){ alert("请选择出生日期!"); return false; } if(info.address.value==""||info.address.value==null){ alert("请输入地址!"); return false; } } </script> </html>
addsave.jsp
<%@ page contentType="text/html; charset=utf-8" import="java.sql.*" errorPage="error.jsp"%> <html> <head> <title>添加图书信息</title> <link rel="stylesheet" type="text/css" href="css/style.css"> </head> <body> <% request.setCharacterEncoding("utf-8"); String uid = request.getParameter("uid"); String name = request.getParameter("name"); String sex = request.getParameter("sex"); String birthday = request.getParameter("birthday"); String address = request.getParameter("address"); Class.forName("com.mysql.cj.jdbc.Driver"); Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/web_lab03?&useSSL=false&serverTimezone=UTC", "root", ""); //使用Statement对象 //Statement stmt = con.createStatement(); //String sql = "insert into bookinfo(bookname,author,price) values('" + bookname + "','" + author + "'," + price + ")"; //int i = stmt.executeUpdate(sql); PreparedStatement stmt = con.prepareStatement("insert into student(uid,name,sex,birthday,address) values(?, ?, ?, ?, ?)"); stmt.setString(1, uid); stmt.setString(2, name); stmt.setString(3, sex); stmt.setString(4, birthday); stmt.setString(5, address); int i = stmt.executeUpdate(); if (i == 1) { out.println("<h2>添加成功!</h2><br/>"); out.println("<a href='index.jsp'>返回首页</a>"); } else { out.println("<h2>添加失败!</h2><br/>"); out.println("<a href='add.jsp'>重新添加</a>"); } stmt.close(); con.close(); %> </body> </html>
del.jsp
<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" pageEncoding="utf-8"%> <html> <head> <title>删除学生信息</title> <link rel="stylesheet" type="text/css" href="css/style.css"> </head> <body> <% request.setCharacterEncoding("utf-8"); Class.forName("com.mysql.cj.jdbc.Driver"); Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/web_lab03?&useSSL=false&serverTimezone=UTC", "root", ""); Statement stmt=con.createStatement(); String uid=request.getParameter("uid"); int i=stmt.executeUpdate("delete from student where uid="+uid); if(i==1) { out.println("<h2>删除成功!</h2><br/>"); } else { out.println("<h2>删除失败!</h2><br/>"); } out.println("<a href='index.jsp'>返回首页</a>"); stmt.close(); con.close(); %> </body> </html>
edit.jsp
<%@ page import="java.sql.*" pageEncoding="utf-8" errorPage="error.jsp"%> <html> <head> <title>修改学生信息</title> <link rel="stylesheet" type="text/css" href="css/style.css"> </head> <body> <% request.setCharacterEncoding("utf-8"); String uid = request.getParameter("uid"); Class.forName("com.mysql.cj.jdbc.Driver"); Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/web_lab03?&useSSL=false&serverTimezone=UTC", "root", ""); //Statement stmt = con.createStatement(); //ResultSet rs = stmt.executeQuery("select * from bookinfo where id=" + id); PreparedStatement stmt = con.prepareStatement("select * from student where uid=?"); stmt.setString(1, uid); ResultSet rs = stmt.executeQuery(); rs.next(); %> <form action="editsave.jsp" method="post"> <h2>修改学生信息</h2> <table style="50%"> <tr> <th width="30%">姓名:</th> <td width="70%"><input name="name" type="text" value="<%=rs.getString(2)%>"></td> </tr> <tr> <th>出生日期:</th> <td><input name="birthday" type="date" value="<%=rs.getString(4)%>"></td> </tr> <tr> <th>地址:</th> <td><input name="address" type="text" value="<%=rs.getString(5)%>"></td> </tr> <tr> <td colspan="2"> <input type="hidden" name="uid" value="<%=uid%>"> <input type="submit" value="修改"> <input type="reset"value="重置"> </td> </tr> </table> </form> <% rs.close(); stmt.close(); con.close(); %> </body> </html>
editsave.jsp
<%@ page import="java.sql.*" pageEncoding="utf-8" errorPage="error.jsp"%> <html> <head> <title>修改完成</title> <link rel="stylesheet" type="text/css" href="css/style.css"> </head> <body> <% request.setCharacterEncoding("utf-8"); String uid = request.getParameter("uid"); String name = request.getParameter("name"); String birthday = request.getParameter("birthday"); String address = request.getParameter("address"); System.out.println(uid+name+birthday+address); Class.forName("com.mysql.cj.jdbc.Driver"); Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/web_lab03?&useSSL=false&serverTimezone=UTC", "root", ""); Statement stmt = con.createStatement(); //String sql = "update student set name='" + name + "',birthday='" + birthday + "',address=" + address + " where uid=" + uid; String sql="update student set name='"+ name +"',birthday='"+ birthday +"',address='"+ address +"'where uid='"+ uid +"'"; int i = stmt.executeUpdate(sql); if (i == 1) { out.println("<h2>修改成功!</h2><br/>"); out.println("<a href='index.jsp'>返回首页</a>"); } else { out.println("<h2>修改失败!</h2><br/>"); out.println("<a href='edit.jsp?uid='" + uid + ">重新添加</a>"); } stmt.close(); con.close(); %> </body> </html>
error.jsp
<%@ page language="java" isErrorPage="true" pageEncoding="utf-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>error page</title> </head> <body> 错误信息为:<br/> <%=exception.getMessage()%><br> <%=exception.toString()%> </body> </html>
css
body { text-align: center; } table { width: 400px; border: 1px solid #696969; border-collapse: collapse; margin:0 auto; } th { border: 1px solid #696969; background-color: #FFF8DC; } td { text-align: center; border: 1px solid #696969; height: 50px; background-color: #E0FFFF; } input { font-size: 20px; }
数据库结构截图
程序截图