java中ATM与数据库Mysql的连接
importjava.sql.*;
importjava.util.*;
publicclassATM1{
Stringcode;
intpass;
doublemoney;
inti=1;
//检查登录
publicvoidcheckLogin(){
inti=1;
while(i<=3){
System.out.print("请输入您的卡号:");
Scannersc=newScanner(System.in);
Stringcode_=sc.nextLine();
System.out.print("请输入您的密码:");
intpass_=sc.nextInt();
try{
//加载驱动
Class.forName("com.mysql.jdbc.Driver");
//建立连接
java.sql.Connectionconn=DriverManager.getConnection("jdbc:mysql://localhost:3306/atmdb","root","root");
//创建sql传送对象
Statementstmt=conn.createStatement();
//将sql语句通过sql传送对象传送到数据库并执行,返还结果集
Stringsql="select*fromaccountwherecode='"+code_+"'andpass="+pass_;
ResultSetrs=stmt.executeQuery(sql);
//当账号、密码与sql中的账号与密码相对应的时候
//把sql中的相关数据传送到目前变量以便进行数据操作
if(rs.next()){
code=rs.getString(1);
pass=rs.getInt(2);
money=rs.getDouble(3);
loadSys();
}
rs.close();
stmt.close();
conn.close();
}
//捕获异常
catch(Exceptione){
System.out.println(e);
}
//出现三次错误之后提示
i++;
if(i==3){
System.out.print("您已经输错三次密码,该卡已经被锁,请及时到相关网点咨询!");
}
}
}
//保存数据
//注意;下面的关键字("pass、code、money")要与数据库中的名字一样,避免出现错误
publicvoidsaveDb(){
try{
Class.forName("com.mysql.jdbc.Driver");
java.sql.Connectionconn=DriverManager.getConnection("jdbc:mysql://localhost:3306/atmdb","root","root");
Statementstmt=conn.createStatement();
Stringsql="updateaccountsetpass="+pass+"wherecode='"+code+"'";
//dode为String型,所以要用‘’括起来
Stringsql1="updateaccountsetmoney="+money+"wherecode='"+code+"'";
stmt.executeUpdate(sql);//update操作进行数据更新
stmt.executeUpdate(sql1);
stmt.close();
conn.close();
}
catch(Exceptione){
System.out.println(e);
}
}
//欢迎界面
publicstaticvoidwelcome(){
System.out.println("!*****************************************!");
System.out.println("!*************欢迎使用华夏银行*************!");
System.out.println("!*****************************************!");
}
//系统主界面
publicvoidloadSys(){
System.out.println(".------------------------------------.");
System.out.println("1查询余额存款2");
System.out.println("3取款修改密码4");
System.out.println("5退出");
System.out.println(".------------------------------------.");
System.out.print("请输入相应的功能选项数字:");
Scannersz=newScanner(System.in);
intnum=sz.nextInt();
switch(num){
case1:
chaxun();
break;
case2:
cunkuan();
break;
case3:
qukuan();
break;
case4:
xiugai();
break;
case5:
quit();
break;
default:
System.out.println("您输入的数字有误!");
loadSys();
}
}
//查询余额
publicvoidchaxun(){
System.out.println("您卡上的余额为:"+money);
loadSys();
}
//存款
publicvoidcunkuan(){
System.out.print("请输入存款金额:");
Scannerck=newScanner(System.in);
doublemoney1=ck.nextDouble();
money=money+money1;
saveDb();
System.out.println("您卡上的余额为:"+money);
System.out.println("请收好您的卡!");
loadSys();
}
//取款
publicvoidqukuan(){
System.out.print("请输入取款金额:");
Scannerqk=newScanner(System.in);
doublemoney2=qk.nextDouble();
if(money2>money){
System.out.println("您的余额不足!");
qukuan();
}else{
money=money-money2;
saveDb();
System.out.println("您卡上的余额为:"+money);
System.out.println("请收好您的现金!");
loadSys();
}
}
//修改密码
publicvoidxiugai(){
intcs=0;
System.out.print("请输入原密码:");
Scannermm=newScanner(System.in);
intpass1=mm.nextInt();
System.out.print("请输入新密码:");
intpass2=mm.nextInt();
System.out.print("请再次输入新密码:");
intpass3=mm.nextInt();
if(pass==pass1&&pass2==pass3){
System.out.println("修改密码成功!");
pass=pass2;
saveDb();
loadSys();
}else{
if(cs<2){
System.out.print("您输入的密码有误!");
cs++;
xiugai();
}else{
quit();
}
}
}
//退出
publicvoidquit(){
System.exit(1);
}
//主函数
publicstaticvoidmain(Stringargs[]){
/*try{
Class.forName("com.mysql.jdbc.Driver");
java.sql.Connectionconn=DriverManager.getConnection("jdbc:mysql://localhost:3306/atmdb","root","root");
Statementstmt=conn.createStatement();
Stringsql="select*fromaccount";
ResultSetrs=stmt.executeQuery(sql);
while(rs.next()){
Stringcode=rs.getString(1);
intpass=rs.getInt(2);
doublemoney=rs.getDouble(3);
System.out.println(code+"|"+pass+"|"+money);
}
rs.close();
stmt.close();
conn.close();
}
catch(Exceptione){
System.out.println(e);
}*/
ATM1atm=newATM1();
atm.welcome();
atm.checkLogin();
}
}
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。