index.jsp(添加界面):
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>注册</title> <!-- CSS样式 --> <style type="text/css"> tr{height:30px;} th{width:70px;text-align:left;} </style> <!-- 结束 --> </head> <body > <h2 style="text-align:center">添加学生信息</h2> <form action="./AddServlet" method="post" onsubmit="return check()"> <table style="align:center;margin:auto;" > <tr > <th >登录账号:</th> <td ><input type="text" name="username"id="username" placeholder="请输入登录账号"></td> </tr> <tr> <th >登录密码:</th> <td><input type="password" name="password"id="password"placeholder="请输入密码"></td> </tr> <tr > <th >姓名:</th> <td><input type="text" name="name"id="name"placeholder="请输入姓名"></td> </tr> <tr > <th >性别:</th> <td><input type="radio" name="sex"id="sex" value="男" checked="checked">男<input type="radio" name="sex"id="sex" value="女">女</td> </tr> <tr > <th >学号:</th> <td><input type="text" name="xuehao"id="xuehao"placeholder="请输入学号"></td> </tr> <tr > <th >邮箱:</th> <td><input type="text" name="mail"id="mail"placeholder="请输入邮箱"></td> </tr> <tr > <th >所在学院:</th> <td><input type="text" name="suozaixueyuan"id="suozaixueyuan"placeholder="请输入所在学院"></td> </tr> <tr > <th >所在系:</th> <td><input type="text" name="xi"id="xi"placeholder="请输入所在系"></td> </tr> <tr > <th >所在班级:</th> <td><input type="text" name="banji"id="banji"placeholder="请输入所在班级"></td> </tr> <tr > <th >入学年份:</th> <td><input type="text" name="入学年份"id="入学年份"placeholder="请输入入学年份"></td> </tr> <tr > <th >生源地:</th> <td><input type="text" name="生源地"id="生源地"placeholder="请输入生源地"></td> </tr> <tr > <th >备注:</th> <td><input type="text" name="备注"id="备注"placeholder="请输入备注"></td> </tr> <tr > <td colspan="2"align="center"><input type="submit" value="添加" > </td> </tr> </table> </form> <!-- 输入字段验证部分 --> <script type="text/javascript"> function check(){ var username=document.getElementById("username"); var password=document.getElementById("password"); var name=document.getElementById("name"); var tel=document.getElementById("学号"); var mail=document.getElementById("mail"); var sReg = /[_a-zA-Z\d\-\.]+@[_a-zA-Z\d\-]+(\.[_a-zA-Z\d\-]+)+$/; //正则表达式 //判断用户名位数 if((username.value).length<6||(username.value).length>12){ alert('请输入6到12位英文字符,数字或下划线,以英文字母开头'); username.focus(); return false; } //判断用户名是否包含汉字 if(/.*[\u4e00-\u9fa5]+.*$/.test(username.value)){ alert('用户名不能包含汉字'); username.focus(); return false; } //判断用户名是否以英文字母开头 if(!isNaN(username.value[0])){ alert('请以英文字母开头'); username.focus(); return false; } //判断密码位数 if(number.value<"20180000"|| number.value>"20189999") { alert(" 学号由2018开头的八位组成"); gs5.focus(); return false; } //判断姓名是否为空 if(name.value==''){ alert('姓名不能为空'); name.focus(); return false; } //验证学号是否合法 if(!(/^1[3|4|5|8][0-9]\d{4,8}$/.test(tel.value))){ alert('前四位以2018开头'); xuehao.focus(); return false; } //判断邮箱格式是否正确 if(! sReg.test(mail.value)){ alert('邮箱格式错误'); mail.focus(); return false; } } </script> <!-- 验证结束 --> </body> </html>
success.jsp(成功界面):
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <h1 style="text-align:center;">添加成功</h1> <h1 style="text-align:center;"><a href="index.jsp">返回继续添加信息</a></h1> </body> </html>
fail.jsp(失败界面):
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <h1 style="text-align:center;">添加失败</h1> <h1 style="text-align:center;"><a href="index.jsp">返回继续添加信息</a></h1> </body> </html>
Dao.java:
package Dao; import java.sql.Connection; import java.sql.SQLException; import java.sql.Statement; import DBUtil.DBUtil; import Name.Name; public class Dao { //将数据添加到数据库 public boolean add(Name login) { Connection conn = DBUtil.getConn(); Statement state = null; //添加的SQL语句,将信息录入到login表中 //问题出处!!!!!!!! /*String sql="insert into login(username,password,name,sex,place,tel,mail) values('"+login.getUsername()+"','"+login.getPassword()+"','"+login.getName()+ "','"+login.getSex()+"','"+login.getBanji()+"','"+login.getBeizhu()+"','"+login.getXi()+"','"+login.getShengyuandi()+"','"+login.getRuxuenianfen()+"','"+login.getXueyuan()+"','"+login.getXuehao()+"','"+login.getMail()+"')"; */ String sql="insert into login(username,password,name,sex,xuehao,xueyuan,mail,xi,banji,ruxuenianfen,beizhu,shengyuandi) values('"+login.getUsername()+"','"+login.getPassword()+"','"+login.getName()+ "','"+login.getSex()+"','"+login.getXuehao()+"','"+login.getXueyuan()+"','"+login.getMail()+"','"+login.getXi()+"','"+login.getBanji()+"','"+login.getRuxuenianfen()+"','"+login.getBeizhu()+"','"+login.getShengyuandi()+"')"; int a=0; try { state = conn.createStatement(); a=state.executeUpdate(sql); }catch (SQLException e) { e.printStackTrace(); return false; } finally { DBUtil.close(state,conn); } if(a>0)return true; else return false; } }
Name.java:
package Name; public class Name { private int id; private String username; private String password; private String name; private String sex; private String mail; private String xuehao; private String xueyuan; private String xi; private String banji; private String ruxuenianfen; private String beizhu; private String shengyuandi; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getXuehao() { return xuehao; } public void setXuehao(String xuehao) { this.xuehao = xuehao; } 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 getRuxuenianfen() { return ruxuenianfen; } public void setRuxuenianfen(String ruxuenianfen) { this.ruxuenianfen = ruxuenianfen; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getBeizhu() { return beizhu; } public void setBeizhu(String beizhu) { this.beizhu = beizhu; } 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 getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getShengyuandi() { return shengyuandi; } public void setShengyuandi(String shengyuandi) { this.shengyuandi = shengyuandi; } public String getBanji() { return banji; } public void setBanji(String banji) { this.banji = banji; } public String getMail() { return mail; } public void setMail(String mail) { this.mail = mail; } public Name(int id, String username, String password, String name, String sex, String beizhu, String banji, String mail,String xuehao,String ruxuenianfen,String shengyuandi) { super(); this.id = id; this.username = username; this.password = password; this.name = name; this.sex = sex; this.beizhu = beizhu; this.banji = banji; this.mail = mail; this.xuehao = xuehao; this.ruxuenianfen = ruxuenianfen; this.shengyuandi = shengyuandi; } public Name( String username, String password, String name, String sex, String beizhu, String banji, String mail,String xuehao,String ruxuenianfen,String shengyuandi, String shengyuandi2, String beizhu2) { this.username = username; this.password = password; this.name = name; this.sex = sex; this.beizhu = beizhu; this.banji = banji; this.mail = mail; this.xuehao = xuehao; this.ruxuenianfen = ruxuenianfen; this.shengyuandi = shengyuandi; } }
AddServlet.java
package AddServlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import Dao.Dao; import Name.Name; /** * Servlet implementation class AddServlet */ @WebServlet("/AddServlet") public class AddServlet extends HttpServlet { private static final long serialVersionUID = 1L; Dao dao =new Dao(); /** * @see HttpServlet#HttpServlet() */ public AddServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); String username=request.getParameter("username"); String password=request.getParameter("password"); String name=request.getParameter("name"); String sex=request.getParameter("sex"); String xuehao=request.getParameter("xuehao"); String mail=request.getParameter("mail"); String xueyuan=request.getParameter("xueyuan"); String xi=request.getParameter("xi"); String banji=request.getParameter("banji"); String ruxuenianfen=request.getParameter("ruxuenianfen"); String shengyuandi=request.getParameter("shengyuandi"); String beizhu=request.getParameter("bizhu"); Name login =new Name(username,password,name,sex,xueyuan,ruxuenianfen,mail,xuehao,xi,banji,shengyuandi,beizhu); if(dao.add(login)) { request.getRequestDispatcher("success.jsp").forward(request,response); } else { request.getRequestDispatcher("fail.jsp").forward(request,response); } } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
DBUtil.java:
package DBUtil; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class DBUtil { public static String db_url="jdbc:mysql://127.0.0.1:3306/javaweb?useUnicode=ture&" + "characterEncoding=utf8&serverTimezone=GMT&useSSL=false"; public static String db_user="root"; public static String db_password="19991021shen"; public static Connection getConn() { Connection conn=null; try { Class.forName("com.mysql.cj.jdbc.Driver"); conn=DriverManager.getConnection(db_url,db_user,db_password); System.out.println("数据库连接成功"); }catch(Exception e) { e.printStackTrace(); } return conn; } public static void close (Statement state,Connection conn) { if(state!=null) { try { state.close(); //System.out.println("state关闭"); }catch(SQLException e) { e.printStackTrace(); } } if(conn!=null) { try { conn.close(); //System.out.println("conn关闭"); }catch(SQLException e) { e.printStackTrace(); } } } public static void close (ResultSet rs,Statement state,Connection conn) { if(rs!=null) { try { rs.close(); //System.out.println("rs关闭"); }catch(SQLException e) { e.printStackTrace(); } } if(state!=null) { try { state.close(); //System.out.println("state关闭"); }catch(SQLException e) { e.printStackTrace(); } } if(conn!=null) { try { conn.close(); //System.out.println("conn关闭"); }catch(SQLException e) { e.printStackTrace(); } } } public static void main(String[] args) { @SuppressWarnings("unused") Connection conn = DBUtil.getConn(); } }
数据库表: