mashuxia

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
package com.util;
 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
 
/*
 * 鏁版嵁搴撹繛鎺ュ伐鍏�
 */
public class DBUtil {
     
    public static String db_url = "jdbc:mysql://localhost:3306/test?useSSL=false&useUnicode=true&characterEncoding=UTF-8";
    public static String db_user = "root";
    public static String db_pass = "123";
     
    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;
    }
     
    /*10鍏抽棴杩炴帴*/
    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 (SQLException e) {
                e.printStackTrace();
            }
        }
    }
 
    public static void main(String[] args) throws SQLException {
        Connection conn = getConn();
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        String sql ="select * from user";
        pstmt = conn.prepareStatement(sql);
        rs = pstmt.executeQuery();
        if(rs.next()){
            System.out.println("绌�");
        }else{
            System.out.println("涓嶇┖");
        }
    }
}

  

package com.dao;

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

import com.entry.user;
import com.util.DBUtil;

public class userDao {
	//娣诲姞
		public boolean add(user user) {
	        String sql = "insert into user2(id,password,sex,name,no,mail,xy,x,bj,nf,syd,bz) "
	        		+ "values('" + user.getId() + "','"  + user.getPassword() + "','"  + user.getSex() + "','"  + user.getName() + "','"  + user.getNo() + "','"  + user.getMail() + "','"  + user.getXy() + "','"  + user.getX() + "','"  + user.getBj() + "','"  + user.getNf() + "','"  + user.getSyd() + "','"  + user.getBz() + "')";
	        //鍒涘缓鏁版嵁搴撻摼鎺�
	        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;
	    }
		
		
		 /*
	     * 楠岃瘉鍚嶇О鏄惁鍞竴
	     * true --- 涓嶅敮涓�
	     */
	    public boolean id(String id) {
	        boolean f = false;
	        String sql = "select id from user2 where name = '" + id + "'";
	        //c鍒涘缓鏁版嵁搴撹繛鎺�
	        Connection conn = DBUtil.getConn();
	        Statement state = null;
	        ResultSet rs = null;       
	        try {
	            state = conn.createStatement();
	            rs = state.executeQuery(sql);
	            while (rs.next()) {
	                f = true;
	            }
	        } catch (SQLException e) {
	            e.printStackTrace();
	        } finally {
	            DBUtil.close(rs, state, conn);
	        }
	        return f;
	    }
}

  

package com.service;

import com.dao.userDao;
import com.entry.user;

public class userservice {
userDao cDao = new userDao();
    
    /*娣诲姞*/
    public boolean add(user user) {
        boolean f = false;
        if(!cDao.id(user.getId())) {
            cDao.add(user);
            f = true;
        }
        return f;
    }
    
    
    
}

  

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>Insert title here</title>
<meta charset="UTF-8">
	<title>注册页面</title>
</head>
<body>
    <%
         Object message = request.getAttribute("message");
         if(message!=null && !"".equals(message)){
     
    %>
         <script type="text/javascript">
              alert("<%=request.getAttribute("message")%>");
         </script>
    <%} %>
    <div align="center">
        <h6 style="color: black;">注册</h6>
     
        <form name = "form1" action="AddServlet?method=add" method="post" onsubmit="return check_submit()">
	<table>
			<tr>
				<td>登录账号</td>
				<td colspan="2"> <input type="text" id="id" name="id"  maxlength="12" onblur="blur_id()" onfocus="focus_id()"/></td>
			    <td width="300"><div id="result"></td>
			</tr>
			<tr>
				<td>登录密码</td>
				<td colspan="2"> <input type="password" id="password" name="password" onblur="blur_pass()" onfocus="focus_pass()" /></td>
				<td width="300"><div id="result1"></td>
			</tr>
			<tr>
				<td>性别:</td>
				<td colspan="2">
              <select name="sex"id="sex" >
              <option value="男" >男</option>
              <option value="女" >女</option>
            </select></td>
			</tr>
			<tr>
				<td>姓名</td>
				<td colspan="2"><input type="text" id="name" name="name" /></td>
			</tr>
			<tr>
				<td>学号</td>
				<td colspan="2"><input style="ime-mode:Disabled" maxlength="8" type="text" id="no" name="no" onblur="blur_phone()" onfocus="focus_phone()"/></td>
			     <td width="300"><div id="result_name"></td>
			</tr><tr>
				<td>电子邮件</td>
				<td colspan="2"><input type="email" id="mail" name="mail" ></td>
			</tr>
			<tr>
				<td>所在学院</td>
				<td colspan="2"><input type="text" id="xy" name="xy" ></td>
			</tr>
			<tr>
				<td>所在系</td>
				<td colspan="2"><input type="text" id="x" name="x" ></td>
			</tr>
			<tr>
				<td>所在班级</td>
				<td colspan="2"><input type="text" id="bj" name="bj" ></td>
			</tr>
			<tr>
				<td>入学年份(届):</td>
				<td colspan="2">
              <select name="nf"id="nf" >
              <option value="2017" >2017</option>
              <option value="2018" >2018</option>
            </select></td>
			</tr>
			<tr>
				<td>生源地:</td>
				<td colspan="2"><input type="text" id="syd" name="syd" ></td>
			</tr>
			<tr>
				<td>备注:</td>
				<td colspan="2"><input type="text" id="bz" name="bz" ></td>
			</tr>
			
			<tr>
			<td colspan="3"> <button type="submit" class="b">保   存</button>         
                <button type="reset" class="b">重   置</button></td>
			
			</tr>
	</table>
	</form>
    </div>
    <script type="text/javascript">
/*
    表单验证
*/
var flag = false;   // flag 如果为true(即用户名合法)就允许表单提交, 如果为false(即用户名不合法)阻止提交
function focus_pass()
{
	var nameObj = document.getElementById("result1");
    nameObj.innerHTML = "由八位以上字符和数字组成";
    nameObj.style.color="#999";
	}
function blur_pass()
{
	var nameObj = document.getElementById("result1");
    // 判断用户名是否合法
    var str2 = check_user_pass(document.form1.password.value);
    nameObj.style.color="red";
    if ("密码合法" ==  str2)
    {
        flag = true;
        nameObj.innerHTML = str2;
    }
    else
    {
        nameObj.innerHTML = str2;
    }
}

function check_user_pass(str)
{  var str2 = "密码合法";
if ("" == str)
{
    str2 = "密码为空";
    return str2;
}
else if (str.length<8)
{
    str2 = "密码应为8位组成";
    return str2;
}
else if (!check_word(str))
{
    str2 = "未含有英文字符";
    return str2;
}

return str2;
	
	
	}





function focus_id()
{
	var nameObj = document.getElementById("result");
    nameObj.innerHTML = "由六到十二英文字符和数字组成";
    nameObj.style.color="#999";
	}
function blur_id()
{
	var nameObj = document.getElementById("result");
    // 判断用户名是否合法
    var str2 = check_user_id(document.form1.id.value);
    nameObj.style.color="red";
    if ("用户名合法" ==  str2)
    {
        flag = true;
        nameObj.innerHTML = str2;
    }
    else
    {
        nameObj.innerHTML = str2;
    }
}

function check_user_id(str)
{
    var str2 = "用户名合法";
    if ("" == str)
    {
        str2 = "用户名号为空";
        return str2;
    }
    else if ((str.length<=4)||(str.length>=12))
    {
        str2 = "用户名应是六到十二位组成";
        return str2;
    }
    else if (!check_word(str))
    {
        str2 = "未含有英文字符";
        return str2;
    }
    else if(!check_firstword(str))
    {
    	str2 = "必须以英文字母开头";
        return str2;
    }
    return str2;
}

// 当鼠标聚焦于用户名
function focus_phone()
{
    // 找到后面的div, id = result_name
    var nameObj = document.getElementById("result_name");
    nameObj.innerHTML = "学号为8位数字且以2018开头";
    nameObj.style.color="#999";
}
// 当鼠标不聚焦于用户名input
function blur_phone()
{
    // 找到id=result_name的div
    var nameObj = document.getElementById("result_name");
    // 判断用户名是否合法
    var str2 = check_user_name(document.form1.no.value);
    nameObj.style.color="red";
    if ("手机号合法" ==  str2)
    {
        flag = true;
        nameObj.innerHTML = str2;
    }
    else
    {
        nameObj.innerHTML = str2;
    }

}   
// 检查用户名是否合法        合法就返回"该用户名合法"
function check_user_name(str)
{
    var str2 = "学号合法";
    if ("" == str)
    {
        str2 = "学号为空";
        return str2;
    }
    else if (str.length!=8)
    {
        str2 = "学号必须为8位";
        return str2;
    }
    else if (check_other_char(str))
    {
        str2 = "不能含有特殊字符";
        return str2;
    }
    return str2;
}
function check_firstword(str)
{   var arr = ["a", "b", "c", "d", "e", "f", "g", "h","i","j", "k", "l", "m", "n", "o", "p", "q","r", "s", "t", "u", "v", "w", "x", "y","z","A", "B", "C", "D", "E", "F", "G", "H","I","J", "K", "L", "M", "N", "O", "P", "Q","R","S", "T", "U", "V", "W", "X", "Y", "Z"];
for (var i = 0; i < arr.length; i++)
{
        if (arr[i] == str.charAt(0))
        {
            return true;
        }
}   
return false;
	}


function check_word(str)
{   var arr = ["a", "b", "c", "d", "e", "f", "g", "h","i","j", "k", "l", "m", "n", "o", "p", "q","r", "s", "t", "u", "v", "w", "x", "y","z","A", "B", "C", "D", "E", "F", "G", "H","I","J", "K", "L", "M", "N", "O", "P", "Q","R","S", "T", "U", "V", "W", "X", "Y", "Z"];
for (var i = 0; i < arr.length; i++)
{
    for (var j = 0; j < str.length; j++)
    {
        if (arr[i] == str.charAt(j))
        {
            return true;
        }
    }
}   
return false;
	}

// 验证用户名是否含有特殊字符
function check_other_char(str)
{
    var arr = ["&", "\\", "/", "*", ">", "<", "@", "!"];
    for (var i = 0; i < arr.length; i++)
    {
        for (var j = 0; j < str.length; j++)
        {
            if (arr[i] == str.charAt(j))
            {
                return true;
            }
        }
    }   
    return false;
}
// 根据验证结果确认是否提交
function check_submit()
{
    if (flag == false)
    {
        return false;
    }
    return true;
}
</script>
</head>
</body>
</html>

  

posted on 2019-10-21 21:26  mamiaomiao  阅读(172)  评论(0编辑  收藏  举报