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 { //該方法是 得到數(shù)據(jù)庫的操作平臺(tái)的。 getStatement(); System.out.println("well come to bank of china"); System.out.println("請(qǐng)選擇你需要的內(nèi)容:"+"1.注冊(cè)"+"2.登陸"); int m=sca.nextInt(); for(int j=m;;){ if(j==1){ System.out.println("請(qǐng)輸入新的賬號(hào)"); String bname=sca.next(); System.out.println("請(qǐng)輸入新的密碼"); String bpassword=sca.next(); String sql="select * from bank where bname='"+bname+"'"; ResultSet bq=sc.executeQuery(sql);//查詢數(shù)據(jù)庫 if(bq.next()){//判斷數(shù)據(jù)庫中是否也存在注冊(cè)的帳號(hào) System.out.println("該帳號(hào)已被注冊(cè)"); }else{ sql="insert into bank (bname,bpassword) values('"+bname+"','"+bpassword+"')"; int i=sc.executeUpdate(sql);//更新數(shù)據(jù)庫,用i來接收返回的數(shù)據(jù) if(i!=0){ System.out.println("注冊(cè)成功"); }else{ System.out.println("注冊(cè)失敗"); } } } else if(j==2){ for(int w=1;w<=3;w++){ System.out.println("請(qǐng)登錄:"); System.out.println("用戶名:"); username=sca.next(); System.out.println("密碼:"); String password=sca.next(); //調(diào)用查詢賬戶方法,需要傳入 用戶名 和密碼 返回int類型的值 int num=queryAccount(username,password); //num==1 表示 數(shù)據(jù)庫中有對(duì)應(yīng)的用戶名和密碼 if(num==1){ System.out.println("登錄成功"); //使用for死循環(huán) 進(jìn)行操作 for(;;){ System.out.println("請(qǐng)選擇交易類型:"); System.out.println("1.存錢 2.取錢 3.查詢余額 4.轉(zhuǎn)賬 5.退卡"); int zx=sca.nextInt();//輸入操作類型 if(zx==1){ cun(); //調(diào)用存錢方法 }else if(zx==2){ qu();//調(diào)用取錢方法 }else if(zx==3){ query();//調(diào)用查詢余額方法 }else if(zx==4){ zhuan();//調(diào)用轉(zhuǎn)賬方法 }else if(zx==5){ System.out.println("已退出。謝謝使用!請(qǐng)收好您的卡片!"); System.exit(0); }else{ System.out.println("輸入錯(cuò)誤,已退出。謝謝使用!"); break; } } }else{ System.out.println("登錄失??!您還有"+(3-w)+"次機(jī)會(huì)"); } } System.exit(0); } else{ System.out.println("輸入錯(cuò)誤,已退出。謝謝使用!請(qǐng)收好您的卡片!"); break; } } } /** * 取錢方法 * @throws Exception */ public static void qu() throws Exception{ System.out.println("請(qǐng)輸入你的取款金額:"); 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("本機(jī)只提供面值為100元人民幣存取!"); } } /** * 存錢方法 */ public static void cun() throws Exception{ System.out.println("請(qǐng)輸入你的存款金額:"); double bmoney=sca.nextDouble();//輸入存款金額 //拼接 修改sql String sql="update bank set bmoney=bmoney+"+bmoney; //在 操作平臺(tái)中 執(zhí)行 sql語句 boolean a=sc.execute(sql); //判斷是否成功 if(bmoney%100==0){ if(!a){ System.out.println("存款成功!"); } }else{ System.out.println("本機(jī)只提供面值為100元人民幣存??!"); } } public static int queryAccount(String bname,String bpassword) throws Exception{ //拼接查詢sql 注意: 在拼接的時(shí)候,,字符串需要在前后加'(單引號(hào)) String sql="select * from bank where bname='"+bname+"' and bpassword='"+bpassword+"'"; //在平臺(tái)中執(zhí)行查詢sql ,并把查詢的內(nèi)容放在 ResultSet rs 里面 ResultSet rs=sc.executeQuery(sql); //聲明一個(gè)int 類型的變量 初始值 0 int num=0; //如果查詢的結(jié)果里面有值得話,就進(jìn)入循環(huán)里面 while(rs.next()){ //rs.next() 是判斷當(dāng)前位置是否有數(shù)據(jù),有就進(jìn)入 沒有就跳過 num++; } return num; } /* * 查詢方法 */ public static void query() throws Exception{ //拼接查詢sql 注意: 在拼接的時(shí)候,,字符串需要在前后加'(單引號(hào)) String sql="select bmoney from bank where bname='"+username+"'"; //在平臺(tái)中執(zhí)行查詢sql ,并把查詢的內(nèi)容放在 ResultSet rs 里面 ResultSet rs=sc.executeQuery(sql); //聲明一個(gè)double類型的變量 賦值 0 double bmoney=0; //rs.next() 是判斷當(dāng)前位置是否有數(shù)據(jù),有就進(jìn)入 沒有就跳過 while(rs.next()){ //把查詢出來的數(shù)據(jù)賦值給money 變量 bmoney=rs.getDouble(1); } System.out.println("你的賬戶余額:"+bmoney); } public static void zhuan() throws Exception{ System.out.println("請(qǐng)輸入您要轉(zhuǎn)入的賬戶:"); String zname=sca.next(); System.out.println("請(qǐng)確認(rèn)您要轉(zhuǎn)入的賬戶:"); String zrname=sca.next(); if(zname.equals(zrname)){ String sql="select * from bank where bname='"+zname+"'"; ResultSet bq=sc.executeQuery(sql);//查詢數(shù)據(jù)庫 if(bq.next()){ System.out.println("該賬戶存在,請(qǐng)輸入轉(zhuǎn)入金額:"); int zrmoney=sca.nextInt();//輸入轉(zhuǎn)入金額 //拼接 修改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);//更新數(shù)據(jù)庫,用i來接收返回的數(shù)據(jù) System.out.println("成功"); }else{ System.out.println("賬戶不存在"); } }else{ System.out.println("倆次輸入不一致"); } } /** * 得到數(shù)據(jù)庫操作平臺(tái)方法 * @throws Exception */ public static void getStatement() throws Exception{ //1\加載驅(qū)動(dòng) Class.forName("com.MySQL.jdbc.Driver"); /** * 數(shù)據(jù)庫連接URL * jdbc:mysql://IP:port/數(shù)據(jù)庫名 * jdbc:mysql://localhost:3306/score */ String url="jdbc:mysql://localhost:3306/atm"; //數(shù)據(jù)庫用戶名 String bname="root"; //數(shù)據(jù)庫密碼 String bword="556687a"; //使用驅(qū)動(dòng)得到數(shù)據(jù)庫連接,需要傳入 url username password Connection c=DriverManager.getConnection(url, bname, bword); //得到數(shù)據(jù)庫操作平臺(tái),平臺(tái) sc=c.createStatement(); } }
分享標(biāo)題:java連接MySQL。ATM
文章網(wǎng)址:http://jinyejixie.com/article34/ijopse.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供建站公司、網(wǎng)站排名、網(wǎng)站營(yíng)銷、企業(yè)建站、面包屑導(dǎo)航、App開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)