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)  進行操作
						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)+"次機會");
					}
				}
				 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;
		//在 操作平臺中  執(zhí)行  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+"'";
		//在平臺中執(zhí)行查詢sql ,并把查詢的內(nèi)容放在  ResultSet rs 里面
		ResultSet rs=sc.executeQuery(sql);
		//聲明一個int 類型的變量  初始值 0
		int num=0;
		//如果查詢的結(jié)果里面有值得話,就進入循環(huán)里面
		while(rs.next()){  //rs.next()  是判斷當前位置是否有數(shù)據(jù),有就進入 沒有就跳過
			num++;
		}
		return num;
	}
	/*
	 * 查詢方法
	 */
	public static void query() throws Exception{
		//拼接查詢sql   注意:  在拼接的時候,,字符串需要在前后加'(單引號)
		String sql="select bmoney from bank where bname='"+username+"'";
		//在平臺中執(zhí)行查詢sql ,并把查詢的內(nèi)容放在  ResultSet rs 里面
		ResultSet rs=sc.executeQuery(sql);
		//聲明一個double類型的變量  賦值 0
		double bmoney=0;
		//rs.next()  是判斷當前位置是否有數(shù)據(jù),有就進入 沒有就跳過
		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("請確認您要轉(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();				
	}
}

分享標題:java連接MySQL。ATM
文章網(wǎng)址:http://muchs.cn/article34/ijopse.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供建站公司、網(wǎng)站排名網(wǎng)站營銷、企業(yè)建站、面包屑導(dǎo)航、App開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)