一,题目:
一、 考试要求:
1登录账号:要求由6到12位字母、数字、下划线组成,只有字母可以开头;(1分)
2登录密码:要求显示“• ”或“*”表示输入位数,密码要求八位以上字母、数字组成。(1分)
3性别:要求用单选框或下拉框实现,选项只有“男”或“女”;(1分)
4学号:要求八位数字组成,前四位为“2018”开头,输入自己学号;(1分)
5姓名:输入自己的姓名;
5电子邮箱:要求判断正确格式[email protected];(1分)
6点击“添加”按钮,将学生个人信息存储到数据库中。(3分)
7可以演示连接上数据库。(2分)
二,设计思路:
1,首先按要求编写出WEB网页。
2,在DB.java文件编写中对数据库进行连接和关闭的操作。
3,在Dao.java文件中编写对 sql 语句的拼接以及执行 sql 语句向数据库中添加东西的函数。
4,先在 .jsp 文件中对表单进行前端校验,将符合要求的传递给Selvlet.java.文件。
5,在Selvlet.java 文件导入DB.java文件和Dao.java文件并对表单进行二次校验。
6,若数据合法,调用DB.java的方法,连接数据库将数据通过参数的形式传递给在Dao.java文件中向数据库中添加东西的函数,将数据添加到数据库。若添加成功给出提示并返回WEB网页。若失败,给出提示,并返回WEB网页。
三,源代码
WEB网页:shangke.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>注册界面</title> <script type="text/javascript"> /*表单校验*/ function check() { f=0; var re = /^[\w\u4e00-\u9fa5]{6,8}$/; //判断字符串是否为数字和字母组合 var myPattern = new RegExp("^[a-zA-Z]"); // 以英文字母开头 var username = document.getElementById("username").value; //alert(username.length); if(!(username.length>5&&username.length<13)){ alert("用户名长度错误!");return false; } else if(!(re.test(username))){ alert("用户名组成内容错误!");return false; }else if(!(myPattern.exec(username))){ alert("用户名开头必须是字母!");return false; } var password = document.getElementById("password").value; if(password.length<8){f++;alert("密码长度错误!");} return false; var xuehao = document.getElementById("xuehao").value; if(xuehao.length!=8){ alert("学号长度错误!");return false; } if(xuehao[0]==‘2‘&&xuehao[1]==‘0‘&&xuehao[2]==‘1‘&&xuehao[3]==‘8‘){f++;} else{ alert("学号格式错误!");return false; } var mail = document.getElementById("mail").value; if(/^([a-zA-Z0-9_-])[email protected]([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/.test(mail)){f++;} else{ alert("邮箱格式错误!");return false; } if(f>1){ alert("添加成功"); return true; } else{ return false; } } </script> </head> <body background="1.jpg"> <form action="UserServlet?method=add" method="post" onsubmit="return check()"><br/><br/><br/><br/><br/><br/><br/> <table align="center"> <tr> <td>登入账号:</td> <td><input type="text" id="username" name="username" value="" ></td> </tr> <tr> <td>登入密码:</td> <td> <input type="password" id="password" name="password" value="" ></td> </tr> <tr> <td>性 别:</td> <td> <select id="sex" name="sex" > <option>--请选择--</option> <option value="男">男</option> <option value="女">女</option> </select> </td> </tr> <tr> <td>姓 名:</td> <td> <input type="text" name="name" value="" ></td> </tr> <tr> <td>学 号:</td> <td> <input type="text" name="xuehao" id="xuehao" value="" ></td> </tr> <tr> <td>电子邮件:</td> <td> <input type="text" id="mail" name="mail" value="" ></td> </tr> <tr> <td>所在学院:</td> <td> <input type="text" id="xueyuan" name="xueyuan" value="" ></td> </tr> <tr> <td>所在系:</td> <td> <input type="text" id="xi" name="xi" value="" ></td> </tr> <tr> <td>所在班级:</td> <td> <input type="text" id="ban" name="ban" value="" ></td> </tr> <tr> <td>入学年份(届):</td> <td> <select id="year" name="year"> <option>--请选择--</option> <option value="2018">2018</option> <option value="2017">2019</option> <option value="2016">2018</option> <option value="2015">2019</option> <option value="2014">2018</option> </select> </td> </tr> <tr> <td>生源地:</td> <td> <select name="address"> <option value ="河北省">河北省</option> <option value ="北京市">北京市</option> <option value ="天津市">天津市</option> <option value ="邱县">邱县</option> </select> </td> </tr> <tr> <td>备注:</td> <td> <input type="text" id="beizhu" name="beizhu" value="" ></td> </tr> <tr> <td> <button type="submit" >添 加</button></td> </tr> </table> </form> </body> </html>
数据库的连接与关闭:DB.java
package DateBase; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class DB { public static String db_url = "jdbc:mysql://localhost:3306/web08?useSSL=false&serverTimezone=UTC&characterEncoding=UTF-8";//?serverTimezone=GMT%2B8 public static String db_user = "root"; public static String db_pass = "root"; public static Connection getConn () { Connection conn = null; try { Class.forName("com.mysql.cj.jdbc.Driver");//加载驱动 conn = DriverManager.getConnection(db_url, db_user, db_pass); } catch (Exception e) { e.printStackTrace(); } return conn; } /** * 关闭连接 * @param state * @param conn */ public static void close (Statement state, Connection conn) { if (state != null) { try { state.close(); } catch (SQLException e) { e.printStackTrace(); } } if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } public static void close (ResultSet rs, Statement state, Connection conn) { if (rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } if (state != null) { try { state.close(); } catch (SQLException e) { e.printStackTrace(); } } if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } public static void main(String[] args) throws SQLException { Connection conn = getConn(); PreparedStatement pstmt = null; ResultSet rs = null; String sql ="select * from USER"; pstmt = conn.prepareStatement(sql); rs = pstmt.executeQuery(); if(rs.next()){ System.out.println("空"); }else{ System.out.println("不空"); } } }
执行sql语句:Dao.java
package Dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.List; import DateBase.DB; import Entity.FileUploadUtils; import Entity.User; import jdk.nashorn.internal.ir.RuntimeNode.Request; public class Dao { public boolean add(String username,String password,String sex,String name,String xuehao,String mail,String xueyuan,String xi,String ban,String year,String address,String beizhu) { String sql = "insert into shangke values (‘" + username + "‘,‘" + password +"‘,‘" + sex +"‘,‘" +name +"‘,‘" + xuehao +"‘,‘" + mail +"‘,‘" +xueyuan +"‘,‘"+xi +"‘,‘"+ban +"‘,‘"+year+"‘,‘"+address +"‘,‘"+beizhu +"‘)"; // 创建数据库链接 Connection conn = DB.getConn(); Statement state = null; boolean f = false; int a = 0; try { state = conn.createStatement(); a = state.executeUpdate(sql); } catch (Exception e) { e.printStackTrace(); } finally { // 关闭z 连接 DB.close(state, conn); } if (a > 0) { f = true; } return f; } }
Selvlet:UserSelvlet.java
package Servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import Entity.User; import Dao.Dao; /** * Servlet implementation class UserServlet */ @WebServlet("/UserServlet") public class UserServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public UserServlet() { super(); // TODO Auto-generated constructor stub } Dao dao = new Dao(); protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("utf-8"); String method = req.getParameter("method"); if ("add".equals(method)) { add(req, resp); //System.out.println("!!!"); } } private void add(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException { // TODO Auto-generated method stub String username = req.getParameter("username"); String password = req.getParameter("password"); String sex = req.getParameter("sex"); String name = req.getParameter("name"); String xuehao = req.getParameter("xuehao"); String mail = req.getParameter("mail"); String xueyuan = req.getParameter("xueyuan"); String xi = req.getParameter("xi"); String ban = req.getParameter("ban"); String year = req.getParameter("year"); String address = req.getParameter("address"); String beizhu = req.getParameter("beizhu"); if(dao.add(username,password,sex,name,xuehao,mail,xueyuan,xi,ban,year,address,beizhu)) { req.setAttribute("message", "注册成功!"); req.getRequestDispatcher("shangke.jsp").forward(req, resp); }else { req.setAttribute("message", "账号重复,请重新输入!"); req.getRequestDispatcher("shangke.jsp").forward(req, resp); } } }
四,运行测试
1,正确添加
2,登入账号校验:
3,密码校验:
4, 学号校验:
5,邮箱校验:
原文地址:https://www.cnblogs.com/20183544-wangzhengshuai/p/11714698.html