add.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%--设置页面的脚本支持语言为java—导入util包中的类—申明编码方式为UTF-8--%> <% request.setCharacterEncoding("UTF-8"); //设置响应的编码为UTF-8 response.setCharacterEncoding("UTF-8"); //设置响应的编码为UTF-8 %> <% String path = request.getContextPath(); //相对Path设置 String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; //相对Path设置 %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <%--文档声明--%> <html> <head> <title>添加学生信息</title> <%--页面标题--%> <body> <script type="text/javascript" language="JavaScript"> //JS function validate() { var dengluid = document.forms[0].dengluid.value; //创建变量name var password = document.forms[0].password.value; var sex = document.forms[0].sex.value; //创建变量teacher //var username = document.forms[0].username.value; var name = document.forms[0].name.value; //创建变量name var number = document.forms[0].number.value; //创建变量teacher var e_mail = document.forms[0].e_mail.value; //创建变量address var collage = document.forms[0].collage.value; //创建变量address var xi = document.forms[0].xi.value; //创建变量address var class1 = document.forms[0].class1.value; //创建变量address var year = document.forms[0].year.value; //创建变量address var shengyuan = document.forms[0].shengyuan.value; //创建变量address var beizhu = document.forms[0].beizhu.value; //创建变量address if (name.length <= 0) { //判断姓名位数,必填 alert("姓名不能为空,请输入姓名!"); return false; } else if (dengluid .length<6||dengluid .length>12) { //判断学号位数,必填 alert("账号为六到十二个字符"); return false; } else if (dengluid[0]<‘A‘||dengluid[0]>‘z‘) { //判断学号位数,必填 alert("登录账号必须字母开头"); return false; } else if (password.length<8) { //判断学号位数,必填 alert("密码必须为八位以上"); return false; } else if (number[3]!=‘8‘) { //判断学号位数,必填 alert("学号必须以2018开头"); return false; } else if (number.length!=8) { //判断学号位数,必填 alert("学号必须为八位"); return false; } else if(!/(\S)+[@]{1}(\S)+[.]{1}(\w)+/.test(e_mail)) { alert("请输入格式正确的 e-mail 地址!"); return false; } else { return true; } //document.getElementById("form").submit(); } </script> </head> <body> <br> <%--换行--%> <center> <h2>添加学生信息</h2> <hr size="1" noshade color="#000000"> <%--横线--%> <form action="insert.jsp" method="post" id="form" onSubmit="return validate()"> <%--跳转到insert.jsp文件,方法为post--%> <table width="800" border="0" align="center"> <%--表格的宽为800,居中对齐--%> <tr> <td>登录账号: <input type="text" name="dengluid"></td> </tr> <tr> <td>登录密码: <input type="password" name="password"></td> </tr> <tr> <td>性别: <select name="sex" οnchange="showUser(this.value)"> <option value="1">男</option> <option value="2">女</option> </select> </td> </tr> <tr> <td>姓名: <input type="text" name="name"></td> </tr> <tr> <td>学号: <input type="text" name="number"></td> </tr> <tr> <td>电子邮箱: <input type="text" name="e_mail"> </td> </tr> <tr> <td>所在学院: <input type="text" name="collage"></td> </tr> <tr> <td>所在系: <input type="text" name="xi"></td> </tr> <tr> <td>所在班级: <input type="text" name="class1"></td> </tr> <tr> <td>入学年份(届): <select name="year" οnchange="showUser(this.value)"> <option value="1">2018</option> <option value="2">2019</option> </select> </td> </tr> <tr> <td>生源地: <input type="text" name="shengyuan"></td> </tr> <tr> <td>备注: <input type="text" name="beizhu"></td> </tr> <tr> <td> <input name="submit" type="submit" value="添加" /> </td> </tr> </table> <p> </p> </form> </center> </body> </html>
insert.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%--设置页面的脚本支持语言为java—导入util包中的类—申明编码方式为UTF-8--%> <%@ page import="java.sql.*"%> <%--导入java中的sql包--%> <%@page import="com.javao.msg.DBUtil"%> <% request.setCharacterEncoding("UTF-8"); //设置响应的编码为UTF-8 response.setCharacterEncoding("UTF-8"); //设置响应的编码为UTF-8 %> <% String path = request.getContextPath(); //相对Path设置 String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; //相对Path设置 %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <%--文档声明--%> <html> <head> <base href="<%=basePath%>"> <%--设置基础路径,basepath为变量--%> <title>添加学生信息</title> <%--页面标题--%> </head> <body> s <% request.setCharacterEncoding("UTF-8"); //设置响应的编码为UTF-8 String dengluid1 = request.getParameter("dengluid"); String password = request.getParameter("password"); String sex = request.getParameter("sex"); String name = request.getParameter("name"); String number = request.getParameter("number"); String e_mail = request.getParameter("e_mail"); String collage = request.getParameter("collage"); String xi = request.getParameter("xi"); String class1 = request.getParameter("class1"); String year = request.getParameter("year"); String shengyuan = request.getParameter("shengyuan"); String beizhu = request.getParameter("beizhu"); Connection conn = null; //定义静态数据库连接 Statement stat = null; ResultSet rs = null; //将rs滞空。 conn = DBUtil.getConnection(); stat = conn.createStatement(); //String sql = ; //向对应的数据字段添加数据 stat.executeUpdate("insert into cld(dengluid,password,sex,name,number,e_mail,collage,xi,class1,year,shengyuan,beizhu) values(‘" + dengluid1 + "‘,‘" + password + "‘,‘" + sex + "‘,‘" + name + "‘,‘" +number + "‘,‘" + e_mail+ "‘,‘" +collage+ "‘,‘" +xi+ "‘,‘" +class1 + "‘,‘" +year+ "‘,‘" +shengyuan+ "‘,‘" +beizhu+ "‘)"); rs = stat.executeQuery("select * from cld"); //查找data表 %> <center> <% if (rs.next()) { out.print("<br><h3>添加成功!</h3>"); } else { out.print("<br><h3>添加失败!</h3>"); } %> </center> </center> <% if (rs != null) { rs.close(); //关闭结果集,但是rs还是有null值。 rs = null; //将rs滞空。 } if (stat != null) { stat.close(); //关闭stat。 stat = null; //滞空stat。 } if (conn != null) { conn.close(); //关闭数据库连接 conn = null; } %> </body> </html>
show.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%--设置页面的脚本支持语言为java—导入util包中的类—申明编码方式为UTF-8--%> <%@ page import="java.sql.*"%> <%--导入java中的sql包--%> <%@page import="com.javao.msg.DBUtil"%> <% request.setCharacterEncoding("UTF-8"); //设置响应的编码为UTF-8 response.setCharacterEncoding("UTF-8"); //设置响应的编码为UTF-8 %> <% String path = request.getContextPath(); //相对Path设置 String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; //相对Path设置 %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <%--文档声明--%> <html> <head> <title>添加学生信息</title> <%--页面标题--%> </head> <body> <% String dengluid1 = request.getParameter("dengluid"); String password = request.getParameter("password"); String sex = request.getParameter("sex"); String name = request.getParameter("name"); String number = request.getParameter("number"); String e_mail = request.getParameter("e_mail"); String collage = request.getParameter("collage"); String xi = request.getParameter("xi"); String class1 = request.getParameter("class1"); String year = request.getParameter("year"); String shengyuan = request.getParameter("shengyuan"); String beizhu = request.getParameter("beizhu"); Connection conn = null; //定义静态数据库连接 Statement stat = null; ResultSet rs = null; //将rs滞空。 conn = DBUtil.getConnection(); stat = conn.createStatement(); rs = stat.executeQuery("select * from cld"); //查找data表 %> <br> <h2>用户信息</h2> <%--标题样式2--%> <hr noshade> <br> <h3>全部用户信息如下</h3> <%--标题样式3--%> <table width="1000" border="100" cellSpacing=3 style="font-size: 15pt; border: dashed 1pt"> <%--表格宽度450--%> <tr> <td width="110">登录账号</td> <td width="110">登录密码</td> <td width="211">性别</td> <td width="211">姓名</td> <td width="110">学号</td> <td width="110">电子邮件</td> <td width="110">所在学院</td> <td width="110">所在系</td> <td width="110">所在班级</td> <td width="110">入学年份(届)</td> <td width="110">生源地</td> <td width="110">备注</td> </tr> <% while (rs.next()) { out.print("<tr>"); out.print("<td>" + rs.getString("dengluid") + "</td>"); //输出name内容 out.print("<td>" + rs.getString("password") + "</td>"); //输出gender内容 out.print("<td>" + rs.getString("sex") + "</td>"); out.print("<td>" + rs.getString("name") + "</td>"); //输出major内容 out.print("<td>" + rs.getString("number") + "</td>"); //输出major内容 out.print("<td>" + rs.getString("e_mail") + "</td>"); //输出major内容 out.print("<td>" + rs.getString("collage") + "</td>"); out.print("<td>" + rs.getString("xi") + "</td>"); out.print("<td>" + rs.getString("class1") + "</td>"); out.print("<td>" + rs.getString("year") + "</td>"); out.print("<td>" + rs.getString("shengyuan") + "</td>"); out.print("<td>" + rs.getString("beizhu") + "</td>"); //输出major内容 %> <td><a href="delete.jsp?username=<%=rs.getString("dengluid")%>">删除</a></td> <td><a href="update.jsp?username=<%=rs.getString("dengluid")%>">修改</a></td> <% out.print("</tr>"); } %> </table> <br> <form action="select_for_name.jsp" method="post"> <%--post方法跳转到select_for_age.jsp文件--%> <h3> 按菜品名称查询: <input type="text" name="name" value="" title="姓名"></input> <input type="submit" value="查询" /> <br> </h3> </form> <form action="select_for_money.jsp" method="post"> <%--post方法跳转到select_for_gender.jsp文件--%> <h3> 按菜品价格查询: <input type="text" name="money" value="" title="菜品价格不能为空"></input> <input type="submit" value="查询" /> <br> </h3> </form> <form action="select_for_id.jsp" method="post"> <%--post方法跳转到select_for_major.jsp文件--%> <h3> 按菜品编号查询: <input type="text" name="id" value="" title="菜品编号不能为空"></input> <input type="submit" value="查询" /> <br> </h3> </form> <form action="select_for_time.jsp" method="post"> <%--post方法跳转到select_for_major.jsp文件--%> <h3> 按菜品时间查询: <input type="text" name="time" value="" title="菜品时间不能为空"></input> <input type="submit" value="查询" /> <br> </h3> </form> <a href=add.jsp>返回添加课程信息页面</a> <% if (rs != null) { rs.close(); //关闭结果集,但是rs还是有null值。 rs = null; //将rs滞空。 } if (stat != null) { stat.close(); //关闭stat。 stat = null; //滞空stat。 } if (conn != null) { conn.close(); //关闭数据库连接 conn = null; } %> </body> </html>
NewFile.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"> <title>MyFirstWeb</title> </head> <body> <h1>Hello World!</h1> </body> </html>
DBUtil.java
package com.javao.msg; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class DBUtil { public static Connection getConnection() { try { // 1 加载驱动 Class.forName("com.mysql.cj.jdbc.Driver"); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } String user = "root"; String password = "MM19990920"; String url = "jdbc:mysql://localhost:3306/cld?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false"; Connection connection = null; try { // 2 创建链接对象connection connection = DriverManager.getConnection(url, user, password); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return connection; } // 关闭资源的方法 public static void close(Connection connection) { try { if (connection != null) { connection.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void close(PreparedStatement preparedStatement) { try { if (preparedStatement != null) { preparedStatement.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void close(ResultSet resultSet) { try { if (resultSet != null) { resultSet.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
原文地址:https://www.cnblogs.com/MoooJL/p/11717052.html
时间: 2024-09-30 03:39:09