溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

java中ATM與數據庫Mysql的連接

發布時間:2020-08-10 08:32:01 來源:網絡 閱讀:1607 作者:陳年往事 欄目:數據庫
  1. import java.sql.*;  

  2. import java.util.*;  

  3. public class ATM1 {  

  4.     String code;  

  5.     int pass;  

  6.     double money;  

  7.     int i=1;  

  8.     //檢查登錄  

  9.     public void checkLogin(){  

  10.         int i=1;  

  11.         while(i<=3){  

  12.             System.out.print("請輸入您的卡號:");  

  13.             Scanner sc=new Scanner(System.in);  

  14.             String code_=sc.nextLine();  

  15.             System.out.print("請輸入您的密碼:");  

  16.             int pass_=sc.nextInt();  

  17.             try{  

  18.                 //加載驅動  

  19.                 Class.forName("com.mysql.jdbc.Driver");  

  20.                 //建立連接  

  21.                 java.sql.Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/atmdb","root","root");     

  22.                 //創建sql傳送對象  

  23.                 Statement stmt = conn.createStatement();  

  24.                 //將sql語句通過sql傳送對象傳送到數據庫并執行,返還結果集  

  25.                 String sql = "select * from account where code='"+code_+"'and pass="+pass_;  

  26.                 ResultSet rs = stmt.executeQuery(sql);    

  27.                 //當賬號、密碼與sql中的賬號與密碼相對應的時候  

  28.                 //把sql中的相關數據傳送到目前變量以便進行數據操作  

  29.                 if(rs.next()){  

  30.                     code = rs.getString(1);  

  31.                     pass = rs.getInt(2);  

  32.                     money = rs.getDouble(3);  

  33.                     loadSys();  

  34.                 }  

  35.                 rs.close();  

  36.                 stmt.close();  

  37.                 conn.close();  

  38.             }  

  39.             //捕獲異常  

  40.             catch (Exception e){  

  41.                 System.out.println(e);  

  42.             }  

  43.             //出現三次錯誤之后提示  

  44.             i++;  

  45.             if( i == 3){  

  46.             System.out.print("您已經輸錯三次密碼,該卡已經被鎖,請及時到相關網點咨詢!");  

  47.             }  

  48.         }  

  49.     }  

  50.     //保存數據  

  51.     //注意;下面的關鍵字(    "pass、code、money")要與數據庫中的名字一樣,避免出現錯誤  

  52.     public void saveDb(){  

  53.         try{  

  54.             Class.forName("com.mysql.jdbc.Driver");  

  55.             java.sql.Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/atmdb","root","root");     

  56.             Statement stmt = conn.createStatement();  

  57.             String sql="update account set pass ="+pass+" where code='"+code+"'";    

  58.             //dode為String型,所以要用‘’括起來  

  59.             String sql1="update account set money="+money+" where code='"+code+"'";  

  60.             stmt.executeUpdate(sql);    //update操作進行數據更新  

  61.             stmt.executeUpdate(sql1);  

  62.             stmt.close();  

  63.             conn.close();  

  64.         }     

  65.         catch (Exception e){  

  66.                 System.out.println(e);  

  67.             }  

  68.           

  69.     }  

  70.     //歡迎界面  

  71.     public static void welcome(){  

  72.         System.out.println("!*****************************************!");  

  73.         System.out.println("!*************歡迎使用華夏銀行*************!");  

  74.         System.out.println("!*****************************************!");  

  75.     }  

  76.     //系統主界面  

  77.     public void loadSys(){  

  78.         System.out.println(".------------------------------------.");  

  79.         System.out.println("1 查詢余額                      存款 2");  

  80.         System.out.println("3 取款                      修改密碼 4");  

  81.         System.out.println("5 退出                                ");  

  82.         System.out.println(".------------------------------------.");  

  83.         System.out.print("請輸入相應的功能選項數字:");  

  84.         Scanner sz=new Scanner(System.in);  

  85.         int num=sz.nextInt();         

  86.         switch(num){  

  87.             case 1:     

  88.                 chaxun();  

  89.                 break;  

  90.             case 2:     

  91.                cunkuan();  

  92.                break;  

  93.             case 3:     

  94.                qukuan();  

  95.                break;  

  96.             case 4:     

  97.                xiugai();  

  98.                break;  

  99.             case 5:     

  100.                quit();  

  101.                break;  

  102.             default:  

  103.                System.out.println("您輸入的數字有誤!");  

  104.                loadSys();  

  105.         }  

  106.     }  

  107.     //查詢余額  

  108.     public void chaxun(){  

  109.         System.out.println("您卡上的余額為:" +money);  

  110.         loadSys();  

  111.     }  

  112.     //存款  

  113.     public void cunkuan(){  

  114.         System.out.print("請輸入存款金額:");  

  115.         Scanner ck=new Scanner(System.in);  

  116.         double money1=ck.nextDouble();  

  117.         money=money+money1;  

  118.         saveDb();  

  119.         System.out.println("您卡上的余額為:" +money);  

  120.         System.out.println("請收好您的卡!");  

  121.         loadSys();  

  122.     }  

  123.     //取款  

  124.     public void qukuan(){  

  125.         System.out.print("請輸入取款金額:");  

  126.         Scanner qk=new Scanner(System.in);  

  127.         double money2=qk.nextDouble();  

  128.         if(money2>money){  

  129.             System.out.println("您的余額不足!");    

  130.             qukuan();  

  131.         }else{  

  132.             money=money-money2;  

  133.             saveDb();  

  134.             System.out.println("您卡上的余額為:" +money);  

  135.             System.out.println("請收好您的現金!");   

  136.             loadSys();  

  137.         }  

  138.     }  

  139.     //修改密碼  

  140.     public void xiugai(){  

  141.         int cs = 0;  

  142.         System.out.print("請輸入原密碼:");   

  143.         Scanner mm=new Scanner(System.in);  

  144.         int pass1=mm.nextInt();  

  145.         System.out.print("請輸入新密碼:");   

  146.         int pass2=mm.nextInt();  

  147.         System.out.print("請再次輸入新密碼:");   

  148.         int pass3=mm.nextInt();  

  149.         if(pass==pass1&& pass2==pass3){  

  150.             System.out.println("修改密碼成功!");   

  151.             pass=pass2;  

  152.             saveDb();  

  153.             loadSys();  

  154.         }else{  

  155.             if(cs<2){  

  156.                System.out.print("您輸入的密碼有誤!");   

  157.                cs++;  

  158.                xiugai();  

  159.             }else{  

  160.                quit();  

  161.             }  

  162.         }  

  163.     }  

  164.     //退出  

  165.     public void quit(){  

  166.         System.exit(1);       

  167.     }  

  168.     //主函數  

  169.     public static void main(String args[]){  

  170.         /*try{ 

  171.             Class.forName("com.mysql.jdbc.Driver"); 

  172.             java.sql.Connection conn = DriverManager.getConnection( "jdbc:mysql://localhost:3306/atmdb","root","root"); 

  173.             Statement stmt = conn.createStatement(); 

  174.             String sql = "select * from account"; 

  175.             ResultSet rs = stmt.executeQuery(sql); 

  176.             while (rs.next()){ 

  177.                 String code = rs.getString(1); 

  178.                 int pass = rs.getInt(2); 

  179.                 double money = rs.getDouble(3); 

  180.                 System.out.println(code+"|"+pass+"|"+money); 

  181.             } 

  182.             rs.close(); 

  183.             stmt.close(); 

  184.             conn.close(); 

  185.         } 

  186.         catch(Exception e){ 

  187.         System.out.println(e); 

  188.         }*/  

  189.         ATM1 atm = new ATM1();  

  190.         atm.welcome();  

  191.         atm.checkLogin();  

  192.         }     

  193. }  


向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女