web实验四
所花时间:2小时
代码量:如下
博客量:本学期截至目前76篇
了解到的知识点:web实验
BOOK.java:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 | package book.bean; import java.sql.*; import java.util.*; /** * 构造BookInfo对象,对应数据库的BookInfo表 提供了多个静态方法完成BookInfo对象与对应数据库表的所有新增、查询、修改、删除等操作 * * @author Leiyu * @version 1.0 * */ public class Book { private String id; private String num; private String name; private String sex; private String bir; public String getNum() { return num; } public void setNum(String num) { this .num = num; } 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 getBir() { return bir; } public void setBir(String bir) { this .bir = bir; } public void setId(String id) { this .id = id; } public String getId() { return id; } /** * 从BookInfo表中获取所有的图书信息 * * @return BookInfo的数组 */ public static ArrayList<Book> getBookList() { ArrayList<Book> list = new ArrayList<Book>(); String sql = "select * from bookinfo" ; Dao jdbc = new Dao(); ResultSet rs = jdbc.executeQuery(sql); try { while (rs.next()) { Book bi = new Book(); bi.setId(rs.getString( "id" )); bi.setNum(rs.getString( "num" )); bi.setName(rs.getString( "name" )); bi.setSex(rs.getString( "sex" )); bi.setBir(rs.getString( "bir" )); list.add(bi); } rs.close(); } catch (SQLException e) { e.printStackTrace(); } jdbc.close(); return list; } /** * 获取指定id的图书信息 * * @param id 图书id * @return 一个BookInfo对象 */ public static Book getBookById(String id) { String sql = "select * from bookinfo where id=" + id; Dao jdbc = new Dao(); ResultSet rs = jdbc.executeQuery(sql); Book bi = new Book(); try { if (rs.next()) { bi.setId(rs.getString( "id" )); bi.setNum(rs.getString( "num" )); bi.setName(rs.getString( "name" )); bi.setSex(rs.getString( "sex" )); bi.setBir(rs.getString( "bir" )); } rs.close(); } catch (SQLException e) { e.printStackTrace(); } jdbc.close(); return bi; } /** * 更新指定id的图书信息 * * @param bi 要更新的图书的对象 * @return 修改的结果:1代表成功,0代表没有更新 */ public static int updateBook(Book bi) { int result = 0 ; String sql = "update bookinfo set num='" + bi.getNum() + "',name='" + bi.getName() + "',sex='" + bi.getSex() + "',bir='" + bi.getBir() + "' where id=" + bi.getId(); System.out.println(sql); Dao jdbc = new Dao(); result = jdbc.executeUpdate(sql); jdbc.close(); return result; } /** * 删除指定id的图书 * * @param id 图书id * @return 删除的结果:1代表成功,0代表没有删除 */ public static int deleteBook(String id) { int result = 0 ; String sql = "delete from bookinfo where id=" + id; Dao jdbc = new Dao(); result = jdbc.executeUpdate(sql); jdbc.close(); return result; } /** * 增加一本图书 * * @param bi 图书对象 * @return 新增的结果:1代表成功,0代表没有增加 */ public static int addBook(Book bi) { int result = 0 ; String sql = "insert into bookinfo(num,name,sex,bir) values('" + bi.getNum() + "','" + bi.getName() + "','" + bi.getSex() + "','" + bi.getBir() + "')" ; Dao jdbc = new Dao(); System.out.println(sql); result = jdbc.executeUpdate(sql); jdbc.close(); return result; } } |
Dao.java:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | package book.bean; import java.sql.*; /** * 完成与数据库的连接和数据的访问 * @author Leiyu * @version 1.0 * */ public class Dao { private String driverStr = "com.mysql.cj.jdbc.Driver" ; private String connStr = "jdbc:mysql://127.0.0.1:3306/book?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC" ; private String dbusername = "root" ; private String dbpassword = "Wjtssywds123" ; private Connection conn = null ; private Statement stmt = null ; public Dao() { 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) { } } } |
index.html:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 | <html> <head> <title>学生管理系统</title> <meta http-equiv= "Content-Type" content= "text/html; charset=utf-8" /> <link rel= "stylesheet" type= "text/css" href= "css/style.css" > <link rel= "stylesheet" type= "text/css" href= "css/jquery-ui.css" > <script type= "text/javascript" src= "js/jquery-1.7.2.min.js" ></script> <script type= "text/javascript" src= "js/jquery-ui.js" ></script> <script type= "text/javascript" > //通过AJAX方式检索所有的图书 function RetrieveBooks() { $.post( "list.action" , {}, function(data) { $( "#BooksTable tr:gt(0)" ).remove(); for (var i = 0 ; i < data.length; i++) { //插入表行 var trHtml = "<tr id = " +data[i].id + "><td>" + data[i].num + "</td><td>" + data[i].name + "</td><td>" + data[i].sex + "</td><td>" + data[i].bir + "</td><td><a href=\"#\" class=\"updateLink\">修改</a> <a href=\"#\" class=\"deleteLink\">删除</a></td></tr>" ; $( "#BooksTable" ).append(trHtml); } //绑定修改链接 $( ".updateLink" ).click(function() { $.post( "edit.action" , {id:$( this ).closest( "tr" ).attr( "id" )}, function(data){ $( "#UpdateId" ).val(data.id); $( "#UpdateNum" ).val(data.num); $( "#UpdateName" ).val(data.name); $( "#UpdateSex" ).val(data.sex); $( "#UpdateBir" ).val(data.bir); $( "#UpdateDiv" ).dialog( "open" ); }, "json" ); }); //绑定删除链接 $( ".deleteLink" ).click(function(){ $.post( "delete.action" , {id:$( this ).closest( "tr" ).attr( "id" )}, function(data){ if (data== "1" ) { RetrieveBooks(); } else { alert( "删除学生信息失败!" ); } }, "json" ); }); }, "json" ); } $(function() { //设定Ajax提交编码格式为utf-8 $.ajaxSetup({ contentType: "application/x-www-form-urlencoded; charset=utf-8" }); //对“添加图书信息”窗口进行初始化 $( "#AddDiv" ).dialog({ title: "添加学生信息" , autoOpen : false , height : 280 , width : 400 , modal : true , show: "blind" , hide: "fade" , close : function(){ $( "#AddNum" ).val( "" ); $( "#AddName" ).val( "" ); $( "#AddSex" ).val( "" ); $( "#AddBir" ).val( "" ); } }); //对“修改图书信息”窗口进行初始化 $( "#UpdateDiv" ).dialog({ title: "修改学生信息" , autoOpen : false , height : 280 , width : 400 , modal : true , show: "blind" , hide: "fade" , close : function(){ $( "#UpdateId" ).val( "" ); $( "#UpdateNum" ).val( "" ); $( "#UpdateName" ).val( "" ); $( "#UpdateSex" ).val( "" ); $( "#UpdateBir" ).val( "" ); } }); //对添加图书窗口的添加键绑定事件驱动程序 $( "#AddSubmit" ).click(function(){ //提交服务器 $.post( "add.action" , {num:$( "#AddNum" ).val(), name:$( "#AddName" ).val(), sex:$( "#AddSex" ).val(), bir:$( "#AddBir" ).val()}, function(data){ if (data== "1" ) { $( "#AddDiv" ).dialog( "close" ); RetrieveBooks(); } else { $( "#AddTip" ).html( "添加学生信息失败!请重新输入数据。" ); $( "#AddTip" ).show().delay( 5000 ).hide( 0 ); } }, "json" ); }); //对添加图书窗口的添加键绑定事件驱动程序 $( "#UpdateSubmit" ).click(function(){ //提交服务器 // id:$("#UpdateId").val(), $.post( "update.action" , {id:$( "#UpdateId" ).val(),num:$( "#UpdateNum" ).val(),name:$( "#UpdateName" ).val(), sex:$( "#UpdateSex" ).val(),bir:$( "#UpdateBir" ).val()}, function(data){ if (data== "1" ) { $( "#UpdateDiv" ).dialog( "close" ); RetrieveBooks(); } else { $( "#UpdateTip" ).html( "更新学生信息失败!请重新输入数据。" ); $( "#UpdateTip" ).show().delay( 5000 ).hide( 0 ); } }, "json" ); }); //对“新增图书信息”链接绑定事件驱动程序 $( "#AddButton" ).click(function() { $( "#AddDiv" ).dialog( "open" ); }); //第一次加载检索所有书籍 RetrieveBooks(); }); </script> </head> <body> <h1>学生管理系统</h1> <a id= "AddButton" href= "#" >增加学生信息</a> <table style= "width: 50%" id= "BooksTable" > <tr> <th>学号</th> <th>姓名</th> <th>性别</th> <th>生日</th> <th>管理</th> </tr> </table> <div id= "AddDiv" style= "display: hidden" > <form id= "AddForm" > <table style= "width: 350px;" id= "AddTable" > <tr> <th width= "30%" >学号:</th> <td width= "70%" class = "ltd" ><input name= "num" type= "text" id= "AddNum" ></td> </tr> <tr> <th>姓名:</th> <td class = "ltd" ><input name= "name" type= "text" id= "AddName" ></td> </tr> <tr> <th>性别:</th> <td class = "ltd" ><input name= "sex" type= "text" id= "AddSex" ></td> </tr> <tr> <th>生日:</th> <td class = "ltd" ><input name= "bir" type= "date" id= "AddBir" ></td> </tr> <tr> <th colspan= "2" ><input type= "button" value= "添加" id = "AddSubmit" > <input type= "reset" value= "重置" ></th> </tr> </table> </form> <span style= "color:red;" id= "AddTip" ></span> </div> <div id= "UpdateDiv" style= "display: hidden" > <form id= "UpdateForm" > <table style= "width: 350px;" id= "UpdateTable" > <tr> <th width= "30%" >学号:</th> <td width= "70%" class = "ltd" > <input name= "id" type= "hidden" id= "UpdateId" > <input name= "num" type= "text" id= "UpdateNum" ></td> </tr> <tr> <th>姓名:</th> <td class = "ltd" ><input name= "name" type= "text" id= "UpdateName" ></td> </tr> <tr> <th>性别:</th> <td class = "ltd" ><input name= "sex" type= "text" id= "UpdateSex" ></td> </tr> <tr> <th>生日:</th> <td class = "ltd" ><input name= "bir" type= "date" id= "UpdateBir" ></td> </tr> <tr> <th colspan= "2" ><input type= "button" value= "修改" id = "UpdateSubmit" > <input type= "reset" value= "重置" ></th> </tr> </table> </form> <span style= "color:red;" id= "UpdateTip" ></span> </div> <br /> <hr /> <div style= "text-align: center; width: 100%; font-size: 12px; color: #333;" > ©版权所有:石家庄铁道大学信息科学与技术学院 <a href= "Lab04-2.png" target= "_blank" >网站地图</a> </div> </body> </html> |
index.jsp:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | <%@ page pageEncoding= "utf-8" import = "java.util.ArrayList,book.bean.Book" %> <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> <th>管理</th> </tr> <% @SuppressWarnings ( "unchecked" ) ArrayList<Book> list = (ArrayList<Book>) request.getAttribute( "list" ); for (Book bi : list) { String id = bi.getId(); %> <tr> <td><%=bi.getNum()%></td> <td><%=bi.getName()%></td> <td><%=bi.getSex()%></td> <td><%=bi.getBir()%></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> |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)