题目要求:
1登录账号:要求由6到12位字母、数字、下划线组成,只有字母可以开头;(1分)
2登录密码:要求显示“• ”或“*”表示输入位数,密码要求八位以上字母、数字组成。(1分)
3性别:要求用单选框或下拉框实现,选项只有“男”或“女”;(1分)
4学号:要求八位数字组成,前四位为“2018”开头,输入自己学号;(1分)
5姓名:输入自己的姓名;
6点击“添加”按钮,将学生个人信息存储到数据库中。(3分)
7可以演示连接上数据库。(2分)
com.Bean
1 public classUser {2 privateString username;3 privateString password;4 privateString name;5 privateString studentID;6 privateString sex;7 privateString phone;8 privateString email;9 privateString xueyuan;10 privateString xi;11 privateString classes;12 privateString year;13 privateString shengyuandi;14 privateString beizhu;15 publicString getUsername() {16 returnusername;17 }18 public voidsetUsername(String username) {19 this.username =username;20 }21 publicString getPassword() {22 returnpassword;23 }24 public voidsetPassword(String password) {25 this.password =password;26 }27 publicString getName() {28 returnname;29 }30 public voidsetName(String name) {31 this.name =name;32 }33 publicString getStudentID() {34 returnstudentID;35 }36 public voidsetStudentID(String studentID) {37 this.studentID =studentID;38 }39 publicString getSex() {40 returnsex;41 }42 public voidsetSex(String sex) {43 this.sex =sex;44 }45 publicString getPhone() {46 returnphone;47 }48 public voidsetPhone(String phone) {49 this.phone =phone;50 }51 publicString getEmail() {52 returnemail;53 }54 public voidsetEmail(String email) {55 this.email =email;56 }57 publicString getXueyuan() {58 returnxueyuan;59 }60 public voidsetXueyuan(String xueyuan) {61 this.xueyuan =xueyuan;62 }63 publicString getXi() {64 returnxi;65 }66 public voidsetXi(String xi) {67 this.xi =xi;68 }69 publicString getClasses() {70 returnclasses;71 }72 public voidsetClasses(String classes) {73 this.classes =classes;74 }75 publicString getYear() {76 returnyear;77 }78 public voidsetYear(String year) {79 this.year =year;80 }81 publicString getShengyuandi() {82 returnshengyuandi;83 }84 public voidsetShengyuandi(String shengyuandi) {85 this.shengyuandi =shengyuandi;86 }87 publicString getBeizhu() {88 returnbeizhu;89 }90 public voidsetBeizhu(String beizhu) {91 this.beizhu =beizhu;92 }93 publicUser(String username, String password, String name, String studentID, String sex, String phone, String email,94 String xueyuan, String xi, String classes, String year, String shengyuandi, String beizhu) {95 super();96 this.username =username;97 this.password =password;98 this.name =name;99 this.studentID =studentID;100 this.sex =sex;101 this.phone =phone;102 this.email =email;103 this.xueyuan =xueyuan;104 this.xi =xi;105 this.classes =classes;106 this.year =year;107 this.shengyuandi =shengyuandi;108 this.beizhu =beizhu;109 }110
111
112
113 }
数据库驱动加载与连接()
1 packagecom.DBUtil;2
3 importjava.sql.Connection;4
5 importjava.sql.DriverManager;6 //import java.sql.ResultSet;
7 importjava.sql.SQLException;8 //import java.sql.Statement;
9
10 public classUserDBUtil {11 staticConnection con;12 public staticConnection getConnection() {13 try{14 Class.forName("com.mysql.cj.jdbc.Driver");15 System.out.println("数据库驱动加载成功");16 } catch(ClassNotFoundException e) {17 //TODO Auto-generated catch block
18 e.printStackTrace();19 }20
21 try{22 con=DriverManager.getConnection("jdbc:mysql://localhost:3306/user?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B88&serverTimezone=GMT%2B8&useSSL=false","root","101032");23 System.out.println("数据库链接成功!");24 } catch(SQLException e) {25 //TODO Auto-generated catch block
26 e.printStackTrace();27 }28
29 returncon;30 }31
32 public static voidmain(String[] args) {33 UserDBUtil c=newUserDBUtil();34 c.getConnection();35
36 }
出现如图所示输出,说明数据库的驱动加载成功,数据库连接成功。
servlet是运行在web服务器或应用服务器的程序,用来处理客户端请求的动态资源.即后台的服务程序
1 packagecom.Servlet;2
3 importjava.io.IOException;4 importjava.sql.Connection;5 importjava.sql.SQLException;6 importjava.sql.Statement;7 //import java.util.List;
8
9 importjavax.servlet.ServletException;10 importjavax.servlet.http.HttpServlet;11 importjavax.servlet.http.HttpServletRequest;12 importjavax.servlet.http.HttpServletResponse;13
14 //import com.Bean.*;
15 import com.DBUtil.*;16
17 public class AddServlet extendsHttpServlet {18
19 private static final long serialVersionUID = 1L;20
21 protected void service(HttpServletRequest req,HttpServletResponse resp)throwsServletException,IOException {22 req.setCharacterEncoding("UTF-8");23
24 String username = req.getParameter("username");25 String password= req.getParameter("password");26 String name= req.getParameter("name");27 String studnetID = req.getParameter("studnetID");28 String sex= req.getParameter("sex");29 String phone= req.getParameter("phone");30 String email= req.getParameter("email");31 String xueyuan = req.getParameter("xueyuan");32 String xi = req.getParameter("xi");33 String classes = req.getParameter("clsaaes");34 String year = req.getParameter("year");35 String shengyuandi = req.getParameter("shengyuandi");36 String beizhu = req.getParameter("beizhu");37
38 Connection conn =UserDBUtil.getConnection();39 Statement state =null;40 String addsql = null;41 try{42 state =conn.createStatement();43 addsql = "insert into use01 (username,password,name,studnetID,sex,phone,email,xueyuan,xi,clsaaes,year,shengyuandi,beizhu) values ('"+username+"','"+password+"','"+name+"','"+studnetID+"','"+sex+"','"+phone+"','"+email+"','"+xueyuan+"','"+xi+"','"+classes+"','"+year+"','"+shengyuandi+"','"+beizhu+"')";44 state.executeUpdate(addsql);45 conn.close();46 } catch(SQLException e) {47 e.printStackTrace();48 }49 resp.sendRedirect(req.getContextPath()+"/index.jsp");50
51 }52 }
网页的排版编码:
1
2 pageEncoding="UTF-8"%>
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 2014
70 2015
71 2016
72 2017
73 2018
74 2019
75
76
77
78 生源地:
79
80
81
82 备注:
83
84
85
86
87
88
89
90
91
92
93
94 //正则表达式
95 functioncheck(form){96
97 if(form.username.value.length<6 || form.username.value.length>12 || new RegExp("[^0-9a-zA-Z]").test(form.username.value)){98 alert("登陆账号必须由6-12位英文字符或数字组成!");99 form.user.focus();100 return false;101 }102 if(new RegExp("[^a-zA-Z]").test(form.username.value.substring(0,1))){103 alert("登陆账号必须以英文字母开头!");104 form.user.focus();105 return false;106 }107 if(form.password.value.length <= 8 || new RegExp("[^0-9a-zA-Z]").test(form.password.value)){108 alert("密码必须由八位以上字母、数字组成!");109 form.phone.focus();110 return false;111 }112 if(form.studentID.value.length != 8 || new RegExp("[^0-9]").test(form.studentID.value)){113 alert("请输入八位学号!");114 form.phone.focus();115 return false;116 }117
118 if(form.phone.value.length != 11 || new RegExp("[^0-9]").test(form.phone.value)){119 alert("手机号请输入11位数字!");120 form.phone.focus();121 return false;122 }123
124 //以数字字母开头,中间可以是多个数字字母或下划线;然后是“@”;然后是数字字母;然后是“.”;最后是2-4个字母结尾
125 var regex = /^([a-zA-Z]|[0-9])(\w|\-)+@[a-zA-Z0-9]+\.([a-zA-Z]{2,4})$/;126 if(!regex.test(form.email.value)){127 alert("邮箱格式错误!");128 form.email.focus();129 return false;130 }131 return true;132 }133
134
135
136
网页面板如下:
问题:
当数据输入到界面时,界面跳转失败,无法录入数据到数据库。
错误原因如下:
目前仍旧没由修正。