使用Java生成虚拟个人信息,姓名、性别 、年龄 、 出生日期、户籍地址、身份证号
姓名根据性别生成,身份证号根据户籍地址 、性别、出生日期 进行生成,生成数据与真人无差。
提供了两种生成方式,单个生成和批量生成。程序不太完善,仅仅完成了生成,整理数据和数据导出还请自行完善
使用时请自行创建库并导入数据表信息,然后修改数据库连接配置文件,sql文件在项目文件中,自行下载
- 使用相关jar包
- mysql-connector-java
- commons-dbutils
- com.alibaba.druid
- 数据库表
- area_code // 省/市/县/镇信息
- code // 省/市/县/镇的编码
- name // 省/市/县/镇名称
- level // 行政等级 1代表省 2代表市以此类推,总计五级
- pcode // 市/县/镇的上一级编码
- village //村庄信息
- id //无实际意义,仅用于随机查询时使用
- code // 村庄编码
- name // 村庄名字
- level // 行政等级
- pcode // 上级编码,也就是乡镇编码
- area_code // 省/市/县/镇信息
- 文件目录
- dao // 有关数据库查询的类
- AreaInfo.class // 用于从数据库查询生成地址信息
- BaseDao.class // 优化查询的工具类使用dbutils包
- domain // 实体类
- AreaCode.class //地址信息实体类
- Person.class // 个人信息实体类
- servile // 服务类
- IdCardGenerator.class //用于根据性别,地址,出生日期生成身份证号
- PersonInfo.class //生成个人信息
- utils
- JDBCUtil.class // 数据库连接工具类
- Test.class // 测试类
- druid.properties // druid数据库连接池配置文件
- dao // 有关数据库查询的类
AreaInfo.class
package dao;
import domain.AreaCode;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class AreaInfo {
/**
* 返回一个地址信息,返回值为AreaCode对象
*/
public AreaCode getArea() {
BaseDao baseDao = new BaseDao();
String sql = "SELECT\r\n" +
" province.`name` AS province,\r\n" +
" city.`name` AS city,\r\n" +
" country.`name` AS country,\r\n" +
" town.`name` AS town,\r\n" +
" village.`name` AS village, \r\n" +
" country.`code` AS countryCode \r\n" +
" FROM\r\n" +
" area_code province\r\n" +
" JOIN area_code city ON province.`code` = city.pcode\r\n" +
" JOIN area_code country ON city.`code` = country.pcode\r\n" +
" JOIN area_code town ON country.`code` = town.pcode\r\n" +
" JOIN village village ON town.`code` = village.pcode \r\n" +
" where village.code=(select code from village where id=?)";
//where village.code=(select code from village where id=?)
//通过village表的id(便于随机查询出一个村庄)查询出village的code,然后查询出村庄所在的省/市/县/镇,查询较快(大约0.02S,
// 避免了使用ORDER BY RAND()查询时间较长的问题,每次生成一个选它就对了!)
Object object = baseDao.query(sql, new BeanHandler(AreaCode.class), new Random().nextInt(633980));
if (object != null && object instanceof AreaCode) {
AreaCode area = (AreaCode) object;
return area;
} else {
System.out.println("查询结果为空" + object);
return null;
}
}
/**
* 根据查询参数生成指定数量的地址信息,返回值为AreaCode类型的List集合
*/
public List<AreaCode> getAreas(int num) {
BaseDao baseDao = new BaseDao();
String sql = "SELECT\r\n" +
" province.`name` AS province,\r\n" +
" city.`name` AS city,\r\n" +
" country.`name` AS country,\r\n" +
" town.`name` AS town,\r\n" +
" village.`name` AS village, \r\n" +
" country.`code` AS countryCode \r\n" +
" FROM\r\n" +
" area_code province\r\n" +
" JOIN area_code city ON province.`code` = city.pcode\r\n" +
" JOIN area_code country ON city.`code` = country.pcode\r\n" +
" JOIN area_code town ON country.`code` = town.pcode\r\n" +
" JOIN village village ON town.`code` = village.pcode \r\n" +
" ORDER BY RAND() LIMIT ?";
//使用ORDER BY RAND() LIMIT ? 查询时间较长2S左右,非批量生成建议不要使用
Object object = baseDao.query(sql, new BeanListHandler(AreaCode.class), num);
if (object != null && object instanceof List) {
ArrayList<AreaCode> areas = (ArrayList<AreaCode>) object;
return areas;
} else {
System.out.println("查询结果为空" + object);
return null;
}
}
}
BaseDao.class
package dao;
import org.apache.commons.dbutils.ResultSetHandler;
import utils.JDBCUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class BaseDao {
//优化查询
public Object query(String sql,ResultSetHandler<?> rsh,Object... params) {
Connection conn=null;
PreparedStatement pstmt=null;
ResultSet rs=null;
//定义一个返回结果
Object obj=null;
try {
//获得连接
conn=JDBCUtil.getConnection();
//预编译sql
pstmt=conn.prepareStatement(sql);
//将参数设置进去
for(int i=0 ; params != null && i < params.length ; i++) {
pstmt.setObject(i+1, params[i]);
}
//发送sql
rs=pstmt.executeQuery();
//让调用者去处理结果集
obj=rsh.handle(rs);
} catch (SQLException e) {
//释放
return new Exception(e.getMessage());
}finally {
JDBCUtil.getClose(pstmt, rs, conn);
}
return obj;
};
}
AreaCode.class
package domain;
public class AreaCode {
private String province;
private String city;
private String country;
private String town;
private String village;
private long countryCode;
public AreaCode() {
}
public AreaCode(String province, String city, String country, String town, String village, long countryCode) {
this.province = province;
this.city = city;
this.country = country;
this.town = town;
this.village = village;
this.countryCode = (long) Math.floor(countryCode/1000000);
}
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 getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getTown() {
return town;
}
public void setTown(String town) {
this.town = town;
}
public String getVillage() {
return village;
}
public void setVillage(String village) {
this.village = village;
}
public long getCountryCode() {
return countryCode;
}
public void setCountryCode(long countryCode) {
this.countryCode = (long) Math.floor(countryCode/1000000);
}
@Override
public String toString() {
return "AreaCode{" +
"province='" + province + '\'' +
", city='" + city + '\'' +
", country='" + country + '\'' +
", town='" + town + '\'' +
", village='" + village + '\'' +
", countryCode=" + countryCode +
'}';
}
}
Person.class
package domain;
import java.util.Calendar;
import java.util.Date;
/**
* @author Administrator
*/
public class Person {
private String name;
private String sex;
private int age;
private Calendar birthday;
public Person() {
}
public Person(String name, String sex, int age, Calendar birthday) {
this.name = name;
this.sex = sex;
this.age = age;
this.birthday = birthday;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Calendar getBirthday() {
return birthday;
}
public void setBirthday(Calendar birthday) {
this.birthday = birthday;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", sex='" + sex + '\'' +
", age=" + age +
", birthday=" + birthday +
'}';
}
}
IdCardGenerator.class
package service;
import dao.AreaInfo;
import domain.AreaCode;
import domain.Person;
import java.util.Calendar;
/**
* @author Administrator
*/
public class IdCardGenerator {
/**
* 生成方法
* @return
*/
public String generate(Person person, AreaCode area) {
StringBuilder generater = new StringBuilder();
generater.append(this.randomAreaCode(area));
generater.append(this.randomBirthday(person.getBirthday()));
generater.append(this.randomCode(person.getSex()));
generater.append(this.calcTrailingNumber(generater.toString().toCharArray()));
return generater.toString();
}
/**
* 随机地区
* @return
*/
public int randomAreaCode(AreaCode area) {
int code=(int)area.getCountryCode();
return code;
}
/**
* 随机出生日期
* @return
*/
public String randomBirthday(Calendar birthday) {
StringBuilder builder = new StringBuilder();
builder.append(birthday.get(Calendar.YEAR));
long month = birthday.get(Calendar.MONTH) + 1;
if (month < 10) {
builder.append("0");
}
builder.append(month);
long date = birthday.get(Calendar.DATE);
if (date < 10) {
builder.append("0");
}
builder.append(date);
return builder.toString();
}
/**
* <p>18位身份证验证</p>
* 根据〖中华人民共和国国家标准 GB 11643-1999〗中有关公民身份号码的规定,公民身份号码是特征组合码,由十七位数字本体码和一位数字校验码组成。
* 排列顺序从左至右依次为:六位数字地址码,八位数字出生日期码,三位数字顺序码和一位数字校验码。
* 第十八位数字(校验码)的计算方法为:
* 1.将前面的身份证号码17位数分别乘以不同的系数。从第一位到第十七位的系数分别为:7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 4 2
* 2.将这17位数字和系数相乘的结果相加。
* 3.用加出来和除以11,看余数是多少?
* 4.余数只可能有0 1 2 3 4 5 6 7 8 9 10这11个数字。其分别对应的最后一位身份证的号码为1 0 X 9 8 7 6 5 4 3 2。
* 5.通过上面得知如果余数是2,就会在身份证的第18位数字上出现罗马数字的Ⅹ。如果余数是10,身份证的最后一位号码就是2。
*/
public char calcTrailingNumber(char[] chars) {
if (chars.length < 17) {
return ' ';
}
int[] c = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };
char[] r = { '1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2' };
int[] n = new int[17];
int result = 0;
for (int i = 0; i < n.length; i++) {
n[i] = Integer.parseInt(chars[i] + "");
}
for (int i = 0; i < n.length; i++) {
result += c[i] * n[i];
}
return r[result % 11];
}
/**
* 随机产生3位数
* @return
*/
public String randomCode(String sex) {
//第三位为奇数代表男性,偶数带包女性
int code=0;
if ("男".equals(sex)){
code = (int) (Math.random() * 1000);
if (code%2==0){
code=code+1;
}
}else {
code = (int) (Math.random() * 1000);
if (code%2!=0){
code=code+1;
}
}
if (code < 10) {
return "00" + code;
} else if (code < 100) {
return "0" + code;
} else {
return "" + code;
}
}
}
PersonInfo.class
package service;
import domain.Person;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Random;
public class PersonInfo {
String familyName = "赵钱孙李周吴郑王冯陈褚卫蒋沈韩杨朱秦尤许何吕施张孔曹严华金魏陶姜戚谢邹喻水云苏潘葛奚范彭郎鲁韦昌马苗凤花方俞任袁柳鲍史唐费岑薛雷贺倪汤滕殷罗毕郝邬安常乐于时傅卞齐康伍余元卜顾孟平"
+ "黄和穆萧尹姚邵湛汪祁毛禹狄米贝明臧计成戴宋茅庞熊纪舒屈项祝董粱杜阮席季麻强贾路娄危江童颜郭梅盛林刁钟徐邱骆高夏蔡田胡凌霍万柯卢莫房缪干解应宗丁宣邓郁单杭洪包诸左石崔吉"
+ "龚程邢滑裴陆荣翁荀羊甄家封芮储靳邴松井富乌焦巴弓牧隗山谷车侯伊宁仇祖武符刘景詹束龙叶幸司韶黎乔苍双闻莘劳逄姬冉宰桂牛寿通边燕冀尚农温庄晏瞿茹习鱼容向古戈终居衡步都耿满弘国文东殴沃曾关红游盖益桓公晋楚闫";
String firstName2 = "欧阳太史端木上官司马东方独孤南宫万俟闻人夏侯诸葛尉迟公羊赫连澹台皇甫宗政濮阳公冶太叔申屠公孙慕容仲孙钟离长孙宇文司徒鲜于司空闾丘子车亓官司寇巫马公西颛孙壤驷公良漆雕乐正宰父谷梁拓跋夹谷轩辕令狐段干百里呼延东郭南门羊舌微生公户公玉公仪梁丘公仲公上公门公山公坚左丘公伯西门公祖第五公乘贯丘公皙南荣东里东宫仲长子书子桑即墨达奚褚师吴铭";
String girlName = "秀娟英华慧巧美娜静淑惠珠翠雅芝玉萍红娥玲芬芳燕彩春菊兰凤洁梅琳素云莲真环雪荣爱妹霞香月莺媛艳瑞凡佳嘉琼勤珍贞莉桂娣叶璧璐娅琦晶妍茜秋珊莎锦黛青倩婷姣婉娴瑾颖露瑶怡婵雁蓓纨仪荷丹蓉眉君琴蕊薇菁梦岚苑婕馨瑗琰韵融园艺咏卿聪澜纯毓悦昭冰爽琬茗羽希宁欣飘育滢馥筠柔竹霭凝晓欢霄枫芸菲寒伊亚宜可姬舒影荔枝思丽";
String boyName = "伟刚勇毅俊峰强军平保东文辉力明永健世广志义兴良海山仁波宁贵福生龙元全国胜学祥才发武新利清飞彬富顺信子杰涛昌成康星光天达安岩中茂进林有坚和彪博诚先敬震振壮会思群豪心邦承乐绍功松善厚庆磊民友裕河哲江超浩亮政谦亨奇固之轮翰朗伯宏言若鸣朋斌梁栋维启克伦翔旭鹏泽晨辰士以建家致树炎德行时泰盛雄琛钧冠策腾楠榕风航弘";
public String randFamilyName() {
String str = "";
int strLen;
int randNum = new Random().nextInt(2) + 1;
int index;
if (randNum == 1) {
strLen = familyName.length();
index = new Random().nextInt(strLen);
str = String.valueOf(familyName.charAt(index));
} else {
strLen = firstName2.length();
index = new Random().nextInt(strLen);
if (index % 2 == 0) {
str = firstName2.substring(index, index + 2);
} else {
str = firstName2.substring(index - 1, index + 1);
}
}
return str;
}
/**
* 随机生成一个性别
* @return
*/
public String randSex() {
int randNum = new Random().nextInt(2) + 1;
return randNum == 1 ? "男" : "女";
}
/**
* 随机根据性别生成名字
* @param sex
* @return
*/
public String randName(String sex) {
String name = "";
int randNum = new Random().nextInt(2) + 1;
int index;
if (sex.equals("男")) {
int strLen = boyName.length();
if (randNum % 2 == 0) {
index = new Random().nextInt(strLen - 1);
name = boyName.substring(index, index + randNum).concat("-男");
} else {
index = new Random().nextInt(strLen);
name = boyName.substring(index, index + randNum).concat("-男");
}
} else {
int strLen = girlName.length();
if (randNum % 2 == 0) {
index = new Random().nextInt(strLen - 1);
name = girlName.substring(index, index + randNum).concat("-女");
} else {
index = new Random().nextInt(strLen);
name = girlName.substring(index, index + randNum).concat("-女");
}
}
return name;
}
/**
* 随机产生一个出生日期,类型为Calender
* @return
*/
public Calendar getBirthday(){
Calendar birthday = Calendar.getInstance();
birthday.set(Calendar.YEAR, (int) (Math.random() * 60) + 1950);
birthday.set(Calendar.MONTH, (int) (Math.random() * 12));
birthday.set(Calendar.DATE, (int) (Math.random() * 31));
return birthday;
}
/**
* 获取年龄
* @param birthday
* @return
*/
public int randAge(Calendar birthday) {
System.out.println();
return (Calendar.getInstance().get(Calendar.YEAR)-birthday.get(Calendar.YEAR));
}
/**
* 返回一个Person对象
* @return
*/
public Person getPerson() {
Person person = new Person();
PersonInfo personInfo = new PersonInfo();
// 姓氏随机生成
String familyName = personInfo.randFamilyName();
// 名字依托于性别产生
String randName = personInfo.randName(personInfo.randSex());
String[] fixed = randName.split("-");
String name = fixed[0];
String sex = fixed[1];
Calendar birthday = personInfo.getBirthday();
//年龄根据出生日日期计算
int age = personInfo.randAge(birthday);
person.setName(familyName.concat(name));
person.setSex(sex);
person.setAge(age);
person.setBirthday(birthday);
return person;
}
public List<Person> getPersons(int num) {
ArrayList<Person> persons=new ArrayList<Person>();
Person person = new Person();
PersonInfo personInfo = new PersonInfo();
for (int i = 0; i < num; i++) {
// 姓氏随机生成
String familyName = personInfo.randFamilyName();
// 名字依托于性别产生
String randName = personInfo.randName(personInfo.randSex());
String[] fixed = randName.split("-");
String name = fixed[0];
String sex = fixed[1];
Calendar birthday = personInfo.getBirthday();
//年龄根据出生日期计算
int age = personInfo.randAge(birthday);
persons.add(new Person(familyName.concat(name),sex,age,birthday));
}
return persons;
}
}
JDBCUtil.class
package utils;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class JDBCUtil {
private static DataSource ds;
static{
try {
Properties pro=new Properties();
pro.load(JDBCUtil.class.getClassLoader().getResourceAsStream("druid.properties"));
ds= DruidDataSourceFactory.createDataSource(pro);
} catch (Exception e) {
e.printStackTrace();
}
}
public static Connection getConnection() throws SQLException {
return ds.getConnection();
}
public static void getClose(Statement stmt, Connection conn){
try {
if (stmt!=null){
stmt.close();
}
if (conn!=null){
conn.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void getClose(ResultSet res,Connection conn){
try {
if (res!=null){
res.close();
}
if (conn!=null){
conn.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void getClose(Statement stmt,ResultSet res,Connection conn) {
try {
if(stmt!=null) {
stmt.close();
}
if (res!=null){
res.close();
}
if (conn!=null){
conn.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static DataSource getDataSource(){
return ds;
}
}
Test.class
import dao.AreaInfo;
import domain.AreaCode;
import domain.Person;
import service.IdCardGenerator;
import service.PersonInfo;
public class Test {
public static void main(String[] args) {
PersonInfo personInfo = new PersonInfo();
AreaInfo areaInfo = new AreaInfo();
IdCardGenerator idCardGenerator = new IdCardGenerator();
//一次生成一个循环生成,此生成方法会使用大量SQL
for (int i = 0; i < 1000; i++) {
try {
//由于循环过快导致SQL查询过多导致数据库连接池等待超时,每次查询大约40毫秒,为避免异常程序中断此处休眠80毫秒
Thread.sleep(80);
} catch (InterruptedException e) {
e.printStackTrace();
}
AreaCode area = areaInfo.getArea();
Person person = personInfo.getPerson();
String generate = idCardGenerator.generate(person,area);
System.out.println(person.toString());
System.out.println(area.toString());
System.out.println(generate);
}
//一次生成指定数量只用一次SQL
// List<AreaCode> areas = areaInfo.getAreas(100000);
// List<Person> persons = randInfo.getPersons(100000);
// for (int i = 0; i < persons.size(); i++) {
// AreaCode area = areas.get(i);
// Person person = persons.get(i);
// String generate = idCardGenerator.generate(person,area);
// System.out.println(person.toString());
// System.out.println(area.toString());
// System.out.println(generate);
// }
}
}
druid.properties
driverClassName=com.mysql.cj.jdbc.Driver //根据你的MySql版本进行设置5.X和8.X的是不一样的
url=jdbc:mysql://数据库地址:端口号/数据库名?serverTimezone=UTC
username=数据库登录用户名
password=数据库密码
//初始化连接数
initialSize=5
//最大连接数
maxActive=10
//最大超时时间(毫秒)
maxWait=3000
maven jar包依赖配置信息,不使用maven可忽视
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.25</version>
</dependency>
<dependency>
<groupId>commons-dbutils</groupId>
<artifactId>commons-dbutils</artifactId>
<version>1.7</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.8</version>
</dependency>
</dependencies>
声明:若发现资源失效或错误可在文章顶部“评论建议”留言,我们将尽快处理!本站绝大部分资源都来自互联网,原作者信息已无从考究。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。