日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

java中ATM与数据库Mysql的连接

發布時間:2023/12/31 数据库 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java中ATM与数据库Mysql的连接 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
  • 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();??

  • ????????}?????

  • } ?


  • 轉載于:https://blog.51cto.com/12166507/1865135

    創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

    總結

    以上是生活随笔為你收集整理的java中ATM与数据库Mysql的连接的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。