这是第一次用纯jsp写代码,写篇博客纪念一下
项目地址:https://github.com/XiaoGao128/StuManager
查看效果:http://123.56.137.134/StuManager/welcome.jsp
通过jsp实现对数据库的增删改查。页面通过bootstarp和jquery实现,嵌入Java代码对数据库进行访问
首页:两个板块都预加载并隐藏,点击会显示出添加或浏览界面
1 <%@page import="java.sql.ResultSet"%> 2 <%@page import="java.sql.Statement"%> 3 <%@page import="java.sql.DriverManager"%> 4 <%@page import="java.sql.Connection"%> 5 <%@ page language="java" contentType="text/html; charset=utf-8" 6 pageEncoding="utf-8"%> 7 <!DOCTYPE html> 8 <html> 9 <head> 10 <title>学生管理系统</title> 11 <link rel="stylesheet" 12 href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" 13 integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" 14 crossorigin="anonymous"> 15 <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" 16 integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" 17 crossorigin="anonymous"></script> 18 <script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script> 19 <script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script> 20 <script 21 src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" 22 integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" 23 crossorigin="anonymous"></script> 24 <script 25 src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js" 26 integrity="sha384-OgVRvuATP1z7JjHLkuOU7Xw704+h835Lr+6QL9UvYjZE3Ipu6Tp75j7Bh/kR0JKI" 27 crossorigin="anonymous"></script> 28 29 <link rel="stylesheet" type="text/css" href="dashboard.css"> 30 <script type="text/javascript"> 31 $(document).ready(function() { 32 $("#messagetable").hide(); 33 $("#addtable").hide(); 34 $(".rtmenu").click(function() { 35 $(".rtmenu").removeClass("active"); 36 $(this).addClass("active"); 37 }); 38 $("#look").click(function(){ 39 //$("#messagetable").show(); 40 $("#addtable").hide(); 41 $("#messagetable").fadeIn("slow"); 42 $("#hh5").text("学生信息列表"); 43 }); 44 $("#add").click(function(){ 45 $("#messagetable").hide(); 46 $("#addtable").fadeIn("slow"); 47 $("#hh5").text("添加学生信息"); 48 }); 49 $("form").submit(function(event){ 50 if($("#name").val()==""||$("#place").val()==""||$("#birthday").val()==""){ 51 event.preventDefault(); 52 alert("请将表单填写完整!"); 53 } 54 }); 55 }); 56 57 </script> 58 </head> 59 60 <body style="padding-top: 65px; overflow: hidden;"> 61 62 <div class="container-fluid"> 63 <div class="col-md-12"> 64 <nav class="navbar navbar-expand-sm bg-dark navbar-dark fixed-top"> 65 <div class="col-md-4"></div> 66 <a class="navbar-brand col-md-4 " href="#" 67 style="text-align: center;">学生管理系统</a> 68 <div class="col-md-4"></div> 69 </nav> 70 </div> 71 <div class="row"> 72 <div class="col-md-2"> 73 <nav id="sidebarMenu" 74 class="col-md-2 fixed-left d-md-block bg-light sidebar collapse"> 75 <div class="sidebar-sticky pt-3"> 76 <h6 77 class="sidebar-heading d-flex justify-content-between align-items-center px-3 mt-4 mb-1 text-muted"> 78 <span>20183732 高梦涛</span> 79 </h6> 80 <ul class="list-group"> 81 <li class="list-group-item" ><a class="nav-link rtmenu" id="look" 82 href="#">浏览学生信息<span class="sr-only">(current)</span> 83 </a></li> 84 <li class="list-group-item "><a class="nav-link rtmenu" id="add" 85 href="#">添加学生信息 </a></li> 86 </ul> 87 </div> 88 </nav> 89 </div> 90 <div class="col-md-10" align="left"> 91 <h5 id="hh5" class="mx-auto font-weight-normal text-secondary text-muted">尊敬的访客,小高欢迎你……</h5> 92 <table id="messagetable" class="table table-hover" > 93 <thead class="thead-light"> 94 <tr> 95 <th>姓名</th> 96 <th>性别</th> 97 <th>出生日期</th> 98 <th>所在地</th> 99 <th>操作</th> 100 </tr> 101 </thead> 102 <tbody> 103 <% 104 try { 105 Connection con = null; 106 String driverName = "com.mysql.cj.jdbc.Driver"; 107 String dbURL = "jdbc:mysql://localhost:3306/studentmanagement" 108 + "?useSSL=false&serverTimezone=UTC&useUnicode=yes&characterEncoding=utf8"; 109 String userName = "root"; 110 String userPwd = "123456"; 111 try { 112 Class.forName(driverName); 113 System.out.println("加载驱动成功!"); 114 } catch (Exception e) { 115 e.printStackTrace(); 116 System.out.println("加载驱动失败!"); 117 } 118 try { 119 con = DriverManager.getConnection(dbURL, userName, userPwd); 120 System.out.println("连接数据库成功!"); 121 } catch (Exception e) { 122 e.printStackTrace(); 123 System.out.print("mysql连接失败!"); 124 } 125 //使用Statement对象 126 Statement stmt = con.createStatement(); 127 ResultSet rs = stmt.executeQuery("select * from student"); 128 129 /* 130 PreparedStatement stmt = con.prepareStatement("select * from bookinfo"); 131 ResultSet rs = stmt.executeQuery(); 132 */ 133 while (rs.next()) { 134 int id = rs.getInt(1); 135 out.println("<tr><td>" + rs.getString(2) + "</td><td>" + rs.getString(3) + "</td><td>" 136 + rs.getString(4)+ "</td><td>" + rs.getString(5) + "</td><td><a href=‘edit.jsp?id=" + id 137 + "‘>修改</a> <a href=‘del.jsp?id=" + id + "‘ id=‘del‘>删除</a></td></tr>"); 138 } 139 rs.close(); 140 stmt.close(); 141 con.close(); 142 } catch (Exception e) { 143 out.println("Exception:" + e.getMessage()); 144 } 145 %> 146 </tbody> 147 </table> 148 <form action="add.jsp" class="form-group"> 149 <table id="addtable" class="table table-hover table-bordered" > 150 <thead class="thead-light"> 151 <tr> 152 <th>类目</th> 153 <th>值</th> 154 </tr> 155 </thead> 156 <tr><td>姓名</td><td><input id="name" type="text" name="name" class="form-control"></td></tr> 157 <tr><td>性别</td><td><input type="radio" name="sex" value="男" >男 <input type="radio" name="sex" value="女">女</td></tr> 158 <tr><td>出生日期</td><td><input id="birthday" name="birthday" type="text" class="form-control"></td></tr> 159 <tr><td>所在地域</td><td><input id="place" name="place" type="text" class="form-control"></td></tr> 160 <tr><td colspan="2" align="center"><input type="submit" class="btn-primary" value="提交"> 161 162 ; <input type="reset" class="btn-secondary" value="重置"></td></tr> 163 </table> 164 </form> 165 </div> 166 </div> 167 </div> 168 </body> 169 </html>
通过点击修改或删除跳转到相应的页面进行操作
1 <%@page import="java.sql.ResultSet"%> 2 <%@page import="java.sql.Statement"%> 3 <%@page import="java.sql.DriverManager"%> 4 <%@page import="java.sql.Connection"%> 5 <%@ page language="java" contentType="text/html; charset=utf-8" 6 pageEncoding="utf-8"%> 7 <!DOCTYPE html> 8 <html> 9 <head> 10 <title>学生管理系统</title> 11 <link rel="stylesheet" 12 href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" 13 integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" 14 crossorigin="anonymous"> 15 <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" 16 integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" 17 crossorigin="anonymous"></script> 18 <script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script> 19 <script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script> 20 <script 21 src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" 22 integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" 23 crossorigin="anonymous"></script> 24 <script 25 src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js" 26 integrity="sha384-OgVRvuATP1z7JjHLkuOU7Xw704+h835Lr+6QL9UvYjZE3Ipu6Tp75j7Bh/kR0JKI" 27 crossorigin="anonymous"></script> 28 29 <link rel="stylesheet" type="text/css" href="dashboard.css"> 30 <script type="text/javascript"> 31 $(document).ready(function() { 32 $("#messagetable").hide(); 33 $("#addtable").hide(); 34 $(".rtmenu").click(function() { 35 $(".rtmenu").removeClass("active"); 36 $(this).addClass("active"); 37 }); 38 $("#look").click(function(){ 39 //$("#messagetable").show(); 40 $("#addtable").hide(); 41 $("#messagetable").fadeIn("slow"); 42 $("#hh5").text("学生信息列表"); 43 $("#edittable").hide(); 44 }); 45 $("#add").click(function(){ 46 $("#messagetable").hide(); 47 $("#addtable").fadeIn("slow"); 48 $("#hh5").text("添加学生信息"); 49 $("#edittable").hide(); 50 }); 51 52 }); 53 54 </script> 55 </head> 56 57 <body style="padding-top: 65px; overflow: hidden;"> 58 59 <div class="container-fluid"> 60 <div class="col-md-12"> 61 <nav class="navbar navbar-expand-sm bg-dark navbar-dark fixed-top"> 62 <div class="col-md-4"></div> 63 <a class="navbar-brand col-md-4 " href="#" 64 style="text-align: center;">学生管理系统</a> 65 <div class="col-md-4"></div> 66 </nav> 67 </div> 68 <div class="row"> 69 <div class="col-md-2"> 70 <nav id="sidebarMenu" 71 class="col-md-2 fixed-left d-md-block bg-light sidebar collapse"> 72 <div class="sidebar-sticky pt-3"> 73 <h6 74 class="sidebar-heading d-flex justify-content-between align-items-center px-3 mt-4 mb-1 text-muted"> 75 <span>20183732 高梦涛</span> 76 </h6> 77 <ul class="list-group"> 78 <li class="list-group-item" ><a class="nav-link rtmenu" id="look" 79 href="#">浏览学生信息<span class="sr-only">(current)</span> 80 </a></li> 81 <li class="list-group-item "><a class="nav-link rtmenu" id="add" 82 href="#">添加学生信息 </a></li> 83 </ul> 84 </div> 85 </nav> 86 </div> 87 <div class="col-md-10" align="left"> 88 <h5 id="hh5" class="mx-auto font-weight-normal text-secondary text-muted">修改学生信息</h5> 89 <table id="messagetable" class="table table-hover" > 90 <thead class="thead-light"> 91 <tr> 92 <th>姓名</th> 93 <th>性别</th> 94 <th>出生日期</th> 95 <th>所在地</th> 96 <th>操作</th> 97 </tr> 98 </thead> 99 <tbody> 100 <% 101 try { 102 Connection con = null; 103 String driverName = "com.mysql.cj.jdbc.Driver"; 104 String dbURL = "jdbc:mysql://localhost:3306/studentmanagement" 105 + "?useSSL=false&serverTimezone=UTC&useUnicode=yes&characterEncoding=utf8"; 106 String userName = "root"; 107 String userPwd = "123456"; 108 try { 109 Class.forName(driverName); 110 System.out.println("加载驱动成功!"); 111 } catch (Exception e) { 112 e.printStackTrace(); 113 System.out.println("加载驱动失败!"); 114 } 115 try { 116 con = DriverManager.getConnection(dbURL, userName, userPwd); 117 System.out.println("连接数据库成功!"); 118 } catch (Exception e) { 119 e.printStackTrace(); 120 System.out.print("mysql连接失败!"); 121 } 122 //使用Statement对象 123 Statement stmt = con.createStatement(); 124 ResultSet rs = stmt.executeQuery("select * from student"); 125 126 /* 127 PreparedStatement stmt = con.prepareStatement("select * from bookinfo"); 128 ResultSet rs = stmt.executeQuery(); 129 */ 130 while (rs.next()) { 131 int id = rs.getInt(1); 132 out.println("<tr><td>" + rs.getString(2) + "</td><td>" + rs.getString(3) + "</td><td>" 133 + rs.getString(4)+ "</td><td>" + rs.getString(5) + "</td><td><a href=‘edit.jsp?id=" + id 134 + "‘>修改</a> <a href=‘del.jsp?id=" + id + "‘>删除</a></td></tr>"); 135 } 136 rs.close(); 137 stmt.close(); 138 con.close(); 139 } catch (Exception e) { 140 out.println("Exception:" + e.getMessage()); 141 } 142 %> 143 </tbody> 144 </table> 145 <form action="add.jsp" class="form-group"> 146 <table id="addtable" class="table table-hover table-bordered" > 147 <thead class="thead-light"> 148 <tr> 149 <th>类目</th> 150 <th>值</th> 151 </tr> 152 </thead> 153 <tr><td>姓名</td><td><input id="name" type="text" name="name" class="form-control"></td></tr> 154 <tr><td>性别</td><td><input type="radio" name="sex" value="男" >男 <input type="radio" name="sex" value="女">女</td></tr> 155 <tr><td>出生日期</td><td><input id="birthday" name="birthday" type="text" class="form-control"></td></tr> 156 <tr><td>所在地域</td><td><input id="place" name="place" type="text" class="form-control"></td></tr> 157 <tr><td colspan="2" align="center"><input type="submit" class="btn-primary" value="提交"> 158 159 ; <input type="reset" class="btn-secondary" value="重置"></td></tr> 160 </table> 161 </form> 162 <% int iid=Integer.parseInt(request.getParameter("id"));%> 163 <form action="editsave.jsp" class="form-group"> 164 <table id="edittable" class="table table-hover table-bordered" > 165 <thead class="thead-light"> 166 <tr> 167 <th>类目</th> 168 <th>值</th> 169 </tr> 170 </thead> 171 <% 172 173 try { 174 Connection con = null; 175 String driverName = "com.mysql.cj.jdbc.Driver"; 176 String dbURL = "jdbc:mysql://localhost:3306/studentmanagement" 177 + "?useSSL=false&serverTimezone=UTC&useUnicode=yes&characterEncoding=utf8"; 178 String userName = "root"; 179 String userPwd = "123456"; 180 try { 181 Class.forName(driverName); 182 System.out.println("加载驱动成功!"); 183 } catch (Exception e) { 184 e.printStackTrace(); 185 System.out.println("加载驱动失败!"); 186 } 187 try { 188 con = DriverManager.getConnection(dbURL, userName, userPwd); 189 System.out.println("连接数据库成功!"); 190 } catch (Exception e) { 191 e.printStackTrace(); 192 System.out.print("mysql连接失败!"); 193 } 194 //使用Statement对象 195 Statement stmt = con.createStatement(); 196 ResultSet rsst=stmt.executeQuery("select * from student where sno="+iid); 197 rsst.next(); 198 out.println("<input type=‘text‘ hidden=‘true‘ name=‘id‘ value="+rsst.getInt("sno")+">"+"<tr><td>姓名</td><td><input id=‘name‘ type=‘text‘ name=‘name‘ class=‘form-control‘ value=‘"+ 199 rsst.getString("name")+"‘></td></tr><tr><td>性别</td><td><input type=‘text‘ name=‘sex‘ value="+rsst.getString("gender")+" ></td></tr>" 200 +"<tr><td>出生日期</td><td><input id=‘birthday‘ name=‘birthday‘ type=‘text‘ class=‘form-control‘ value="+rsst.getString("birthday")+"></td></tr>" 201 +"<tr><td>所在地域</td><td><input id=‘place‘ name=‘place‘ type=‘text‘ class=‘form-control‘ value="+rsst.getString("address")+"></td></tr>" 202 +"<tr><td colspan=‘2‘ align=‘center‘><input type=‘submit‘ class=‘btn-primary‘ value=‘提交‘> " 203 +" " 204 +" <input type=‘reset‘ class=‘btn-secondary‘ value=‘重置‘></td></tr>"); 205 rsst.close(); 206 stmt.close(); 207 con.close(); 208 } catch (Exception e) { 209 out.println("Exception:" + e.getMessage()); 210 } 211 %> 212 213 </table> 214 </form> 215 </div> 216 </div> 217 </div> 218 </body> 219 </html>
1 <%@page import="java.sql.ResultSet"%> 2 <%@page import="java.sql.Statement"%> 3 <%@page import="java.sql.DriverManager"%> 4 <%@page import="java.sql.Connection"%> 5 <%@ page language="java" contentType="text/html; charset=utf-8" 6 pageEncoding="utf-8"%> 7 <!DOCTYPE html> 8 <html> 9 <head> 10 <title>学生管理系统</title> 11 <link rel="stylesheet" 12 href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" 13 integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" 14 crossorigin="anonymous"> 15 <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" 16 integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" 17 crossorigin="anonymous"></script> 18 <script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script> 19 <script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script> 20 <script 21 src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" 22 integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" 23 crossorigin="anonymous"></script> 24 <script 25 src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js" 26 integrity="sha384-OgVRvuATP1z7JjHLkuOU7Xw704+h835Lr+6QL9UvYjZE3Ipu6Tp75j7Bh/kR0JKI" 27 crossorigin="anonymous"></script> 28 29 <link rel="stylesheet" type="text/css" href="dashboard.css"> 30 </head> 31 <script type="text/javascript"> 32 if(!confirm("确认删除?")){ 33 window.location.href="welcome.jsp"; 34 } 35 </script> 36 <% 37 String name=request.getParameter("name"); 38 String sex=request.getParameter("sex"); 39 String birth=request.getParameter("birthday"); 40 String place=request.getParameter("place"); 41 try { 42 Connection con = null; 43 String driverName = "com.mysql.cj.jdbc.Driver"; 44 String dbURL = "jdbc:mysql://localhost:3306/studentmanagement" 45 + "?useSSL=false&serverTimezone=UTC&useUnicode=yes&characterEncoding=utf8"; 46 String userName = "root"; 47 String userPwd = "123456"; 48 try { 49 Class.forName(driverName); 50 System.out.println("加载驱动成功!"); 51 } catch (Exception e) { 52 e.printStackTrace(); 53 System.out.println("加载驱动失败!"); 54 } 55 try { 56 con = DriverManager.getConnection(dbURL, userName, userPwd); 57 System.out.println("连接数据库成功!"); 58 } catch (Exception e) { 59 e.printStackTrace(); 60 System.out.print("mysql连接失败!"); 61 } 62 //使用Statement对象 63 Statement stmt = con.createStatement(); 64 stmt.executeUpdate("delete from student where sno="+request.getParameter("id")); 65 stmt.close(); 66 con.close(); 67 } catch (Exception e) { 68 out.println("Exception:" + e.getMessage()); 69 } 70 %> 71 <body style="padding-top: 65px; overflow: hidden;"> 72 <h5>删除成功!</h5><a a href="welcome.jsp">点击返回</a> 73 </body> 74 </html>
1 <%@page import="java.sql.ResultSet"%> 2 <%@page import="java.sql.Statement"%> 3 <%@page import="java.sql.DriverManager"%> 4 <%@page import="java.sql.Connection"%> 5 <%@ page language="java" contentType="text/html; charset=utf-8" 6 pageEncoding="utf-8"%> 7 <!DOCTYPE html> 8 <html> 9 <head> 10 <title>学生管理系统</title> 11 <link rel="stylesheet" 12 href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" 13 integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" 14 crossorigin="anonymous"> 15 <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" 16 integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" 17 crossorigin="anonymous"></script> 18 <script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script> 19 <script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script> 20 <script 21 src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" 22 integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" 23 crossorigin="anonymous"></script> 24 <script 25 src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js" 26 integrity="sha384-OgVRvuATP1z7JjHLkuOU7Xw704+h835Lr+6QL9UvYjZE3Ipu6Tp75j7Bh/kR0JKI" 27 crossorigin="anonymous"></script> 28 29 <link rel="stylesheet" type="text/css" href="dashboard.css"> 30 </head> 31 <% 32 String name=request.getParameter("name"); 33 String sex=request.getParameter("sex"); 34 String birth=request.getParameter("birthday"); 35 String place=request.getParameter("place"); 36 try { 37 Connection con = null; 38 String driverName = "com.mysql.cj.jdbc.Driver"; 39 String dbURL = "jdbc:mysql://localhost:3306/studentmanagement" 40 + "?useSSL=false&serverTimezone=UTC&useUnicode=yes&characterEncoding=utf8"; 41 String userName = "root"; 42 String userPwd = "123456"; 43 try { 44 Class.forName(driverName); 45 System.out.println("加载驱动成功!"); 46 } catch (Exception e) { 47 e.printStackTrace(); 48 System.out.println("加载驱动失败!"); 49 } 50 try { 51 con = DriverManager.getConnection(dbURL, userName, userPwd); 52 System.out.println("连接数据库成功!"); 53 } catch (Exception e) { 54 e.printStackTrace(); 55 System.out.print("mysql连接失败!"); 56 } 57 //使用Statement对象 58 Statement stmt = con.createStatement(); 59 System.out.print("update student set name=‘"+name+"‘,gender=‘"+sex+"‘,birthday=‘"+birth+"‘,address=‘"+place+"‘ where sno="+request.getParameter("id")); 60 61 stmt.executeUpdate("update student set name=‘"+name+"‘,gender=‘"+sex+"‘,birthday=‘"+birth+"‘,address=‘"+place+"‘ where sno="+request.getParameter("id")); 62 stmt.close(); 63 con.close(); 64 } catch (Exception e) { 65 out.println("Exception:" + e.getMessage()); 66 } 67 %> 68 <body style="padding-top: 65px; overflow: hidden;"> 69 <h5>提交成功!</h5><a a href="welcome.jsp">点击返回</a> 70 </body> 71 </html>
填写信息,通过jq判断信息完整性
1 <%@page import="java.sql.ResultSet"%> 2 <%@page import="java.sql.Statement"%> 3 <%@page import="java.sql.DriverManager"%> 4 <%@page import="java.sql.Connection"%> 5 <%@ page language="java" contentType="text/html; charset=utf-8" 6 pageEncoding="utf-8"%> 7 <!DOCTYPE html> 8 <html> 9 <head> 10 <title>学生管理系统</title> 11 <link rel="stylesheet" 12 href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" 13 integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" 14 crossorigin="anonymous"> 15 <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" 16 integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" 17 crossorigin="anonymous"></script> 18 <script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script> 19 <script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script> 20 <script 21 src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" 22 integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" 23 crossorigin="anonymous"></script> 24 <script 25 src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js" 26 integrity="sha384-OgVRvuATP1z7JjHLkuOU7Xw704+h835Lr+6QL9UvYjZE3Ipu6Tp75j7Bh/kR0JKI" 27 crossorigin="anonymous"></script> 28 29 <link rel="stylesheet" type="text/css" href="dashboard.css"> 30 </head> 31 <% 32 String name=request.getParameter("name"); 33 String sex=request.getParameter("sex"); 34 String birth=request.getParameter("birthday"); 35 String place=request.getParameter("place"); 36 try { 37 Connection con = null; 38 String driverName = "com.mysql.cj.jdbc.Driver"; 39 String dbURL = "jdbc:mysql://localhost:3306/studentmanagement" 40 + "?useSSL=false&serverTimezone=UTC&useUnicode=yes&characterEncoding=utf8"; 41 String userName = "root"; 42 String userPwd = "123456"; 43 try { 44 Class.forName(driverName); 45 System.out.println("加载驱动成功!"); 46 } catch (Exception e) { 47 e.printStackTrace(); 48 System.out.println("加载驱动失败!"); 49 } 50 try { 51 con = DriverManager.getConnection(dbURL, userName, userPwd); 52 System.out.println("连接数据库成功!"); 53 } catch (Exception e) { 54 e.printStackTrace(); 55 System.out.print("mysql连接失败!"); 56 } 57 //使用Statement对象 58 Statement stmt = con.createStatement(); 59 System.out.print("insert into student(name,gender,birthday,address) values(‘"+name+"‘,‘"+sex+"‘,‘"+birth+"‘,‘"+place+"‘)"); 60 61 stmt.executeUpdate("insert into student(name,gender,birthday,address) values(‘"+name+"‘,‘"+sex+"‘,‘"+birth+"‘,‘"+place+"‘)"); 62 stmt.close(); 63 con.close(); 64 } catch (Exception e) { 65 out.println("Exception:" + e.getMessage()); 66 } 67 %> 68 <body style="padding-top: 65px; overflow: hidden;"> 69 <h5>提交成功!</h5><a a href="welcome.jsp">点击返回</a> 70 </body> 71 </html>