java中ATM与数据库Mysql的连接

[b]昨晚太晚了,所以没写博客就睡觉了,今天师兄又开始讲新的内容(jsp)所以趁刚刚弄懂了一点点jsp,现在写写昨晚的的ATM[/b] :D 里面还有很多自己不是很理解的地方,现在只是很浅的知道操作,还不是清除内存是怎么运行的!先记录起来吧!

import java.sql.*;
import java.util.*;
public class ATM1 {
String code;
int pass;
double money;
int i=1;
//检查登录
public void checkLogin(){
int i=1;
while(i<=3){
System.out.print("请输入您的卡号:");
Scanner sc=new Scanner(System.in);
String code_=sc.nextLine();
System.out.print("请输入您的密码:");
int pass_=sc.nextInt();
try{
//加载驱动
Class.forName("com.mysql.jdbc.Driver");
//建立连接
java.sql.Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/atmdb","root","root");
//创建sql传送对象
Statement stmt = conn.createStatement();
//将sql语句通过sql传送对象传送到数据库并执行,返还结果集
String sql = "select * from account where code='"+code_+"'and pass="+pass_;
ResultSet rs = 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 (Exception e){
System.out.println(e);
}
//出现三次错误之后提示
i++;
if( i == 3){
System.out.print("您已经输错三次密码,该卡已经被锁,请及时到相关网点咨询!");
}
}
}
//保存数据
//注意;下面的关键字( "pass、code、money")要与数据库中的名字一样,避免出现错误
public void saveDb(){
try{
Class.forName("com.mysql.jdbc.Driver");
java.sql.Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/atmdb","root","root");
Statement stmt = conn.createStatement();
String sql="update account set pass ="+pass+" where code='"+code+"'";
//dode为String型,所以要用‘’括起来
String sql1="update account set money="+money+" where code='"+code+"'";
stmt.executeUpdate(sql); //update操作进行数据更新
stmt.executeUpdate(sql1);
stmt.close();
conn.close();
}
catch (Exception e){
System.out.println(e);
}

}
//欢迎界面
public static void welcome(){
System.out.println("!*****************************************!");
System.out.println("!*************欢迎使用华夏银行*************!");
System.out.println("!*****************************************!");
}
//系统主界面
public void loadSys(){
System.out.println(".------------------------------------.");
System.out.println("1 查询余额 存款 2");
System.out.println("3 取款 修改密码 4");
System.out.println("5 退出 ");
System.out.println(".------------------------------------.");
System.out.print("请输入相应的功能选项数字:");
Scanner sz=new Scanner(System.in);
int num=sz.nextInt();
switch(num){
case 1:
chaxun();
break;
case 2:
cunkuan();
break;
case 3:
qukuan();
break;
case 4:
xiugai();
break;
case 5:
quit();
break;
default:
System.out.println("您输入的数字有误!");
loadSys();
}
}
//查询余额
public void chaxun(){
System.out.println("您卡上的余额为:" +money);
loadSys();
}
//存款
public void cunkuan(){
System.out.print("请输入存款金额:");
Scanner ck=new Scanner(System.in);
double money1=ck.nextDouble();
money=money+money1;
saveDb();
System.out.println("您卡上的余额为:" +money);
System.out.println("请收好您的卡!");
loadSys();
}
//取款
public void qukuan(){
System.out.print("请输入取款金额:");
Scanner qk=new Scanner(System.in);
double money2=qk.nextDouble();
if(money2>money){
System.out.println("您的余额不足!");
qukuan();
}else{
money=money-money2;
saveDb();
System.out.println("您卡上的余额为:" +money);
System.out.println("请收好您的现金!");
loadSys();
}
}
//修改密码
public void xiugai(){
int cs = 0;
System.out.print("请输入原密码:");
Scanner mm=new Scanner(System.in);
int pass1=mm.nextInt();
System.out.print("请输入新密码:");
int pass2=mm.nextInt();
System.out.print("请再次输入新密码:");
int pass3=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();
}
}
}
//退出
public void quit(){
System.exit(1);
}
//主函数
public static void main(String args[]){
/*try{
Class.forName("com.mysql.jdbc.Driver");
java.sql.Connection conn = DriverManager.getConnection( "jdbc:mysql://localhost:3306/atmdb","root","root");
Statement stmt = conn.createStatement();
String sql = "select * from account";
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()){
String code = rs.getString(1);
int pass = rs.getInt(2);
double money = rs.getDouble(3);
System.out.println(code+"|"+pass+"|"+money);
}
rs.close();
stmt.close();
conn.close();
}
catch(Exception e){
System.out.println(e);
}*/
ATM1 atm = new ATM1();
atm.welcome();
atm.checkLogin();
}
}


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值