Java串連Mysql 執行個體

來源:互聯網
上載者:User

標籤:row   資料庫名   bsp   成功   nbsp   div   賦值   位置   manage   

  1 package com.jdbc;  2   3 import java.sql.Connection;  4 import java.sql.DriverManager;  5 import java.sql.ResultSet;  6 import java.sql.Statement;  7 import java.util.Scanner;  8   9 public class Atm{ 10     static Statement sc=null; 11     static Scanner sca=new Scanner(System.in); 12     static String username; 13     public static void main(String[] args) throws Exception { 14         //該方法是 得到資料庫的操作平台的。 15         getStatement();
16 System.out.println("歡迎來到J18銀行"); 17 System.out.println("請登入:"); 18 System.out.println("使用者名稱:"); 19 username=sca.next(); 20 System.out.println("密碼:"); 21 String password=sca.next(); 22 //調用查詢賬戶方法,需要傳入 使用者名稱 和密碼 返回int類型的值 23 int num=queryJ18(username,password); 24 //num==1 表示 資料庫中有對應的使用者名稱和密碼 25
if(num==1){ 26 System.out.println("登入成功"); 27 //使用for死迴圈 進行操作 28 for(;;){ 29 System.out.println("請選擇交易類型:"); 30 System.out.println("1、存錢 2、取錢 3、查詢餘額"); 31 int cz=sca.nextInt();//輸入操作類型 32 if(cz==1){ 33 cun(); //調用存錢方法 34 }else if(cz==2){ 35 qu();//調用取錢方法 36 }else if(cz==3){ 37 query();//調用查詢餘額方法 38 }else{ 39 System.out.println("謝謝使用!"); 40 break; 41 } 42 } 43 } 44
else{ 45 System.out.println("登入失敗!"); 46 } 47 } 48 /** 49 * 取錢方法 50 * @throws Exception 51 */ 52
public static void qu() throws Exception{ 53 System.out.println("請輸入你的取款金額:"); 54 double money=sca.nextDouble(); 55 String sql="update J18 set money=money-"+money; 56 System.out.println(sql); 57 boolean a =sc.execute(sql); 58
if(!a){ 59 System.out.println("取款成功!"); 60 } 61 } 62 /** 63 * 存錢方法 64 */ 65
public static void cun() throws Exception{ 66 67 System.out.println("請輸入你的存款金額:"); 68 double money=sca.nextDouble();//輸入存款金額 69 //拼接 修改sql 70 String sql="update J18 set money=money+"+money; 71 //在 操作平台中 執行 sql語句 72 boolean a =sc.execute(sql); 73 //判斷是否成功 74 if(!a){ 75 System.out.println("存款成功!"); 76 } 77 } 78 public static int queryJ18(String username,String password) throws Exception{ 79 //拼接查詢sql 注意: 在拼接的時候,,字串需要在前後加‘(單引號) 80 String sql="select * from J18 where Sname=‘"+username+"‘ and pwd=‘"+password+"‘"; 81 //在平台中執行查詢sql ,並把查詢的內容放在 ResultSet rs 裡面 82 ResultSet rs=sc.executeQuery(sql); 83 //聲明一個int 類型的變數 初始值 0 84 int num=0; 85 //如果查詢的結果裡面有值得話,就進入迴圈裡面 86 while(rs.next()){ //rs.next() 是判斷當前位置是否有資料,有就進入 沒有就跳過 87 num++; 88 } 89 return num; 90 } 91 /* 92 * 查詢方法 93 */ 94
public static void query() throws Exception{ 95 //拼接查詢sql 注意: 在拼接的時候,,字串需要在前後加‘(單引號) 96 String sql="select money from J18 where Sname=‘"+username+"‘"; 97 //在平台中執行查詢sql ,並把查詢的內容放在 ResultSet rs 裡面 98 ResultSet rs=sc.executeQuery(sql); 99 //聲明一個double類型的變數 賦值 0100 double money=0;101 //rs.next() 是判斷當前位置是否有資料,有就進入 沒有就跳過102 while(rs.next()){103 //把查詢出來的資料賦值給money 變數104 money=rs.getDouble(1);105 }106 System.out.println("你的賬戶餘額:"+money);107 }108 109 /**110 * 得到資料庫操作平台方法111 * @throws Exception112 */113 public static void getStatement() throws Exception{114 //1\載入驅動115 Class.forName("com.mysql.jdbc.Driver");116 /**117 * 資料庫連接URL118 * jdbc:mysql://IP:port/資料庫名119 * jdbc:mysql://localhost:3306/score120 */121 String url="jdbc:mysql://localhost:3306/atm";122 //資料庫使用者名稱123 String username="root";124 //資料庫密碼125 String password="root";126 //使用驅動得到資料庫連接,需要傳入 url username password127 Connection c=DriverManager.getConnection(url, username, password);128 //得到資料庫操作平台,平台129 sc=c.createStatement(); 130 }131 }

 

Java串連Mysql 執行個體

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.