在实验开始前要导入相应的jar包。以及相关的library。
bean.studentBean.java
package bean; import java.sql.ResultSet; import java.util.ArrayList; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; import DBuntil.Dbutil; public class studentBean { private String name; private String id; private String sex; private String birthday; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getBirthday() { return birthday; } public void setBirthday(String birthday) { this.birthday = birthday; } /** * 从BookInfo表中获取所有的图书信息 * * @return BookInfo的数组 */ public static ArrayList<studentBean> getBookList() { ArrayList<studentBean> list = new ArrayList<studentBean>(); String sql = "select * from students"; Dbutil jdbc = new Dbutil(); ResultSet rs = jdbc.executeQuery(sql); try { while (rs.next()) { studentBean bi = new studentBean(); bi.setId(rs.getString("id")); bi.setName(rs.getString("name")); bi.setSex(rs.getString("sex")); bi.setBirthday(rs.getString("birthday")); list.add(bi); } rs.close(); } catch (SQLException e) { e.printStackTrace(); } jdbc.close(); return list; } /** * 获取指定id的图书信息 * * @param id 图书id * @return 一个BookInfo对象 */ public static studentBean getStudentById(String id) { String sql = "select * from students where id=" + id; Dbutil jdbc = new Dbutil(); ResultSet rs = jdbc.executeQuery(sql); studentBean bi = new studentBean(); try { if (rs.next()) { bi.setId(rs.getString("id")); bi.setName(rs.getString("name")); bi.setSex(rs.getString("sex")); bi.setBirthday(rs.getString("birthday")); } rs.close(); } catch (SQLException e) { e.printStackTrace(); } jdbc.close(); return bi; } /** * 更新指定id的图书信息 * * @param bi 要更新的图书的对象 * @return 修改的结果:1代表成功,0代表没有更新 */ public static int updateStudent(studentBean bi) { int result = 0; String sql = "update students set name='" + bi.getName() + "',id='" + bi.getId() + "',sex='" + bi.getSex() + "',birthday='" + bi.getBirthday() + "' where id='" + bi.getId()+"'"; Dbutil jdbc = new Dbutil(); result = jdbc.executeUpdate(sql); System.out.println(sql); jdbc.close(); return result; } /** * 删除指定id的图书 * * @param id 图书id * @return 删除的结果:1代表成功,0代表没有删除 */ public static int deleteStudent(String id) { int result = 0; String sql = "delete from students where id=" + id; Dbutil jdbc = new Dbutil(); result = jdbc.executeUpdate(sql); System.out.println(sql); jdbc.close(); return result; } /** * 增加一本图书 * * @param bi 图书对象 * @return 新增的结果:1代表成功,0代表没有增加 */ public static int addStudent(studentBean bi) { int result = 0; String sql = "insert into students(id,name,sex,birthday) values('" + bi.getId() + "','"+ bi.getName() + "','" + bi.getSex() + "','" + bi.getBirthday() + "')"; Dbutil jdbc = new Dbutil(); result = jdbc.executeUpdate(sql); System.out.println(sql); jdbc.close(); return result; } }
Dbutil.java
package DBuntil; import java.sql.*; public class Dbutil { /** * 完成与数据库的连接和数据的访问 * @author Leiyu * @version 1.0 * */ private String driverStr = "com.mysql.jdbc.Driver"; private String connStr = "jdbc:mysql://localhost:3306/lab03?uerUnicode=true&characterEncoding=UTF-8"; private String dbusername = "root"; private String dbpassword = "123456"; private Connection conn = null; private Statement stmt = null; { try { Class.forName(driverStr); conn = DriverManager.getConnection(connStr, dbusername, dbpassword); stmt = conn.createStatement(); } catch (Exception ex) { System.out.println("数据库连接失败!"); } } /** * 执行更新操作 * @param s * SQL语句 * @return * 更新操作的结果 */ public int executeUpdate(String s) { int result = 0; try { result = stmt.executeUpdate(s); } catch (Exception ex) { System.out.println("更新出现异常!"); } return result; } /** * 执行查询操作 * @param s * SQL语句 * @return * 查询结果 */ public ResultSet executeQuery(String s) { ResultSet rs = null; try { rs = stmt.executeQuery(s); } catch (Exception ex) { System.out.println("查询出现异常!"); } return rs; } /** * 关闭数据库 */ public void close() { try { stmt.close(); conn.close(); } catch (Exception e) { } } }
StudenrtController.java
package servlert; import java.io.IOException; import java.util.ArrayList; import jakarta.servlet.*; import jakarta.servlet.annotation.WebServlet; import jakarta.servlet.http.*; import bean.studentBean; /** * 用来接收客户端的后缀为do的请求 * * @author Leiyu * @version 1.0 * */ @WebServlet("*.do") public class StudentController extends HttpServlet{ private static final long serialVersionUID = 1L; public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); String actionUrl = request.getServletPath(); // 获取客户请求的Servlet地址 if (actionUrl.equals("/index.do")) { // 查询所有学生 ArrayList<studentBean> list = studentBean.getBookList(); // 调用studentBean的getStudentList方法查询所有学生,赋值给list request.setAttribute("list", list); // 在request增加属性list,其结果为list对象 request.getRequestDispatcher("/index.jsp").forward(request, response);// 重定向至index.jsp进行显示 } else if (actionUrl.equals("/addview.do")) { // 新增学生显示页面 request.getRequestDispatcher("add.jsp").forward(request, response); } else if (actionUrl.equals("/add.do")) { // 新增学生 studentBean bi = new studentBean(); String id=request.getParameter("id"); bi.setId(id); String name=request.getParameter("name"); bi.setName(name); String sex=request.getParameter("sex"); bi.setSex(sex); String birthday=request.getParameter("year")+"/"+request.getParameter("month")+"/"+request.getParameter("day"); bi.setBirthday(birthday); int r = studentBean.addStudent(bi); // 调用studentBean的addStudent方法完成 if (r == 1) request.getRequestDispatcher("success.jsp").forward(request, response); // 成功的话重定向至success.jsp else request.getRequestDispatcher("failure.jsp").forward(request, response); // 失败的话重定向至failure.jsp } else if (actionUrl.equals("/edit.do")) { // 客户端要对指定id的学生进行修改 String id = request.getParameter("id"); studentBean bi = studentBean.getStudentById(id); // 调用studentBean的getStudentById方法获取学生信息,赋值给bi对象 request.setAttribute("bi", bi); // 将bi对象增加到request的属性中 request.getRequestDispatcher("/edit.jsp").forward(request, response);// 重定向至edit.jsp进行显示 } else if (actionUrl.equals("/update.do")) { // 用户输入要修改的学生的信息之后需要保存到数据库 studentBean bi = new studentBean(); bi.setId(request.getParameter("id")); bi.setName(request.getParameter("name")); bi.setSex(request.getParameter("sex")); String birthday=request.getParameter("year")+"/"+request.getParameter("month")+"/"+request.getParameter("day"); bi.setBirthday(birthday); int r = studentBean.updateStudent(bi);// 调用studentBean的updateStudent方法实现 if (r == 1) request.getRequestDispatcher("/success.jsp").forward(request, response);// 成功的话重定向至success.jsp else request.getRequestDispatcher("/failure.jsp").forward(request, response);// 失败的话重定向至failure.jsp } else if (actionUrl.equals("/delete.do")) { // 用户需要删除指定id的学生 String id = request.getParameter("id"); int r = studentBean.deleteStudent(id); // 调用studentBean的deleteStudent方法实现 if (r == 1) request.getRequestDispatcher("/success.jsp").forward(request, response);// 成功的话重定向至success.jsp else request.getRequestDispatcher("/failure.jsp").forward(request, response);// 失败的话重定向至failure.jsp } } }
style.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:#9DFCFC; } td { text-align: center; border: 1px solid #696969; height: 50px; background-color:#ff9966; } td.ltd { text-align: left; } input { font-size: 20px; }
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %> <%@ page import="java.util.ArrayList" %> <%@ page import="bean.studentBean" %> <html> <head> <title>图书管理系统</title> <link rel="stylesheet" type="text/css" href="css/style.css"> </head> <body> <h1>图书管理系统</h1> <a href="addview.do">增加图书信息</a> <p /> <table style="width: 50%"> <tr> <th>姓名</th> <th>性别</th> <th>生日</th> <th>管理</th> </tr> <% @SuppressWarnings("unchecked") ArrayList<studentBean> list = studentBean.getBookList(); for (studentBean bi : list) { String id = bi.getId(); %> <tr> <td><%=bi.getName()%></td> <td><%=bi.getSex()%></td> <td><%=bi.getBirthday()%></td> <td><a href="edit.do?id=<%=id%>">修改</a> <a href="delete.do?id=<%=id%>">删除</a></td> </tr> <% } %> </table> <br /> <hr /> <div style="text-align: center; width: 100%; font-size: 12px; color: #333;"> ©版权所有:石家庄铁道大学信息科学与技术学院 <a href="Lab04-1.png" target="_blank">网站地图</a> </div> </body> </html>
add.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>添加学生信息</title> <link rel="stylesheet" type="text/css" href="css/style.css"> </head> <body> <form action="add.do" method="post"> <h2>添加学生信息</h2> <table style="width: 50%"> <tr> <th width="30%">学号:</th> <td width="70%"><input name="id" type="text"></td> </tr> <tr> <th>姓名:</th> <td><input name="name" type="text"></td> </tr> <tr> <th>性别:</th> <td><input name="sex" type="radio" value="男">男 <input name="sex" type="radio" value="女">女 </td> </tr> <tr> <th>生日:</th> <td><input name="year" type="text" style="width:60px;">年<input name="month" type="text" style="width:40px;">月<input name="day" type="text" style="width:40px;">日</td> </tr> <tr> <td colspan="2"><input type="submit" name="submit" value="添加"> <input type="reset" value="重置"></td> </tr> </table> </form> </body> </html>
edit.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %> <%@ page import="bean.studentBean"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>修改学生信息</title> <link rel="stylesheet" type="text/css" href="css/style.css"> </head> <body> <% studentBean bi = (studentBean) request.getAttribute("bi"); String year=null; String month=null; String day=null; String birthday=bi.getBirthday(); String res[]=birthday.split("/"); year=res[0]; month=res[1]; day=res[2]; %> <form action="update.do" method="post"> <input type="hidden" name="id" value="<%=bi.getId()%>"> <table style="width: 50%"> <caption>修改学生信息</caption> <tr> <th width="30%">姓名:</th> <td width="70%"><input name="name" type="text" value="<%=bi.getName()%>"></td> </tr> <tr> <th>性别:</th> <% if ("男".equals(bi.getSex())) { out.println("<td><input name='sex' type='radio' value='男' checked>男 " + "<input name='sex' type='radio' value='女'>女 </td>"); } if ("女".equals(bi.getSex())){ out.println("<td><input name='sex' type='radio' value='男' >男 " + "<input name='sex' type='radio' value='女' checked>女 </td>");} %> </tr> <tr> <th>生日:</th> <th> <input name="year" type="text" style="width:60px;" value="<%=year%>" >年 <input name="month" type="text" style="width:40px;" value="<%=month%>">月 <input name="day" type="text" style="width:40px;" value="<%=day%>">日 </th> </tr> <tr> <th colspan="2"><input type="submit" value="修改"> <input type="reset" value="重置"></th> </tr> </table> </form> </body> </html>
success.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <link rel="stylesheet" type="text/css" href="css/style.css"> <title>操作成功提示</title> </head> <body> <p style="color: #CC1111">操作成功!</p> <a href="index.do">转到主页</a> </body> </html>
failure.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <link rel="stylesheet" type="text/css" href="css/style.css"> <title>Insert title here</title> </head> <body> 操作失败! <a href="javascript:history.back()">返回</a> </body> </html>