题目要求:
1登录账号:要求由6到12位字母、数字、下划线组成,只有字母可以开头;(1分)
2登录密码:要求显示“• ”或“*”表示输入位数,密码要求八位以上字母、数字组成。(1分)
3性别:要求用单选框或下拉框实现,选项只有“男”或“女”;(1分)
4学号:要求八位数字组成,前四位为“2018”开头,输入自己学号;(1分)
5姓名:输入自己的姓名;
5电子邮箱:要求判断正确格式xxxx@xxxx.xxxx;(1分)
6点击“添加”按钮,将学生个人信息存储到数据库中。(3分)
7可以演示连接上数据库。(2分)
com.Bean
public class User {
private String username;
private String password;
private String name;
private String studentID;
private String sex;
private String phone;
private String email;
private String xueyuan;
private String xi;
private String classes;
private String year;
private String shengyuandi;
private String beizhu;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getStudentID() {
return studentID;
}
public void setStudentID(String studentID) {
this.studentID = studentID;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getXueyuan() {
return xueyuan;
}
public void setXueyuan(String xueyuan) {
this.xueyuan = xueyuan;
}
public String getXi() {
return xi;
}
public void setXi(String xi) {
this.xi = xi;
}
public String getClasses() {
return classes;
}
public void setClasses(String classes) {
this.classes = classes;
}
public String getYear() {
return year;
}
public void setYear(String year) {
this.year = year;
}
public String getShengyuandi() {
return shengyuandi;
}
public void setShengyuandi(String shengyuandi) {
this.shengyuandi = shengyuandi;
}
public String getBeizhu() {
return beizhu;
}
public void setBeizhu(String beizhu) {
this.beizhu = beizhu;
}
public User(String username, String password, String name, String studentID, String sex, String phone, String email,
String xueyuan, String xi, String classes, String year, String shengyuandi, String beizhu) {
super();
this.username = username;
this.password = password;
this.name = name;
this.studentID = studentID;
this.sex = sex;
this.phone = phone;
this.email = email;
this.xueyuan = xueyuan;
this.xi = xi;
this.classes = classes;
this.year = year;
this.shengyuandi = shengyuandi;
this.beizhu = beizhu;
} }
数据库驱动加载与连接()
package com.DBUtil; import java.sql.Connection; import java.sql.DriverManager;
//import java.sql.ResultSet;
import java.sql.SQLException;
//import java.sql.Statement; public class UserDBUtil {
static Connection con;
public static Connection getConnection() {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
System.out.println("数据库驱动加载成功");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} try {
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/user?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B88&serverTimezone=GMT%2B8&useSSL=false","root","101032");
System.out.println("数据库链接成功!");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} return con;
} public static void main(String[] args) {
UserDBUtil c=new UserDBUtil();
c.getConnection(); }
出现如图所示输出,说明数据库的驱动加载成功,数据库连接成功。
servlet是运行在web服务器或应用服务器的程序,用来处理客户端请求的动态资源.即后台的服务程序
package com.Servlet; import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
//import java.util.List; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; //import com.Bean.*;
import com.DBUtil.*; public class AddServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void service(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException {
req.setCharacterEncoding("UTF-8"); String username = req.getParameter("username");
String password= req.getParameter("password");
String name= req.getParameter("name");
String studnetID = req.getParameter("studnetID");
String sex= req.getParameter("sex");
String phone= req.getParameter("phone");
String email= req.getParameter("email");
String xueyuan = req.getParameter("xueyuan");
String xi = req.getParameter("xi");
String classes = req.getParameter("clsaaes");
String year = req.getParameter("year");
String shengyuandi = req.getParameter("shengyuandi");
String beizhu = req.getParameter("beizhu"); Connection conn = UserDBUtil.getConnection();
Statement state =null;
String addsql = null;
try {
state = conn.createStatement();
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+"')";
state.executeUpdate(addsql);
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
resp.sendRedirect(req.getContextPath()+"/index.jsp"); }
}
网页的排版编码:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>用户注册</title>
</head>
<body> <script type="text/javascript" src="./js/jquery-3.3.1.min.js"></script> <div class="container">
<section>
<form action="AddServlet" method="post" onsubmit="return check(this)">
<span class="username">登陆账号</span>
<input type="text" name="username" placeholder=" username" id="username">
<br/> <span class="password">登陆密码</span>
<input type="password" name="password" placeholder=" password" id="password">
<br/> <span class="name">姓名</span>
<input type="text" name="name" placeholder=" name" id="name">
<br/> <span class="studentID">学号</span>
<input type="text" name="studentID" placeholder=" studentID" id="studentID" value="2018">
<br/> <span class="sex">性别</span>
<input type="radio" name="sex" id="sexnan" value="男">男
<input type="radio" name="sex" id="sexnv" value="女">女
<br/> <!-- <span class="diqu">地区</span> -->
<!-- <select name="diqu"> -->
<!-- <option value ="北京市">北京市</option> -->
<!-- <option value ="天津市">天津市</option> -->
<!-- <option value="河北省">河北省</option> -->
<!-- </select> -->
<!-- <br/> --> <span class="phone">手机号</span>
<input type="text" name="phone" placeholder=" phone" id="phone">
<br/> <span class="email">邮箱</span>
<input type="text" name="email" placeholder=" email" id="email">
<br/> <span class="xueyuan">所在学院:</span>
<input type="text" name="xueyuan" placeholder=" xueyuan" id="xueyuan">
<br/> <span class="xi">所在系:</span>
<input type="text" name="xi" placeholder=" suozaixi" id="xi">
<br/> <span class="classes">所在班级:</span>
<input type="text" name="classes" placeholder=" classes" id="classes">
<br/> <span class="year">入学年份(届):</span>
<select name="year">
<option value ="2014">2014</option>
<option value ="2015">2015</option>
<option value ="2016">2016</option>
<option value="2017">2017</option>
<option value="2018">2018</option>
<option value ="2019">2019</option>
</select>
<br/> <span class="shengyuandi">生源地:</span>
<input type="text" name="shengyuandi" placeholder=" shengyuandi" id="shengyuandi">
<br/> <span class="beizhu">备注:</span>
<input type="text" name="beizhu" placeholder=" beizhu" id="beizhu">
<br/> <input type="submit" value="保存" class="butn">
<!-- <input type="reset" value="重置" class="butn"> -->
<br/>
</form>
</section>
</div> <script>
//正则表达式
function check(form){ if(form.username.value.length<6 || form.username.value.length>12 || new RegExp("[^0-9a-zA-Z]").test(form.username.value)){
alert("登陆账号必须由6-12位英文字符或数字组成!");
form.user.focus();
return false;
}
if(new RegExp("[^a-zA-Z]").test(form.username.value.substring(0,1))){
alert("登陆账号必须以英文字母开头!");
form.user.focus();
return false;
}
if(form.password.value.length <= 8 || new RegExp("[^0-9a-zA-Z]").test(form.password.value)){
alert("密码必须由八位以上字母、数字组成!");
form.phone.focus();
return false;
}
if(form.studentID.value.length != 8 || new RegExp("[^0-9]").test(form.studentID.value)){
alert("请输入八位学号!");
form.phone.focus();
return false;
} if(form.phone.value.length != 11 || new RegExp("[^0-9]").test(form.phone.value)){
alert("手机号请输入11位数字!");
form.phone.focus();
return false;
} //以数字字母开头,中间可以是多个数字字母或下划线;然后是“@”;然后是数字字母;然后是“.”;最后是2-4个字母结尾
var regex = /^([a-zA-Z]|[0-9])(\w|\-)+@[a-zA-Z0-9]+\.([a-zA-Z]{2,4})$/;
if(!regex.test(form.email.value)){
alert("邮箱格式错误!");
form.email.focus();
return false;
}
return true;
}
</script> </body>
</html>
网页面板如下:
问题:
当数据输入到界面时,界面跳转失败,无法录入数据到数据库。
错误原因如下:
目前仍旧没由修正。