新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
java連接MySQL。ATM
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ù)庫的操作平臺的。 getStatement(); System.out.println("well come to bank of china"); System.out.println("請選擇你需要的內(nèi)容:"+"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);//查詢數(shù)據(jù)庫 if(bq.next()){//判斷數(shù)據(jù)庫中是否也存在注冊的帳號 System.out.println("該帳號已被注冊"); }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("注冊成功"); }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(); //調(diào)用查詢賬戶方法,需要傳入 用戶名 和密碼 返回int類型的值 int num=queryAccount(username,password); //num==1 表示 數(shù)據(jù)庫中有對應(yīng)的用戶名和密碼 if(num==1){ System.out.println("登錄成功"); //使用for死循環(huán) 進(jìn)行操作 for(;;){ System.out.println("請選擇交易類型:"); 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("已退出。謝謝使用!請收好您的卡片!"); System.exit(0); }else{ System.out.println("輸入錯誤,已退出。謝謝使用!"); break; } } }else{ System.out.println("登錄失??!您還有"+(3-w)+"次機(jī)會"); } } 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("本機(jī)只提供面值為100元人民幣存??!"); } } /** * 存錢方法 */ public static void cun() throws Exception{ System.out.println("請輸入你的存款金額:"); double bmoney=sca.nextDouble();//輸入存款金額 //拼接 修改sql String sql="update bank set bmoney=bmoney+"+bmoney; //在 操作平臺中 執(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í)候,,字符串需要在前后加'(單引號) String sql="select * from bank where bname='"+bname+"' and bpassword='"+bpassword+"'"; //在平臺中執(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í)候,,字符串需要在前后加'(單引號) String sql="select bmoney from bank where bname='"+username+"'"; //在平臺中執(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("請輸入您要轉(zhuǎn)入的賬戶:"); String zname=sca.next(); System.out.println("請確認(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("該賬戶存在,請輸入轉(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ù)庫操作平臺方法 * @throws Exception */ public static void getStatement() throws Exception{ //1\加載驅(qū)動 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ū)動得到數(shù)據(jù)庫連接,需要傳入 url username password Connection c=DriverManager.getConnection(url, bname, bword); //得到數(shù)據(jù)庫操作平臺,平臺 sc=c.createStatement(); } }
網(wǎng)站名稱:java連接MySQL。ATM
文章鏈接:http://www.dlmjj.cn/article/ijopse.html