根据身份证号码自动获取出生日期,性别,籍贯

Q1:数据库

由于数据有点多,我们在这就不展示了(需要的联系我,我发给你)

i

这是里面所用到的一些架包,包等等。相信大家对这一块都已经了解了,我就不说废话了。

Q2:html文件

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
    <script src=js/jquery-1.11.3.js></script>
    身份证号码:<input type="text" id="idCard"/>
    出生日期:<input type="text" id="birthday"/>
    性别:<input type="radio" name="sex" value="男" id="nan"/>男<input type="radio" name="sex" value="女" id="nv"/>女
    籍贯:<input type="text" id="place"/>
    <input type="button" value="确定" id="btnok"/>
    <script>
        var app={
                select:function(){
                    var cardid=$("#idCard").val();
                    var place=cardid.substring(0,6);
                    $.ajax({
                        type:'post',
                        url:'S_area_codeaction?action=select',
                        data:{areaCode:place},
                        success:function(data){
                            console.log(data);
                            $("#place").val(data[0].detail);
                        }
                    });
                },
                start:function(){
                    $("#btnok").click(function(){
                        var cardid=$("#idCard").val();
                        var nian=cardid.substring(6,10);//1998
                        var yue=cardid.substring(10,12);
                        var ri=cardid.substring(12,14);
                        var birthdays=nian+"-"+yue+"-"+ri;
                        /* var a=IsDate(birthdays); */
                        /* if(a){ */
                            $("#birthday").val(nian+"年"+yue+"月"+ri+"日");
                            var sex=cardid.substring(16,17);
                            if(sex%2==0){
                                $("#nv").prop("checked",true);
                            }else{
                                $("#nan").prop("checked",true);
                            }
                            app.select();    
                        /* } */
                    });
                }
        };
        app.start();
      //下面可以判断日期:
/* function IsDate(str) { arr = str.split("-"); if(arr.length == 3) { intYear = parseInt(arr[0],10); intMonth = parseInt(arr[1],10); intDay = parseInt(arr[2],10); if(isNaN(intYear) || isNaN(intMonth) || isNaN(intDay)) { return false; } if(intYear > 2100 || intYear < 1900 || intMonth > 12 || intMonth < 0 || intDay > 31 || intDay < 0) { return false; } if((intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) && intDay > 30) { return false; } if(intYear % 100 == 0 && intYear % 400 || intYear % 100 && intYear % 4 == 0) { if(intDay > 29) return false; } else { if(intDay > 28) return false; } return true; } return false; } */ </script> </body> </html>

Q3:Servlet(S_area_codeaction)

package action;

import java.io.IOException;
import java.util.ArrayList;
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;

import com.bo.S_area_codeboimpl;
import com.vo.S_area_code;

import jsonUtil.JsonUtils;

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

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/json;charset=utf-8");
        String action=request.getParameter("action");
        if(action.equals("select")) {
            select(request,response);
        }
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
    //查询
    public void select(HttpServletRequest request, HttpServletResponse response) {
        S_area_codeboimpl si=new S_area_codeboimpl();
        int areaCode=Integer.parseInt(request.getParameter("areaCode"));
        List<S_area_code> lists=si.getAllList(areaCode);
        List<S_area_code> list=new ArrayList<S_area_code>();
        for(S_area_code item:lists) {
            list.add(new S_area_code(item.getID(),item.getAreaCode(),item.getProvince(),item.getCity(),item.getDistrict(),item.getDetail()));
        }
        try {
            response.getWriter().print(JsonUtils.toJson(list));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

Q4:JsonUtils(注释出现了乱码,大家理解一下。当然也可以将改正的方法发给我,谢了。)

package jsonUtil;

import java.text.SimpleDateFormat;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class JsonUtils {
    /**
     *  搴忓垪鍖栨垚json
     * */
    public static String toJson(Object obj) {
        // 瀵硅薄鏄犲皠鍣�
        ObjectMapper mapper = new ObjectMapper();
        String result = null;
        // 搴忓垪鍖杣ser瀵硅薄涓簀son瀛楃涓�
        try {
            result = mapper.writeValueAsString(obj);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return result;
    }
    
    /**
     * 鍙嶅簭鍒楀寲鎴愬璞�
     * */
    public static <T> T toObject(String json,Class<T> valueType) {
        //瀵硅薄鏄犲皠鍣�
        ObjectMapper mapper=new ObjectMapper();
        T result=null;
        try {
            result=mapper.readValue(json,valueType);

        }catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
}

 Q5:com.vo

package com.vo;

public class S_area_code {
    int ID;
    int areaCode;
    String province;
    String city;
    String district;
    String detail;
    public S_area_code() {}
    public S_area_code(int iD, int areaCode, String province, String city, String district, String detail) {
        super();
        ID = iD;
        this.areaCode = areaCode;
        this.province = province;
        this.city = city;
        this.district = district;
        this.detail = detail;
    }
    public int getID() {
        return ID;
    }
    public void setID(int iD) {
        ID = iD;
    }
    public int getAreaCode() {
        return areaCode;
    }
    public void setAreaCode(int areaCode) {
        this.areaCode = areaCode;
    }
    public String getProvince() {
        return province;
    }
    public void setProvince(String province) {
        this.province = province;
    }
    public String getCity() {
        return city;
    }
    public void setCity(String city) {
        this.city = city;
    }
    public String getDistrict() {
        return district;
    }
    public void setDistrict(String district) {
        this.district = district;
    }
    public String getDetail() {
        return detail;
    }
    public void setDetail(String detail) {
        this.detail = detail;
    }
    
}

 

 Q6:com.dao

package com.dao;

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

import com.vo.S_area_code;

import util.DBUtil;

public class S_area_codedaoimpl {
    public List<S_area_code> getAllList(int areaCode){
        String sql="select * from s_area_code where areaCode=?";
        ResultSet rs=(ResultSet)DBUtil.execute(sql, new Object[] {areaCode});
        List<S_area_code> list=new ArrayList<S_area_code>();
        S_area_code model=null;
        try {
            while(rs.next()) {
                model=new S_area_code();
                model.setID(rs.getInt(1));
                model.setAreaCode(rs.getInt(2));
                model.setProvince(rs.getString(3));
                model.setCity(rs.getString(4));
                model.setDistrict(rs.getString(5));
                model.setDetail(rs.getString(6));
                list.add(model);
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return list;
    }
}


 

 Q7:com.bo

package com.bo;

import java.util.List;

import com.dao.S_area_codedaoimpl;
import com.vo.S_area_code;

public class S_area_codeboimpl {
    S_area_codedaoimpl si=new S_area_codedaoimpl();
    public List<S_area_code> getAllList(int areaCode){
        return si.getAllList(areaCode);
    }
}

Q8:DBUtil

package util;

import java.io.IOException;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

public class DBUtil {
    //连接对象
    //Statement 命令对象
    //打开连接
    //关闭连接
    //得到一个连接对象
    //查询(有参,无参)
    //修改(有参,无参)
    
    static Connection conn = null;
    static Statement stmt = null;
    //驱动,服务器地址,登录用户名,密码    
    static String DBDRIVER;
    static String DBURL;
    static String DBUSER;
    static String DBPWD;
    
    static {
        //先创建资源文件,扩展名为.properties
        //内容是以:dbuser=sa  格式
        Properties prop = new Properties();//先获取资源对象
        try {
            prop.load(Thread.currentThread().getContextClassLoader().
                    getResourceAsStream("resources/dbconfig.properties"));
            DBDRIVER = prop.getProperty("DBDRIVER");
            DBURL = prop.getProperty("DBURL");
            DBUSER = prop.getProperty("DBUSER");
            DBPWD = prop.getProperty("DBPWD");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 
    }
    
    //打开连接
    public static void open() {
        //加载驱动
        try {
            Class.forName(DBDRIVER);
            conn=DriverManager.getConnection(DBURL,DBUSER,DBPWD);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
                e.printStackTrace();
        }
    }
    //关闭连接
    public static void close() {
        try {
            if(stmt!=null && stmt.isClosed())
                    stmt.close();
            if(conn!=null && !conn.isClosed())
                conn.close();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    } 
    //得到一个连接对象,当用户使用DBUtil无法解决个性问题时
    //可以通过本方法获得连接对象
    public static Connection getConnection() {
        try {
            if(conn==null ||conn.isClosed())
                open();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }
    
    //executeQuery
    //executeUpdate
    //execute
    //获得查询的数据集
    //不带参数的查询
    //select * from student where name='' and sex=''
    public static ResultSet executeQuery(String sql) {
        try {
            open();//保证连接是成功的
            stmt = conn.createStatement();
            return stmt.executeQuery(sql);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }
    
    //修改表格内容
    public static int executeUpdate(String sql) {
        int result = 0;
        try {
            open();//保证连接是成功的
            stmt = conn.createStatement();
            result = stmt.executeUpdate(sql);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            close();
        }
        return result;
    }
    //如果执行的查询或存储过程,会返回多个数据集,或多个执行成功记录数
    //可以调用本方法,返回的结果,
    //是一个List<ResultSet>或List<Integer>集合
    public static Object execute(String sql) {
        boolean b=false;
        try {
            open();//保证连接是成功的
            stmt = conn.createStatement();
            b = stmt.execute(sql);        
            //true,执行的是一个查询语句,我们可以得到一个数据集
            //false,执行的是一个修改语句,我们可以得到一个执行成功的记录数
            if(b){
                return stmt.getResultSet();
            }
            else {
                return stmt.getUpdateCount();
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            if(!b) {
                close();
            }
        }
        return null;
    }
    
    // 
    //select * from student where name=? and sex=?
    //带参数的查询,只有输入参数
    public static ResultSet executeQuery(String sql,Object[] in) {
        try {
            open();//保证连接是成功的
            PreparedStatement pst = conn.prepareStatement(sql);
            for(int i=0;i<in.length;i++)
                pst.setObject(i+1, in[i]);
            stmt = pst;//只是为了关闭命令对象pst
            return pst.executeQuery();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }
    //带参数修改,只有输入参数
    public static int executeUpdate(String sql,Object[] in) {
        try {
            open();//保证连接是成功的
            PreparedStatement pst = conn.prepareStatement(sql);
            for(int i=0;i<in.length;i++)
                pst.setObject(i+1, in[i]);
            stmt = pst;//只是为了关闭命令对象pst
            return pst.executeUpdate();
        } catch (SQLException e) {
            System.out.println(e.getMessage());
            //e.printStackTrace();
        }finally {
            close();
        }
        return 0;
    }
    public static Object execute(String sql,Object[] in) {
        boolean b=false;
        try {
            open();//保证连接是成功的
            PreparedStatement pst = conn.prepareStatement(sql);
            for(int i=0;i<in.length;i++)
                pst.setObject(i+1, in[i]);
            b = pst.execute();
            //true,执行的是一个查询语句,我们可以得到一个数据集
            //false,执行的是一个修改语句,我们可以得到一个执行成功的记录数
            if(b){
                System.out.println("----");
                /*List<ResultSet> list = new ArrayList<ResultSet>();
                list.add(pst.getResultSet());
                while(pst.getMoreResults()) {
                    list.add(pst.getResultSet());
                }*/
                return pst.getResultSet();
            }
            else {
                System.out.println("****");
                List<Integer> list = new ArrayList<Integer>();
                list.add(pst.getUpdateCount());
                while(pst.getMoreResults()) {
                    list.add(pst.getUpdateCount());
                }
                return list;
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            if(!b) {
                System.out.println("====");
                close();
            }
        }
        return null;
    }
    //调用存储过程  proc_Insert(?,?,?)
    public static Object executeProcedure(String procName,Object[] in) {
        open();
        try {
            procName = "{call "+procName+"(";
            String link="";
            for(int i=0;i<in.length;i++) {
                procName+=link+"?";
                link=",";
            }
            procName+=")}";
            CallableStatement cstmt = conn.prepareCall(procName);
            for(int i=0;i<in.length;i++) {
                cstmt.setObject(i+1, in[i]);
            }
            if(cstmt.execute())
            {
                return cstmt.getResultSet();
            }
            else {
                return cstmt.getUpdateCount();
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        return null;
    }
    

    /*
     * 调用存储过程,并有输出参数
     * @procName ,存储过程名称:proc_Insert(?,?)
     * @in ,输入参数集合
     * @output,输出参数集合
     * @type,输出参数类型集合
     * */
    public static Object executeOutputProcedure(String procName,
            Object[] in,Object[] output,int[] type){
        Object result = null;
        try {
            CallableStatement cstmt = conn.prepareCall("{call "+procName+"}");
            //设置存储过程的参数值
            int i=0;
            for(;i<in.length;i++){//设置输入参数
                cstmt.setObject(i+1, in[i]);
                //print(i+1);
            }
            int len = output.length+i;
            for(;i<len;i++){//设置输出参数
                cstmt.registerOutParameter(i+1,type[i-in.length]);
                //print(i+1);
            }
            boolean b = cstmt.execute();
            //获取输出参数的值
            for(i=in.length;i<output.length+in.length;i++)
                output[i-in.length] = cstmt.getObject(i+1);
            if(b) {
                result = cstmt.getResultSet();
            }
            else {
                result = cstmt.getUpdateCount();
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return result;
    }
    //调用存储过程查询出所有的值
        public static Object executeProcedures(String procName) {
            open();
            try {
                procName = "{call "+procName+"}";
                CallableStatement cstmt = conn.prepareCall(procName);
                if(cstmt.execute())
                {
                    return cstmt.getResultSet();
                }
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return null;
        }
        public static void main(String[] msg) {
            open();
        }
}

Q9:配置文件

DBDRIVER=com.mysql.jdbc.Driver
DBURL=jdbc:mysql://localhost:3306/areadb?serverTimezone=GMT%2B8
DBUSER=root
DBPWD=

注意:此数据库使用的是MySql数据库

该有的代码我都贴了,除了一些架包,这个,需要的q我,我发给你。

结果:

输入身份证号码后,当然这里我的身份证号码被我去掉了,你懂的!

这样,在今后项目中,给用户的体验感就会很强,只需要身份证号码就行。

 

posted @ 2018-07-25 20:20  zywds  阅读(982)  评论(0编辑  收藏  举报