Java web quiz

Topics requirements:

1 login account: the claims . 6 to 12 is letters, numbers, underscores, only letters can begin; ( 1 min)

2 Password: required to display "•" or " *" indicates that the input digit password required eight or more letters, digits. ( 1 point)

3 Sex: required to achieve a single box or drop-down box, the options only "male" or "female"; ( 1 point)

Science No. 4: Requirements eight digits, the first four bits " 2018" at the beginning, enter their student ID; ( 1 min)

5 Name: Enter your name;

5 E-mail: request determining correct format [email protected] ; ( . 1 min)

6 Click the "Add" button to store students' personal information to the database. ( 3 points)

7 demo can be connected to the database. ( 2 points)

1. Database Connection

package zzm;

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

/**
 * 数据库连接工具
 * @author Hu
 *
 */
public class DBUtil {
    
    public static String db_url = "jdbc:mysql://localhost:3306/zzm?useSSL=false";
    public static String db_user = "root";
    public static String db_pass = "********";
    
    public static Connection getConn () {
        Connection conn = null;
        
        try {
            Class.forName("com.mysql.jdbc.Driver");//加载驱动
            conn = DriverManager.getConnection(db_url, db_user, db_pass);
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        return conn;
    }
    
    /**
     * 关闭连接
     * @param state
     * @param conn
     */
    public static void close (Statement state, Connection conn) {
        if (state != null) {
            try {
                state.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    
    public static 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(An SQLException e) { 
                e.printStackTrace (); 
            } 
        } 
    } 

}

 

2 .. create the required variables

package zzm;

public class Zzm {
    private int id;
    private String zhanghao;
    private String password;
    private String sex;
    private String number;
    private String name;
    private String mail;
    private String xueyuan;
    private String zhuanye;
    private String banji;
    private String year;
    private String shengyuandi;
    private String beizhu;
    public int getId()
    {
        return id;
    }
    public void setId()
    {
        this.id=id;
    }
    public String getZhanghao() 
    {
        return zhanghao;
    }
    public void setZhanghao()
    {
        this.zhanghao=zhanghao;
    }
    public String getPassword()
    {
        return password;
    }
    public void setPassword()
    {
        this.password=password;
    }
    public String getSex() 
    {
        return sex;
    }
    public void setSex()
    {
        this.sex=sex;
    }
    public String getName() 
    {
        return name;
    }
    public void setName()
    {
        this.name=name;
    }
    public String getMail() 
    {
        return mail;
    }
    public void setMail()
    {
        this.mail=mail;
    }
    public String getNumber()
    { 
        Return  number;
    } 
    public  void setNumber () 
    { 
        the this .number = Number; 
    } 
    public String getXueyuan () 
    { 
        return Xueyuan; 
    } 
    public  void setXueyuan () 
    { 
        the this .xueyuan = Xueyuan; 
    } 
    public String getZhuanye () 
    { 
        return zhuanye; 
    } 
    public  void setZhuanye () 
    { 
        the this .zhuanye = zhuanye; 
    } 
    public String getBanji () 
    { 
        return Banji; 
    }
    public void setBanji()
    {
        this.banji=banji;
    }
    public String getYear() 
    {
        return year;
    }
    public void setYear()
    {
        this.year=year;
    }
    public String getShengyuandi() 
    {
        return shengyuandi;
    }
    public void setShengyuandi()
    {
        this.shengyuandi=shengyuandi;
    }
    public String getBeizhu() 
    {
        return beizhu;
    }
    public void setBeizhu()
    {
        this.beizhu=beizhu;
    }
    public Zzm() {}
    public Zzm(int id,String zhanghao,String password,String sex,String name,String number,String mail,String xueyuan,String zhuanye,String banji,String year,String shengyuandi,String beizhu)
    {
        this.id=id;
        this.zhanghao = zhanghao;
        this.password = password;
        this.name = name;
        this.sex = sex;
        this.number =number;
        this.name = name;
        this.mail = mail;
        this.xueyuan = xueyuan;
        this.zhuanye = zhuanye;
        this.banji = banji;
        this.year = year;
        this.shengyuandi = shengyuandi;
        this.beizhu = beizhu;
    }
    public Zzm(String zhanghao, String password,String sex, String name,String number,String mail,String xueyuan,String zhuanye,String banji,String year,String shengyuandi,String beizhu) 
    {
        this.zhanghao = zhanghao;
        this.password = password;
        this.name = name;
        this.sex = sex;
        this.number = number;
        this.mail = mail;
        this.xueyuan = xueyuan;
        this.zhuanye = zhuanye;
        this.banji = banji;
        this.year = year;
        this.shengyuandi = shengyuandi;
        this.beizhu = beizhu;
    }
}
    

3. Create an intermediate layer, web forms to collect data

package zzm;


import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@WebServlet("/ZzmServlet")
public class ZzmServlet extends HttpServlet {
    
    private static final long serialVersionUID = 1L;

    ZzmService service = new ZzmService();
    

    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String method = req.getParameter("method");
        if ("add".equals(method)) {
            add(req, resp);
        } 
    }

    /**
     * 添加
     * @param req
     * @param resp
     * @throws IOException 
     * @throws ServletException 
     */
    private void add(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        req.setCharacterEncoding("utf-8");
        String zhanghao = req.getParameter("zhanghao");
        String password = req.getParameter("password");
        String sex = req.getParameter("sex");
        String number = req.getParameter("number");
        String name = req.getParameter("name");
        String mail = req.getParameter("mail");
        String xueyuan= req.getParameter("xueyuan");
        String zhuanye = req.getParameter("zhuanye");
        String banji = req.getParameter("banji");
        String year = req.getParameter("year");
        String shengyuandi = req.getParameter("shengyuandi");
        String beizhu = req.getParameter("beizhu");
        Zzm zzm= new Zzm(zhanghao,password,sex,name,number,mail,xueyuan,zhuanye,banji,year,shengyuandi,beizhu);
        
        //添加后消息显示
        if(service.add(zzm)) {
            req.setAttribute("message","Added successfully");
            req.getRequestDispatcher ( "zzm.jsp" ) .forward (REQ, RESP); 
        } the else { 
            req.setAttribute ( "Message", "user name, please re-enter" ); 
            req.getRequestDispatcher ( "zzm.jsp" ) .forward (REQ, RESP); 
        } 
    } 
    
    
}

4. Create Dao interface, basic database operations

package zzm;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;


/**
 * 
 * Dao层操作数据
 * @author Zhao
 *
 */
public class ZzmDao {

    /**
     * 添加
     * @param zzm
     * @return
     */
    public boolean add(Zzm zzm) {
        String sql = "insert into zzm(zhanghao,password,sex,name,number,mail,xueyuan,zhuanye,banji,year,shengyuandi,beizhu) values('" + zzm.getZhanghao() + "','" + zzm.getPassword() + "','" + zzm.getSex() + "','"+zzm.getName()+"','" + zzm.getNumber() + "','" + zzm.getMail()+"','"+zzm.getXueyuan()+"','"+zzm.getZhuanye()+"','"+zzm.getBanji() +"','"+zzm.getYear()+"','"+zzm.getShengyuandi()+"','"+zzm.getBeizhu() +"')";
        Connection conn = DBUtil.getConn();
        Statement state = null;
        boolean f = false;
        int a = 0;
        
        try {
            state = conn.createStatement();
            state.executeUpdate(sql);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(state, conn);
        }
        
        if (a > 0) {
            f = true;
        }
        return f;
    }

    public boolean zhanghao(String zhanghao) {
        // TODO Auto-generated method stub
        return false;
    }

    

}

5. Create the service layer,

package zzm;

import java.util.List;



/**
 * ZzmService
 * 服务层
 * @author Zhao
 *
 */
public class ZzmService {

    ZzmDao cDao = new ZzmDao();
    
    /**
     * 添加
     * @param Zzm
     * @return
     */
    public boolean add(Zzm zzm) {
        boolean f = false;
        if(!cDao.zhanghao(zzm.getZhanghao())) {
            cDao.add(zzm);
            f = true;
        }
        return f;
    }
    
    
}

6. Draw interface operation (JSP)

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>

<link rel="stylesheet" type="text/css" href="css/nav.css">
<link rel="stylesheet" type="text/css" href="font/iconfont.css">

<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript" src="js/nav.js"></script>

</head>
<body>
<%
         Object message = request.getAttribute("message");
         if(message!=null && !"".equals(message)){
     
    %>
         <script type="text/javascript">
              alert("<%=request.getAttribute("message")%>");
         </script>
    <%} %>
    <div align="center">
<form action="ZzmServlet?method=add" method="post" onsubmit="return check()">
<table  width="500px" height="650px"border="1" style="text-align:center">
         <tr height="50px" align="center" bgcolor="#FFFFCC">
           <td width="100"><font color="black"style="font-family:宋体;">登录账号:</font></td>
            <td width = "400"> < input id = "zhanghao" name = "zhanghao" placeholder = " Please enter the account" /> </ TD> 
           <TD width = "100"> <font Color = "Black "style =" font-family: Arial; "> password: </ font> </ td
            
        </ TR>
        <tr height="50px" align="center" bgcolor="#FFFFCC">
            <td width="400"><input  type="password" name="password" placeholder="请输入密码"/></td>
            
        </tr>
         <tr height="50" align="center" bgcolor="#FFFFCC">
           <td width="100"><font color="black"style="font-family:宋体;">性别:</font></td>
            <td><select id="sex" name="sex" >
                <option value="男">男</option>
                <option value="女">女</option>
                </select></td>
            </tr>
                    <tr height="50px" align="center" bgcolor="#FFFFCC">
           <td width="100"><font color="black"style="font-family:宋体;">姓名:</font></td>
            <td width="400"><input  id="name" name="name" placeholder="请输入姓名"/></td>
        </tr>
            <tr height="50" align="center" bgcolor="#FFFFCC">
           <td width="100"><font color="black"style="font-family:宋体;">学号:</font></td>
            <td width="400"><input  id="number" name="number" placeholder="请输入学号"/></td>
            </tr>
            <tr height="50" align="center" bgcolor="#FFFFCC">
           <td width="100"><font color="black"style="font-family:宋体;">电子邮箱:</font></td>
            <td width="400"><input  id="mail" name="mail "placeholder =" Please enter the Mailbox "/> </ TD> 
           Arial;"> UNIVERSITY where: </ font> </ td<TD width =" 100 "> <font Color = "black" style = "font-
            <TR height =" 50 "align = left =" Center "bgcolor =" # FFFFCC ">
            </ TR>
            <td width="400"><input  id="xueyuan" name="xueyuan" placeholder="请输入学院名" /></td>
            </tr>
            <tr height="50" align="center" bgcolor="#FFFFCC">
           <td width="100"><font color="black"style="font-family:宋体;">所在专业:</font></td>
            <td width="400"><input  id="zhuanye" name="zhuanye" placeholder="请输入专业" /></td>
            </tr>
            <tr height="50" align="center" bgcolor="#FFFFCC">
           <td width="100"><font color="black"style="font-family:宋体;">所在班级:</font></td>
            <td width="400"><input  id="banji" name="banji" placeholder="请输入班级" /></td>
            </tr>
            <tr height="50" align="center" bgcolor="#FFFFCC">
           <td width="100"><font color="black"style="font-family:宋体;">入学年份:</font></td>
            <td><select id="year" name="year" >
                <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></td>
            </tr>
            <tr height="50" align="center" bgcolor="#FFFFCC">
           <td width="100"><font color="black"style="font-family:宋体;">生源地:</font></td>
            <td width="400"><input  id="shengyuandi" name="shengyuandi" placeholder="请输入生源地" /></td>
            </tr>
            
            <tr height="100" align="center" bgcolor="#FFFFCC">
           <td width="100"><font color="black"style="font-family:宋体;">备注:</font></td>
            <td width="400"><input  id="beizhu" name="beizhu" placeholder="无" /></td>
            </tr>

            <tr height="50" align="center" bgcolor="#FFFFCC">
            <td width="400"><button type="submit" >添加</button></td>
            <td width="400"><input type="reset"  value="重新输入" /></td>
            </tr>
                  
</form>
</div>
<script type="text/javascript">  
function check() {
    var zhanghao = document.getElementById("zhanghao");
    var password = document.getElementById("password");
    var number = document.getElementById("number");
    var mail = document.getElementById("mail");
    
    IF (zhanghao.value.match (/ ^ [A-zA-the Z] \ {W} $ 5,11 /! )) {

           

        Alert ( ; "input error, beginning with letters length 6-12" ); 

        

        num.focus (); 

        return  to false ; 

} 


    the else  IF (password.value.match (/ ^ [A-Za-Z0-9] \! {7,20} $ W / )) { 

   

     Alert ( "alphanumeric password from the above composition and above eight" ); 

  

     mima.focus (); 

     return  to false ; 

} 
    the else  IF (number.value < "20.18 million" | | number.value> "20,189,999" ) 
        { 
         Alert ( "student number of eight bits from the beginning of 2018" ); 
         number.focus (); 

         return  to false 
        } 

    the else  IF(mail.value.match (/ ^ [a- z0-9] + ([._ \\ -]! * [a-z0-9]) * @ ([a-z0-9] + [- a- . Z0-9] * [A-Z0-9] +) {1,63} [A-Z0-9] + $ / )) { 

     

     Alert ( "E-mail format error" ); 



     mail.focus (); 

     return  to false ; 

} 

}
 </ Script> 
</ body> 
</ HTML>

interface:

Experimental Summary:

Some java web's basic functions can already be realized.

Operation of the database connection is now possible to achieve a good;

The first java web quizzes do okay

If you do not press the independent inquiry can get about 7 minutes;

After a lot of practice to write java web applet!

Guess you like

Origin www.cnblogs.com/zzmds/p/11715208.html