溫馨提示×

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

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

Mysql最后補(bǔ)充+Java連接數(shù)據(jù)庫(kù)

發(fā)布時(shí)間:2020-06-29 19:03:07 來(lái)源:網(wǎng)絡(luò) 閱讀:310 作者:Undertaker16 欄目:數(shù)據(jù)庫(kù)

Truncate table 表名

直接刪除表中全部數(shù)據(jù),與delete不同的是,此方法無(wú)法使用where選擇,只能全部刪除。

 

truncate table users;

 

Java連接數(shù)據(jù)庫(kù):

package com.edu.gkh;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Scanner;

public class Homework {
 static Statement sc=null;
 static Scanner sca=new Scanner(System.in);
 static String username;
 public static void main(String[] args) throws Exception {
  getStatement();
  String name2="";
  String pwd2="";
  for(;;){
  System.out.println("請(qǐng)輸入注冊(cè)賬號(hào)");
  String name=sca.next();
  System.out.println("請(qǐng)輸入密碼");
  String pwd=sca.next();
  System.out.println("請(qǐng)?jiān)俅屋斎朊艽a");
  String pwd1=sca.next();
   if(pwd1.equals(pwd)){
    name2=name;
    pwd2=pwd;
    System.out.println("注冊(cè)成功");
    break;
   }
   else{
    System.out.println("兩次密碼不一致,請(qǐng)重新輸入;是否重新注冊(cè)yes/no");    
   char regist=sca.next().charAt(0);
   if(regist=='y'){
    System.out.println("請(qǐng)重新輸入");
   }
   else if(regist=='n'){
    System.out.println("您已經(jīng)退出注冊(cè)");
   }
   else{
    System.out.println("您的輸入非法");
   }
  }
 }
  
  System.out.println("歡迎來(lái)到J18銀行");
  System.out.println("請(qǐng)登錄:");
  System.out.println("用戶名:");
  username=sca.next();
  System.out.println("密碼:");
  String password=sca.next();
  int num=queryAccount(username,password);
  if(num==1){
   System.out.println("登陸成功");
   for(;;){
    System.out.println("請(qǐng)選擇交易類型:");
    System.out.println("1、存錢   2、取錢  3、查詢余額");
    int cz=sca.nextInt();
    if(cz==1){
     cun();
    }else if(cz==2){
     qu();
    }else if(cz==3){
     query();
    }else{
     System.out.println("謝謝使用!");
     break;
    }
   }
  }else{
   System.out.println("登錄失敗");
  }
 }
 
 public static void cun() throws Exception{
  System.out.println("請(qǐng)輸入你的存款金額:");
  double money=sca.nextDouble();
  String sql="update account set money=money+"+money;
  boolean a=sc.execute(sql);
  if(!a){
   System.out.println("存款成功!");
  }
 }
 
 public static void qu() throws Exception{
  System.out.println("請(qǐng)輸入你的取款金額:");
  double money=sca.nextDouble();
  String sql="update account set money=money-"+money;
  System.out.println(sql);
  boolean a=sc.execute(sql);
  if(!a){
   System.out.println("取款成功!");
  }
 }
 
 public static int queryAccount(String username,String password) throws Exception{
  String sql="select * from account where aname='"+username+"'and apwd='"+password+"'";
  ResultSet rs=sc.executeQuery(sql);
  int num=0;
  while(rs.next()){
   num++;
  }
  return num;
 }
 
 public static void query() throws Exception{
 String sql="select money from account where aname='"+username+"'";
 ResultSet rs=sc.executeQuery(sql);
 double money=0;
 while(rs.next()){
  money=rs.getDouble(1);
      }
 System.out.println("你的賬戶余額:"+money);
 }
 
 
  public static void getStatement() throws Exception{
  Class.forName("com.mysql.jdbc.Driver");
  String url="jdbc:mysql://localhost:3306/atm";
  String username="root";
  String password="root";
  Connection c=DriverManager.getConnection(url,username,password);
  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