public class AnalysisUsers {
private static SessionFactory sessionFactory;
public static void main(String[] args) throws Exception{
//idCard:18位,前两位:
/*11北京,12,天津,13 河北;14 山西shanxi;内蒙古 15;辽宁省|210000,吉林省|220000,黑龙江省|230000,
上海市|310000,江苏省|320000,浙江省|330000,安徽省|340000,福建省|350000,江西省|360000,山东省|370000,
河南省|410000,湖北省|420000,湖南省|430000,广东省|440000,广西壮族自治区|450000,海南省|460000,
重庆市|500000,四川省|510000,贵州省|520000,云南省|530000,西藏自治区|540000,
陕西省shanxi2|610000,甘肃省|620000,青海省|630000,宁夏回族自治区|640000,新疆维吾尔自治区|650000,台湾省(886)|710000,
香港特别行政区(852)|810000,澳门特别行政区(853)|820000
*/
double beijingCount=0,tianjin = 0,hebei=0,shanxi=0,neimeng=0,liaoning=0,jilin=0,heilongjiang=0;
double shanghai = 0,jiangsu = 0,zhejiang = 0,anhui = 0,fujian = 0,jiangxi = 0,shandong = 0;
double henan = 0,hubei = 0,hunan = 0,guangdong = 0,guangxi = 0,hainan = 0;
double chongqing = 0,sichuan = 0,guichou = 0,yunnan = 0,xizang = 0;
double shanxi2 = 0,gansu = 0,qinghai = 0,ningxia = 0,xinjiang = 0,taiwan = 0;
double xianggang = 0,aomen = 0;
double others=0;
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://127.0.0.1:3306/test";
String user = "root",password = "1111111111";
java.sql.Connection conn = DriverManager.getConnection(url,user,password);
java.sql.Statement sql=conn.createStatement();
String sqlString = "select idCard from t_user ";
java.sql.ResultSet allIdCards_Prov = sql.executeQuery(sqlString);
//1、统计省份人数
while(allIdCards_Prov.next()){
String idCard = new String();
String buff=new String();
idCard += allIdCards_Prov.getString(1).toString();
if(idCard.length() > 2)
{
buff += idCard.substring(0,2);
}
else {
buff = "90";
}
switch (buff) {
case "11":
beijingCount++;
break;
case "12":
tianjin++;
break;
case "13":
hebei++;
break;
case "14":
shanxi++;
break;
case "15":
neimeng++;
break;
case "21":
liaoning++;
break;
case "22":
jilin++;
break;
case "23":
heilongjiang++;
break;
case "31":
shanghai++;
break;
case "32":
jiangsu++;
break;
case "33":
zhejiang++;
break;
case "34":
anhui++;
break;
case "35":
fujian++;
break;
case "36":
jiangxi++;
break;
case "37":
shandong++;
break;
case "41":
henan++;
break;
case "42":
hubei++;
break;
case "43":
hunan++;
break;
case "44":
guangdong++;
break;
case "45":
guangxi++;
break;
case "46":
hainan++;
break;
case "50":
chongqing++;
break;
case "51":
sichuan++;
break;
case "52":
guichou++;
break;
case "53":
yunnan++;
break;
case "54":
xizang++;
break;
case "61":
shanxi2++;
break;
case "62":
gansu++;
break;
case "63":
qinghai++;
break;
case "64":
ningxia++;
break;
case "65":
xinjiang++;
break;
case "71":
taiwan++;
break;
case "81":
xianggang++;
break;
case "82":
aomen++;
break;
default:
others++;
break;
}
}
String res="北京:" + beijingCount + "人\n"+"天津:"+tianjin+"人\n"+"河北:"+hebei+"人\n"+"山西:"+shanxi+"人\n"+"内蒙古:"+neimeng+"人\n"+"辽宁:"+liaoning+"人\n"+"吉林:"+jilin+"人\n"+"黑龙江:"+heilongjiang+"人\n"
+"上海:"+shanghai+"人\n"+"江苏:"+jiangsu+"人\n"+"浙江:"+zhejiang+"人\n"+"安徽:"+anhui+"人\n"+"福建:"+fujian+"人\n"+"江西:"+jiangxi+"人\n"+"山东:"+shandong+"人\n"
+"河南:"+henan+"人\n"+"湖北:"+hubei+"人\n"+"湖南:"+hunan+"人\n"+"广东:"+guangdong+"人\n"+"广西:"+guangxi+"人\n"+"海南:"+hainan+"人\n"
+"重庆:"+chongqing+"人\n"+"四川:"+sichuan+"人\n"+"贵州:"+guichou+"人\n"+"云南:"+yunnan+"人\n"+"西藏:"+xizang+"人\n"
+"陕西:"+shanxi2+"人\n"+"甘肃:"+gansu+"人\n"+"青海:"+qinghai+"人\n"+"宁夏:"+ningxia+"人\n"+"新疆:"+xinjiang+"人\n"+"台湾:"+taiwan+"人\n"+"河北:"+hebei+"人\n"
+"香港:"+xianggang+"人\n"+"澳门:"+aomen+"人\n"+"未知:"+others+"人\n";
System.out.println("会员地区分布:\n"+res);
//2、统计性别统计第17位奇男,偶女
double menCount=0,womenCount=0,sexNo=0;
java.sql.ResultSet allIdCards_Sex = sql.executeQuery(sqlString);
while(allIdCards_Sex.next()){
String idCard = new String();
String sexFlag = new String();
int sexflag = -1;
idCard = allIdCards_Sex.getString(1);
char sexChar = ' ';
int sex = -1;
if(idCard.length()>17){
sexFlag = idCard.substring(16, 17);
// System.out.println("sexFlag:"+sexFlag);
// sexflag = Integer.valueOf(sexFlag);
}else {
sexNo++;
continue;
}
boolean flag = (sexFlag.equals("x")||sexFlag.equals("X"));
// System.out.println("flag:"+flag);
if((sexFlag.charAt(0)>'a'&&sexFlag.charAt(0)<'z')||(sexFlag.charAt(0)>'A'&&sexFlag.charAt(0)<'Z')||sexFlag.equals("*")||sexFlag.equals("/")){
// sexFlag = idCard.substring(15, 16);
sex = 1;
}else{
sex = Integer.valueOf(sexFlag);
}
// System.out.println("sex:"+sex);
switch (sex%2) {
case 1:
menCount++;
break;
case 0:
womenCount++;
break;
default:
break;
}
}
System.out.println("男女人数:"+"男性:"+menCount+"人\n"+"女性:"+womenCount+"人\n");
//3、统计各个年龄段的人,<20;20~40;40~60;60~80
double lit20 = 0,lit40 = 0,lit60 = 0,lit80 = 0,noAgeCount = 0;
java.sql.ResultSet AllIdCards_Age = sql.executeQuery(sqlString);
while(AllIdCards_Age.next()){
int year1=0;
String ageBuff = new String();
String year = new String();
ageBuff = AllIdCards_Age.getString(1).toString();
// System.out.println("ccccccccccccccccccccccccccccc"+ageBuff);
if(ageBuff.length()>11&&!ageBuff.isEmpty()){
char buff[] = ageBuff.substring(6, 10).toCharArray();
int flag=0;
for(int i=0;i<4;i++){
if(buff[i]>='0'&&buff[i]<='9'){
}
else {
flag=1;
break;
}
}
if(flag==0){
year =ageBuff.substring(6, 10);
year1= Integer.valueOf(year);
}else{
noAgeCount++;
continue;
}
// System.out.println("不空:"+year1);
}else{
noAgeCount++;
// System.out.println("小于ageBuff:"+ageBuff);
// year1 = 2016;
continue;
}
int age=-1;
age = 2016 - year1;
if(age<20){
lit20++;
}else if(age<40){
lit40++;
}else if(age<60){
lit60++;
}else if(age<80){
lit80++;
}
}
String resAgeString = "0~20:"+lit20+"人\n"+"20~40:"+lit40+"人\n"+"40~60:"+lit60+"人\n"+"60~80:"+lit80+"人\n"+"身份证问题人数:"+noAgeCount+"人\n";
System.out.println("各年龄段人数:"+resAgeString);
}
}
java连接连接数据库、分析分析身份证号
最新推荐文章于 2023-02-17 11:56:02 发布