mvc简单的注册(新手试用)

1登录账号:要求由612位字母、数字、下划线组成,只有字母可以开头;

2登录密码:要求显示“• ”或“*”表示输入位数,密码要求八位以上字母、数字组成。

3性别:要求用单选框或下拉框实现,选项只有“男”或“女”;

4学号:要求八位数字组成,前四位为2018”开头,输入自己学号;

5姓名:输入自己的姓名;

5电子邮箱:要求判断正确格式[email protected]

6点击“添加”按钮,将学生个人信息存储到数据库中。

 

 until层

DBUtil.java

package util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DBUtil {
    static String user="root";
    static String password="";
    static String url="jdbc:mysql://localhost:3306/login?&useSSL=false&serverTimezone=UTC";


//连接数据库
    public static  Connection getConn(){
        Connection conn=null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
                conn=DriverManager.getConnection(url, user, password);
        } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
        }
        return conn;
    }

//关闭数据库
    public 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();
            }
        }
    }
}

Dao层

DaoDao.java

package Dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;


import util.DBUtil;

public class Dao {
    static Connection conn;
    static PreparedStatement ps = null;
    static ResultSet rs;
    static String sql = "select * from WeiHu";
    static DBUtil ut= new DBUtil();
    static Scanner in = new Scanner(System.in);
    static int num1=1;
    
    //平台维护信息录入
    public static int add(String w1,String w2,String w3,String w4,String w5,String w6,String w7) throws SQLException
    {
        
         int b=0;
         conn= ut.getConn();
         String sql="insert into login1 values(?,?,?,?,?,?,?)";
         try {
             ps=conn.prepareStatement(sql);
             ps.setString(1,w1);
             ps.setString(2,w2);
             ps.setString(3,w3);
             ps.setString(4,w4);
             ps.setString(5,w5);
             ps.setString(6,w6);
             ps.setString(7,w7);
             int a=ps.executeUpdate();
             if(a>0) {
                 b++;
                 System.out.println("添加成功");
             }
             else {
                 System.out.println("添加失败");
             }
         }catch(Exception e) {
             e.printStackTrace();
         }
         try {
             if(ps!=null)ps.close();
             if(conn!=null)conn.close();
         }catch(Exception e2) {
             e2.printStackTrace();
         }
            
         
         return b;
    }    

//插入数据
    public static int add_one(String w1,String w2,String w3,String w4,String w5,String w6,String w7,String w8,String w9,String w10,String w11,String w12) throws SQLException
    {
        
         int b=0;
         conn= ut.getConn();
         String sql="insert into register values(?,?,?,?,?,?,?,?,?,?,?,?)";
         try {
             ps=conn.prepareStatement(sql);
             ps.setString(1,w1);
             ps.setString(2,w2);
             ps.setString(3,w3);
             ps.setString(4,w4);
             ps.setString(5,w5);
             ps.setString(6,w6);
             ps.setString(7,w7);
             ps.setString(8,w8);
             ps.setString(9,w9);
             ps.setString(10,w10);
             ps.setString(11,w11);
             ps.setString(12,w12);
             int a=ps.executeUpdate();
             if(a>0) {
                 b++;
                 System.out.println("添加成功");
             }
             else {
                 System.out.println("添加失败");
             }
         }catch(Exception e) {
             e.printStackTrace();
         }
         try {
             if(ps!=null)ps.close();
             if(conn!=null)conn.close();
         }catch(Exception e2) {
             e2.printStackTrace();
         }
            
         
         return b;
    }    
}

前端使用jsp

login.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>注册</title>
<script type="text/javascript">
function validate()
{
    
    var Id=document.forms["myForm"]["l5"].value;

    if(Id<20180000&&Id>=20190000)
        {
        alert("学号必须以2018开头");
        return false;
        }
}
</script>
</head>
<body>
<header><center><h3>添加学生信息</h3></center></header>
<hr>
<form action="Login_deal.jsp" name="myForm" onsubmit="return validate()">
<table  border="1" align="center" bgcolor="#FFFFCC">
<tr>
<td>登录账号</td>
<td><input type="text" name="l1" ></td>
</tr>
<tr>
<td>登录密码</td>
<td><input type="password" name="l2"></td>
</tr>
<tr>
<td>姓别</td>
<td>
<input type="radio" name="l3"><input type="radio" name="l3"></td>
</tr>
<tr>
<td>姓名</td>
<td>
<input type="text" name="l4">
</td>
</tr>
<tr>
<td>学号</td>
<td>
<input type="text" name="l5">
</td>
</tr>
<tr>
<td>电子邮件</td>
<td><input type="text" name="l6"></td>
</tr>
<tr>
<td>所在学院</td>
<td><input type="text" name="l7"></td>
</tr>
<tr>
<td>所在系</td>
<td><input type="text" name="l8"></td>
</tr>
<tr>
<td>所在班级</td>
<td><input type="text" name="l9"></td>
</tr>
<tr>
<td>入学年份</td>
<td>
<select name="l10">
<option value="2018">2018</option>
<option value="2017">2017</option>
</select>
</td>
</tr>
<tr>
<td>生源地</td>
<td><input type="text" name="l11" value=""></td>
</tr>
<tr>
<td>备注</td>
<td><textarea rows="10" cols="30" name="l12"></textarea></td>
</tr>
<tr>
<td><input type="submit" value="添加"></td>
</tr>
</table>
</form>
</body>
</html>

login_deal.jsp

<%@page import="Dao.Dao"%>
<%@page import="java.util.regex.Pattern"%>
<%@page import="java.util.regex.Matcher"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
    request.setCharacterEncoding("utf-8");
    String l1=request.getParameter("l1");
    String l2=request.getParameter("l2");
    String l3=request.getParameter("l3");
    String l4=request.getParameter("l4");
    String l5=request.getParameter("l5");
    String l6=request.getParameter("l6");
    String l7=request.getParameter("l7");
    String l8=request.getParameter("l8");
    String l9=request.getParameter("l9");
    String l10=request.getParameter("l10");
    String l11=request.getParameter("l11");
    String l12=request.getParameter("l12");
    String user="^[a-zA-Z][a-zA-Z0-9_]{5,12}$";
    Pattern puser = Pattern.compile(user);
    Matcher muser = puser.matcher(l1);
    boolean isMatch3 = muser.matches();
    String pass="^[a-zA-Z][a-zA-Z0-9_]{7,20}$";
    Pattern ppass = Pattern.compile(pass);
    Matcher mpass = ppass.matcher(l2);
    boolean isMatch2 = mpass.matches();
    String mail = "^[\\w-]+(\\.[\\w-]+)*@[\\w-]+(\\.[\\w-]+)+$";
    Pattern pmail = Pattern.compile(mail);
    Matcher mmail = pmail.matcher(l6);
    boolean isMatch1 = mmail.matches();
    if(isMatch3){
        if(isMatch2){
            if(isMatch1){
                if(Dao.add_one(l1, l2, l3, l4, l5, l6, l7,l8,l9,l10,l11,l12)==1)
                {
                    {
                           out.print("<script language = 'javascript'>alert('添加成功');</script>");
                           response.setHeader("refresh", "0;url=Login.jsp");
                           }
                }
                else{
                    out.print("<script language = 'javascript'>alert('添加失败');</script>");
                    response.setHeader("refresh", "0;url=Login.jsp");
                }
            }else{
               out.print("<script language = 'javascript'>alert('您的邮箱" +l6 + "是错误格式!!!');</script>");
                out.print("<script>window.history.go(-1);  </script>");
                
            }
            
           } else {
               out.print("<script language = 'javascript'>alert('您的密码" +l2 + "是错误格式!!!应为八位以上字母数字组成!');</script>");
                out.print("<script>window.history.go(-1);  </script>");
           }
    }else{
        out.print("<script language = 'javascript'>alert('您的用户名" +l1 + "是错误格式!!!应为6-12位以上字母数字下划线组成!字母开头');</script>");
        out.print("<script>window.history.go(-1);  </script>");
    }
     
    
 %>
</body>
</html>

猜你喜欢

转载自www.cnblogs.com/sonofdemon/p/11723355.html