添加学生信息web界面连接数据库
利用eclipse创建web项目,Dynamic Web Project项目,需要配置tomcat及其环境变量,MySQL环境变量,下载Navicat for MySQL
先贴代码
tomcat下载路径:https://tomcat.apache.org/
相关的环境配置步骤:https://www.cnblogs.com/limn/p/9358657.html
具体在eclipse中配置Tomcat 的视频链接:https://www.w3cschool.cn/minicourse/play/bejif?cp=1881&gid=0
jdbc下载路径:https://mvnrepository.com/artifact/mysql/mysql-connector-java
login.jsp文件
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>当前位置:添加学生信息</title> <link rel="stylesheet" href="register.css"> </head> <body> <% Object message = request.getAttribute("message"); if (message != null && !"".equals(message)) { %> <script type="text/javascript"> alert("<%=request.getAttribute("message")%> "); </script> <% } %> <div class="reg_back"> <div class="reg_left"> <p>新用户注册</p> <p>USER REGISTER</p> </div> <div class="reg_center"> <div class="reg_form"> <form action="Servlet?method=add" method="post" onsubmit="return check()"> <table> <tr> <td class="td_left"><label for="name">登录账号:</label></td> <td class="td_right"><input type="text" name="name" placeholder="请输入用户名" id="name" pattern="^(?![^A-Za-z]+$)(?![^0-9]+$)[0-9A-Za-z_]{8,12}$" title="只有字母开头"></td> </tr> <tr> <td class="td_left"><label for="pwd">登录密码:</label></td> <td class="td_right"><input type="password" name="pwd" placeholder="请输入密码" id="pwd" pattern="^[\w]{8,18}$" title="必须由8~12位字母数字下划线构成"></td> </tr> <tr> <td class="td_left"><label>性别:</label></td> <td class="td_right"><input type="radio" name="sex" value="男" checked> 男 <input type="radio" name="sex" value="女"> 女</td> </tr> <tr> <td class="td_left"><label for="user">姓名:</label></td> <td class="td_right"><input type="text" name="user" placeholder="请输入真实姓名" id="user"></td> </tr> <tr> <td class="td_left"><label for="number">学号:</label></td> <td class="td_right"><input type="text" name="number" placeholder="请输入您的学号,以2018开头" id="number" maxlength="8" pattern="^2018\\d{4}$"></td> </tr> <tr> <td class="td_left"><label for="email">电子邮箱:</label></td> <td class="td_right"><input type="email" name="email" placeholder="请输入Email" id="email"></td> </tr> <tr> <td class="td_left"><label for="schooly">所在学院:</label></td> <td class="td_right"><input type="text" name="schooly" placeholder="请输入所在学院" id="schooly"></td> </tr> <tr> <td class="td_left"><label for="schoolx">所在系:</label></td> <td class="td_right"><input type="text" name="schoolx" placeholder="请输入所在系" id="schoolx"></td> </tr> <tr> <td class="td_left"><label for="classes">所在班级:</label></td> <td class="td_right"><input type="text" name="classes" placeholder="请输入所在班级" id="classes"></td> </tr> <tr> <td class="td_left"><label for="sel"><sup><font color="red">*</font></sup>入学年份(届):</label></td> <td class="td_right"><select id="sel"> </select> <span id="jie"> 届</span> </td> </tr> <tr> <td class="td_left"><label for="place">生源地:</label></td> <td class="td_right"><input type="text" name="place" id="place"></td> </tr> <tr> <td class="td_left"><label for="bz">备注:</label></td> <td class="td_right"> <textarea name="bz" id="bz" cols="30" rows="5" placeholder=""style="overflow: auto;"></textarea></td> </tr> <tr> <td colspan="2" align="center"><input type="submit" value="添加" id="btn_sub"></td> </tr> </table> </form> </div> </div> <div class="reg_right"> <p> 已有账号?<a href="#">立即登录</a> </p> </div> </div> <script type="text/javascript"> function check() { var name = document.getElementById("name"); var pwd = document.getElementById("pwd"); var user = document.getElementById("user"); var schooly = document.getElementById("schooly"); var schoolx = document.getElementById("schoolx"); var classes = document.getElementById("classes"); var sel = document.getElementById("sel"); var email = document.getElementById("email"); var place = document.getElementById("place"); var bz = document.getElementById("bz"); //非空 if(user.value == '') { alert('姓名不能为空'); user.focus(); return false; }else if(pwd.value == '') { alert('密码不能为空'); pwd.focus(); return false; }else if(name.value == '') { alert('用户名不能为空') name.focus(); return false; }else if(phone.value == '') { alert('电话号码不能为空'); phone.focus(); return false; }else if(email.value == '') { alert('邮件地址不能为空') email.focus(); return false; } } </script> <script type="text/javascript"> onload = function() { let pastYear = new Date().getFullYear() var year = new Date().getFullYear(); //获取当前年份 var sel = document.getElementById('sel');//获取select下拉列表 for (var i = year; i >=1900; i--)//循环添加当前年份的前两年到当前年份加3年的每个年份依次添加到下拉列表 { var option = document.createElement('option'); option.value = i; var txt = document.createTextNode(i); option.appendChild(txt); sel.appendChild(option); } } </script> </body> </html>
register.css文件
@charset "UTF-8"; *{ margin: 0px; padding: 0px; box-sizing: border-box; } body { background: url("img/timg.jpg") no-repeat center; /*设置图片适应整个页面*/ background-size: 100% 100%; background-attachment:fixed; } .reg_back { /*设置整体的尺寸、背景色、边距等*/ width: 850px; height: 700px; border: 8px solid #eeeeee; background: white; margin: auto; margin-top: 20px; } .reg_left { /*设置左浮动和外边距*/ float: left; margin: 10px; } .reg_left > p:first-child { /*设置段落(新用户注册)颜色和字体大小*/ color: yellow; font-size: 20px; } .reg_left > p:last-child { /*设置段落(USER REGISTER)颜色和字体大小*/ color: #A6A6A6; font-size: 20px; } .reg_center { /*设置中间的各种输入框等*/ float: left; width: 450px; } .reg_right { /*设置右边段落浮动和外间距*/ float: right; margin: 30px; } .reg_right > p:first-child { font-size: 15px; } .reg_right p a { /*设置超链接(立即登录)颜色*/ color: crimson; } .td_left { /*设置表单中字体对齐方式和宽度、高度*/ width: 100px; text-align: right; height: 40px; } .td_right { /*设置输入框内边距*/ padding-left: 40px; } #name ,#pwd,#email, #user ,#number,#schooly,#schoolx,#classes,#place,#bz,#checkcode{ /*设置输入框大小和边框*/ width: 200px; height: 30px; border: 1px solid #A4A4A4; /* 设置边框为圆角 */ border-radius: 8px; padding-left: 10px; } #place,#checkcode{ /*设置输入框大小和边框*/ width: 300px; height: 30px; border: 1px solid #A4A4A4; /* 设置边框为圆角 */ border-radius: 8px; padding-left: 10px; } #checkcode{ /*验证码宽度*/ width: 100px; } #img_check{ /*验证码图片*/ vertical-align: middle; height: 30px; width: 95px; } #btn_sub{ /*注册按钮*/ background: lime; width: 100px; height: 40px; border: 1px solid lime ; }
Login.java
package bean; public class Login { int id; String name; String pwd; String sex; String user; String number; String schooly; String schoolx; String classes; String sel; String email; String place; String bz; public Login(String name, String pwd, String user, String sex, String number, String email,String schooly,String schoolx,String classes,String bz,String sel,String place) { // TODO 自动生成的构造函数存根 this.name = name; this.pwd = pwd; this.user = user; this.sex = sex; this.number=number; this.schooly=schooly; this.schoolx=schoolx; this.classes=classes; this.bz=bz; this.sel=sel; this.place=place; this.email = email; } public Login(int id,String name, String pwd, String user, String sex, String number, String email,String schooly,String schoolx,String classes,String bz,String sel,String place) { // TODO 自动生成的构造函数存根 this.id=id; this.name = name; this.pwd = pwd; this.user = user; this.sex = sex; this.number=number; this.schooly=schooly; this.schoolx=schoolx; this.classes=classes; this.bz=bz; this.sel=sel; this.place=place; this.email = email; } public String getSchooly() { return schooly; } public void setSchooly(String schooly) { this.schooly = schooly; } public String getSchoolx() { return schoolx; } public void setSchoolx(String schoolx) { this.schoolx = schoolx; } public String getClasses() { return classes; } public void setClasses(String classes) { this.classes = classes; } public String getSel() { return sel; } public void setSel(String sel) { this.sel = sel; } public String getPlace() { return place; } public void setPlace(String place) { this.place = place; } public String getBz() { return bz; } public void setBz(String bz) { this.bz = bz; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } public String getUser() { return user; } public void setUser(String user) { this.user = user; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getNumber() { return number; } public void setNumber(String number) { this.number = number; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } }
Add.java
1 package dao; 2 3 import java.sql.Connection; 4 import java.sql.SQLException; 5 import java.sql.Statement; 6 7 import bean.Login; 8 import util.BaseConnection; 9 10 public class Add { 11 // 增 12 public static boolean add1(Login login) { 13 boolean f = false; 14 Connection conn = BaseConnection.getConnection(); 15 // PreparedStatement ps=null; 16 String sql = "insert into login(user,pwd,name,sex,number,email,schooly,schoolx,classes,sel,bz,place) values('" + login.getUser() + "','" 17 + login.getPwd() + "','" + login.getName() + "','" + login.getSex() + "','" + login.getNumber() + "','" 18 + login.getSchooly() + "','" + login.getEmail() + "','" +login.getSchoolx() + "','"+ login.getClasses() + "','"+ login.getSel() + "','"+ login.getBz() + "','"+ login.getPlace() + "')"; 19 // String sql = "insert into course1(name,teacher,classroom) values('" + 20 // cour.getName() + "','" + cour.getTeacher() + "','" + cour.getClassroom() + 21 // "')'"; 22 Statement state = null; 23 int a = 0; 24 try { 25 state = conn.createStatement(); 26 a = state.executeUpdate(sql); 27 } catch (SQLException e) { 28 // TODO Զ ɵ catch 29 e.printStackTrace(); 30 } 31 32 if (a > 0) { 33 System.out.println(" 122 "); 34 f = true; 35 } else { 36 System.out.println("12 "); 37 } 38 return f; 39 } 40 41 public static void main(String[] args) { 42 Login login = new Login("2", "5", "6666d6", "4", "5", "6", "7","77","85","99","河北省","58"); 43 add1(login); 44 } 45 }
check.java
1 package dao; 2 3 import java.util.regex.Matcher; 4 import java.util.regex.Pattern; 5 6 public class check { 7 8 /** 9 * 验证邮箱地址是否正确 10 * 11 * @param email 12 * @return 13 */ 14 public static boolean checkEmail(String email) { 15 boolean flag = false; 16 try { 17 String check = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$"; 18 Pattern regex = Pattern.compile(check); 19 Matcher matcher = regex.matcher(email); 20 flag = matcher.matches(); 21 } catch (Exception e) { 22 // LOG.error("验证邮箱地址错误", e); 23 flag = false; 24 } 25 26 return flag; 27 } 28 29 /** 30 * 验证手机号码 31 * 32 * @param mobiles 33 * @return 34 */ 35 /*public static boolean isMobileNO(String mobiles) { 36 boolean flag = false; 37 try { 38 Pattern p = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$"); 39 Matcher m = p.matcher(mobiles); 40 flag = m.matches(); 41 } catch (Exception e) { 42 // LOG.error("验证手机号码错误", e); 43 flag = false; 44 } 45 return flag; 46 }*/ 47 }
Servlet.java
1 package servlet; 2 3 import java.io.IOException; 4 import java.io.UnsupportedEncodingException; 5 6 import javax.servlet.ServletException; 7 import javax.servlet.annotation.WebServlet; 8 import javax.servlet.http.HttpServlet; 9 import javax.servlet.http.HttpServletRequest; 10 import javax.servlet.http.HttpServletResponse; 11 12 import org.apache.taglibs.standard.tag.el.fmt.RequestEncodingTag; 13 14 import bean.Login; 15 import dao.Add; 16 import dao.check; 17 18 /** 19 * Servlet implementation class Servlet 20 */ 21 @WebServlet("/Servlet") 22 public class Servlet extends HttpServlet { 23 private static final long serialVersionUID = 1L; 24 25 /** 26 * @see HttpServlet#HttpServlet() 27 */ 28 public Servlet() { 29 super(); 30 // TODO Auto-generated constructor stub 31 } 32 33 /** 34 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse 35 * response) 36 */ 37 protected void doGet(HttpServletRequest request, HttpServletResponse response) 38 throws ServletException, IOException { 39 // TODO Auto-generated method stub 40 // response.getWriter().append("Served at: 41 // ").append(request.getContextPath());request. 42 request.setCharacterEncoding("utf-8"); 43 String method = request.getParameter("method"); 44 if (method.equals("add")) { 45 add(request, response); 46 } 47 } 48 49 private void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 50 request.setCharacterEncoding("utf-8"); 51 String name = request.getParameter("name"); 52 String pwd = request.getParameter("pwd"); 53 String sex = request.getParameter("sex"); 54 String user = request.getParameter("user"); 55 String number = request.getParameter("number"); 56 String schooly = request.getParameter("schooly"); 57 String schoolx = request.getParameter("schoolx"); 58 String classes = request.getParameter("classes"); 59 String sel = request.getParameter("sel"); 60 String email = request.getParameter("email"); 61 String place = request.getParameter("place"); 62 String bz = request.getParameter("bz"); 63 Login a = new Login(name, pwd, sex, user, number, schooly, schoolx, classes, sel, email, place, bz); 64 65 if (check.checkEmail(email)) { 66 Login login = new Login(name, pwd, sex, user, number, schooly, schoolx, classes, sel, email, place, bz); 67 if (Add.add1(login)) { 68 request.setAttribute("message", "注册成功"); 69 request.getRequestDispatcher("login.jsp").forward(request, response); 70 } else { 71 request.setAttribute("message", "注册失败"); 72 request.getRequestDispatcher("login.jsp").forward(request, response); 73 } 74 } else { 75 request.setAttribute("message", "电子邮件格式不符"); 76 request.getRequestDispatcher("login.jsp").forward(request, response); 77 } 78 79 } 80 81 /** 82 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse 83 * response) 84 */ 85 protected void doPost(HttpServletRequest request, HttpServletResponse response) 86 throws ServletException, IOException { 87 // TODO Auto-generated method stub 88 doGet(request, response); 89 } 90 91 }
BaseConnection.java
1 package util; 2 3 import java.sql.Connection; 4 import java.sql.DriverManager; 5 import java.sql.ResultSet; 6 import java.sql.SQLException; 7 import java.sql.Statement; 8 9 public class BaseConnection { 10 11 public static Connection getConnection() {// 用这个方法获取mysql的连接 12 Connection conn = null; 13 String driver = "com.mysql.jdbc.Driver"; 14 String url = "jdbc:mysql://localhost:3308/demo2?characterEncoding=utf8&useSSL=true";// 修改数据库名称 15 String user = "root"; 16 String password = "022414ls"; 17 try { 18 Class.forName(driver);// 加载驱动类 19 conn = DriverManager.getConnection(url, user, password);// (url数据库的IP地址,user数据库用户名,password数据库密码) 20 } catch (Exception e) { 21 e.printStackTrace(); 22 } 23 return conn; 24 } 25 26 // 关闭数据库链接 27 public static void close(Statement state, Connection conn) { 28 if (state != null) { 29 try { 30 state.close(); 31 } catch (SQLException e) { 32 e.printStackTrace(); 33 } 34 } 35 36 if (conn != null) { 37 try { 38 conn.close(); 39 } catch (SQLException e) { 40 e.printStackTrace(); 41 } 42 } 43 } 44 45 // 关闭数据库连接 46 public static void close(ResultSet rs, Statement state, Connection conn) { 47 if (rs != null) { 48 try { 49 rs.close(); 50 } catch (SQLException e) { 51 e.printStackTrace(); 52 } 53 } 54 55 if (state != null) { 56 try { 57 state.close(); 58 } catch (SQLException e) { 59 e.printStackTrace(); 60 } 61 } 62 63 if (conn != null) { 64 try { 65 conn.close(); 66 } catch (SQLException e) { 67 e.printStackTrace(); 68 } 69 } 70 } 71 72 public static void main(String[] args) { 73 getConnection(); 74 } 75 }
最后界面: