该程序实现的功能有,输入18位身份证号,然后程序输出性别,出生日期,籍贯,精确到县,判断身份证号码是否正确。数据库请读者自己配置
import java.util.Scanner;
/*各位:今天和明天做一下底下的作业(下周一03月11日交)
第十八位数字的计算方法为:
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。
例如:某男性的身份证号码是34052419800101001X。我们要看看这个身份证是不是合法的身份证。
首先:我们得出,前17位的乘积和是189
然后:用189除以11得出的结果是17 + 2/11,也就是说余数是2。
最后:通过对应规则就可以知道余数2对应的数字是x。所以,这是一个合格的身份证号码。 */
public class TestIdentityCard {
//根据身份证号判断生日
//根据身份证号判断籍贯,精确到县
//如果前17位身份证号码正确,第十八位校验码错误,则显示正确的身份证号码
//根据身份证号判断男女judgment gender
static void JudgmentGender(String str1){
char c=str1.charAt(16);
int num = c - '0';
if(num%2==0){
System.out.println("性别:女");
}
else{
System.out.println("性别:男");
}
}
//判断身份证号是否合法
static void Validation(String str1){
int sum = 0;
/*
* 该方法实现功能验证身份证号码是否合法。
*/
char c = str1.charAt(17);
String str = str1.substring(0, str1.length() - 1);
char[] ch = new char[17];
for (int i = 0; i < str.length(); i++) {// i=0,2,4,6,8
ch[i] = str.charAt(i);
}
sum = (ch[0] - '0') * 7 + (ch[1] - '0') * 9 + (ch[2] - '0') * 10
+ (ch[3] - '0') * 5 + (ch[4] - '0') * 8 + (ch[5] - '0') * 4
+ (ch[6] - '0') * 2 + (ch[7] - '0') * 1 + (ch[8] - '0') * 6
+ (ch[9] - '0') * 3 + (ch[10] - '0') * 7 + (ch[11] - '0') * 9
+ (ch[12] - '0') * 10 + (ch[13] - '0') * 5 + (ch[14] - '0') * 8
+ (ch[15] - '0') * 4 + (ch[16] - '0') * 2;
int y = sum % 11;
if ((y == 0 && c == '1') || (y == 1 && c == '0')
|| (y == 2 && (c == 'X'||c=='x')) || (y == 3 && c == '9')
|| (y == 4 && c == '8') || (y == 5 && c == '7')
|| (y == 6 && c == '6') || (y == 7 && c == '5')
|| (y == 8 && c == '4') || (y == 9 && c == '3')
|| (y == 10 && c == '2')) {
System.out.println("这是合法身份证");
} else {
System.out.println("这不是合法身份证");
}
}
public static void main(String[] args) {
System.out.println("请输入您的身份证号码");
Scanner s = new Scanner(System.in);
String str1 = s.nextLine();
JudgmentGender(str1);
System.out.println("出生日期:");
//Datebirth(str1);
System.out.println("籍贯:");
DB b=new DB();
b.executeQuery(str1);
Validation(str1);
}
}
连接数据库封装好的类
import java.sql.*;
import com.mysql.jdbc.Connection;
public class DB {
static {
try {
Class.forName("com.mysql.jdbc.Driver");
//System.out.println("数据库驱动成功");
}catch (Exception e) {
System.out.println("加载数据库驱动失败");
}
}
/*public static Connection getConn() {
java.sql.Connection conn = null;
try {
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/identitycard?user=sa&password=123456");
} catch (SQLException e) {
e.printStackTrace();
}
return (Connection) conn;
}*/
public static ResultSet executeQuery(String str1){
char ch1=str1.charAt(0);
char ch2=str1.charAt(1);
char ch3=str1.charAt(2);
char ch4=str1.charAt(3);
char ch5=str1.charAt(4);
char ch6=str1.charAt(5);
int sum=(ch1-'0')*100000+(ch2-'0')*10000+(ch3-'0')*1000+(ch4-'0')*100+(ch5-'0')*10+(ch6-'0');
ResultSet rs = null;
java.sql.Connection conn = null;
try {
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/identitycard?user=root&password=root");
//System.out.println("数据库连接成功");
} catch (SQLException e4) {
// TODO Auto-generated catch block
e4.printStackTrace();
System.out.println("数据库连接失败");
}
java.sql.Statement stmt = null;
try {
stmt = conn.createStatement();
} catch (SQLException e3) {
// TODO Auto-generated catch block
e3.printStackTrace();
}// 创建SQL命令对象
// 创建表
//System.out.println("查询");
//System.out.println("开始读取数据库");
try {
rs = stmt
.executeQuery("select * from card where id="+sum);
} catch (SQLException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
// 循环输出每一条记录
try {
while (rs.next())
{
System.out.println(rs.getString("addr"));
}
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
//System.out.println("读取完毕");
// 关闭连接
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}// 关闭命令对象连接
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}// 关闭数据库连接
return rs;
}
}