溫馨提示×

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

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

java連接MySQL。ATM

發(fā)布時(shí)間:2020-07-07 05:23:06 來(lái)源:網(wǎng)絡(luò) 閱讀:1095 作者:叼草小毛驢 欄目:數(shù)據(jù)庫(kù)
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ù)庫(kù)的操作平臺(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ù)庫(kù)
				if(bq.next()){//判斷數(shù)據(jù)庫(kù)中是否也存在注冊(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ù)庫(kù),用i來(lá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ù)庫(kù)中有對(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語(yǔ)句
		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)入 沒(méi)有就跳過(guò)
			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)入 沒(méi)有就跳過(guò)
		while(rs.next()){
			//把查詢出來(lái)的數(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ù)庫(kù)
		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ù)庫(kù),用i來(lái)接收返回的數(shù)據(jù)
		System.out.println("成功");
			}else{
				System.out.println("賬戶不存在");
			}
			}else{
				System.out.println("倆次輸入不一致");
			}
		}
	/**
	 * 得到數(shù)據(jù)庫(kù)操作平臺(tái)方法
	 * @throws Exception
	 */
	public static void getStatement() throws Exception{
		//1\加載驅(qū)動(dòng)
		Class.forName("com.mysql.jdbc.Driver");
		/**
		 * 數(shù)據(jù)庫(kù)連接URL
		 * jdbc:mysql://IP:port/數(shù)據(jù)庫(kù)名
		 * jdbc:mysql://localhost:3306/score
		 */
		String url="jdbc:mysql://localhost:3306/atm";
		//數(shù)據(jù)庫(kù)用戶名
		String bname="root";
		//數(shù)據(jù)庫(kù)密碼
		String bword="556687a";
		//使用驅(qū)動(dòng)得到數(shù)據(jù)庫(kù)連接,需要傳入  url username password
		Connection c=DriverManager.getConnection(url, bname, bword);
		//得到數(shù)據(jù)庫(kù)操作平臺(tái),平臺(tái)
		sc=c.createStatement();				
	}
}


向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI