java串連MySQL。ATM

來源:互聯網
上載者:User

標籤:package   import   public   資料庫   china   

package bank;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.Statement;import java.util.Scanner;public class JDBC {static Statement sc=null;static Scanner sca=new Scanner(System.in);static String username;public static void main(String[] args) throws Exception {//該方法是 得到資料庫的操作平台的。getStatement();System.out.println("well come to bank of china");System.out.println("請選擇你需要的內容:"+"1.註冊"+"2.登陸");int m=sca.nextInt();for(int j=m;;){if(j==1){System.out.println("請輸入新的帳號");String bname=sca.next();System.out.println("請輸入新的密碼");String bpassword=sca.next();String sql="select * from bank where bname=‘"+bname+"‘";ResultSet bq=sc.executeQuery(sql);//查詢資料庫if(bq.next()){//判斷資料庫中是否也存在註冊的帳號System.out.println("該帳號已被註冊");}else{sql="insert into bank (bname,bpassword) values(‘"+bname+"‘,‘"+bpassword+"‘)";int i=sc.executeUpdate(sql);//更新資料庫,用i來接收返回的資料if(i!=0){System.out.println("註冊成功");}else{System.out.println("註冊失敗");}}}else if(j==2){for(int w=1;w<=3;w++){System.out.println("請登入:");System.out.println("使用者名稱:");username=sca.next();System.out.println("密碼:");String password=sca.next();//調用查詢賬戶方法,需要傳入   使用者名稱 和密碼  返回int類型的值int num=queryAccount(username,password);//num==1 表示 資料庫中有對應的使用者名稱和密碼if(num==1){System.out.println("登入成功");//使用for死迴圈  進行操作for(;;){System.out.println("請選擇交易類型:");System.out.println("1.存錢   2.取錢   3.查詢餘額   4.轉賬    5.退卡");int zx=sca.nextInt();//輸入操作類型if(zx==1){cun(); //調用存錢方法}else if(zx==2){qu();//調用取錢方法}else if(zx==3){query();//調用查詢餘額方法}else if(zx==4){zhuan();//調用轉賬方法}else if(zx==5){System.out.println("已退出。謝謝使用!請收好您的卡片!");System.exit(0);}else{System.out.println("輸入錯誤,已退出。謝謝使用!");break;}}}else{System.out.println("登入失敗!您還有"+(3-w)+"次機會");}} System.exit(0);}else{System.out.println("輸入錯誤,已退出。謝謝使用!請收好您的卡片!");break;}}}/** * 取錢方法 * @throws Exception */public static void qu() throws Exception{System.out.println("請輸入你的取款金額:");int bmoney=sca.nextInt();if(bmoney%100==0){String sql="update bank set bmoney=bmoney-"+bmoney;System.out.println(sql);boolean a =sc.execute(sql);if(!a){System.out.println("取款成功!");}}else{System.out.println("本機只提供面值為100元人民幣存取!");}}/** * 存錢方法 */public static void cun() throws Exception{System.out.println("請輸入你的存款金額:");double bmoney=sca.nextDouble();//輸入存款金額//拼接  修改sql  String sql="update bank set bmoney=bmoney+"+bmoney;//在 操作平台中  執行  sql語句boolean a=sc.execute(sql);//判斷是否成功if(bmoney%100==0){if(!a){System.out.println("存款成功!");}}else{System.out.println("本機只提供面值為100元人民幣存取!");}}public static int queryAccount(String bname,String bpassword) throws Exception{//拼接查詢sql   注意:  在拼接的時候,,字串需要在前後加‘(單引號)String sql="select * from bank where bname=‘"+bname+"‘ and bpassword=‘"+bpassword+"‘";//在平台中執行查詢sql ,並把查詢的內容放在  ResultSet rs 裡面ResultSet rs=sc.executeQuery(sql);//聲明一個int 類型的變數  初始值 0int num=0;//如果查詢的結果裡面有值得話,就進入迴圈裡面while(rs.next()){  //rs.next()  是判斷當前位置是否有資料,有就進入 沒有就跳過num++;}return num;}/* * 查詢方法 */public static void query() throws Exception{//拼接查詢sql   注意:  在拼接的時候,,字串需要在前後加‘(單引號)String sql="select bmoney from bank where bname=‘"+username+"‘";//在平台中執行查詢sql ,並把查詢的內容放在  ResultSet rs 裡面ResultSet rs=sc.executeQuery(sql);//聲明一個double類型的變數  賦值 0double bmoney=0;//rs.next()  是判斷當前位置是否有資料,有就進入 沒有就跳過while(rs.next()){//把查詢出來的資料賦值給money 變數bmoney=rs.getDouble(1);}System.out.println("你的賬戶餘額:"+bmoney);}public static void zhuan() throws Exception{System.out.println("請輸入您要轉入的賬戶:");String zname=sca.next();System.out.println("請確認您要轉入的賬戶:");String zrname=sca.next();if(zname.equals(zrname)){String sql="select * from bank where bname=‘"+zname+"‘";ResultSet bq=sc.executeQuery(sql);//查詢資料庫if(bq.next()){System.out.println("該賬戶存在,請輸入轉入金額:");int zrmoney=sca.nextInt();//輸入轉入金額//拼接  修改sql      String  sql1="update bank set bmoney=bmoney+"+zrmoney+" WHERE bname=‘"+zname+"‘;";    int m=sc.executeUpdate(sql1);  String sql2="update bank set bmoney=bmoney-"+zrmoney+" WHERE bname=‘"+username+"‘;";int n=sc.executeUpdate(sql2);//更新資料庫,用i來接收返回的資料System.out.println("成功");}else{System.out.println("賬戶不存在");}}else{System.out.println("倆次輸入不一致");}}/** * 得到資料庫操作平台方法 * @throws Exception */public static void getStatement() throws Exception{//1\載入驅動Class.forName("com.mysql.jdbc.Driver");/** * 資料庫連接URL * jdbc:mysql://IP:port/資料庫名 * jdbc:mysql://localhost:3306/score */String url="jdbc:mysql://localhost:3306/atm";//資料庫使用者名稱String bname="root";//資料庫密碼String bword="556687a";//使用驅動得到資料庫連接,需要傳入  url username passwordConnection c=DriverManager.getConnection(url, bname, bword);//得到資料庫操作平台,平台sc=c.createStatement();}}


本文出自 “12160034” 部落格,請務必保留此出處http://12170034.blog.51cto.com/12160034/1865207

java串連MySQL。ATM

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.