學習java第42天
努力努力在努力,堅持堅持在堅持!
mysql代碼
創建數據庫 Account
- 創建一張表 t_ccount。有以下列
- cardId:字符串,主鍵
- password:字符串,非空
- username:字符串,非空
- balance:小數,非空
- phone:字符串,非空
CREATE DATABASE Account CHARACTER SET utf8;
USE Account
CREATE TABLE t_account(
cardid VARCHAR(20) PRIMARY KEY,
`password` VARCHAR(20) NOT NULL,
username VARCHAR(20) NOT NULL,
balance DOUBLE NOT NULL,
phone VARCHAR(11) NOT NULL
)CHARSET=utf8;
INSERT INTO t_account VALUES('b101','123','大慶',1000,'132444')
INSERT INTO t_account VALUES('b102','123','大白',6000,'132455')
INSERT INTO t_account VALUES('b103','123','大A',7000,'132466')
INSERT INTO t_account VALUES('b104','123','大B',8000,'132477')
SELECT * FROM t_account;
創建AccountSystem類,完成下列功能
- 開戶:控制檯輸入所有的賬戶信息,使用PreparedStatement添加至t_account表
- 存款:控制檯輸入卡號、密碼、存儲金額進行修改
- 取款:輸入卡號、密碼、取款金額
- 轉賬:輸入卡號、密碼、對方卡號、轉賬金額進行修改
- 修改密碼:控制檯輸入卡號、密碼,再輸入新密碼進行修改
- 註銷:控制檯輸入卡號、密碼,刪除對應的賬戶信息
AccountSystem代碼
package com.qf.day2.t3;
import java.sql.*;
import java.util.Scanner;
/*
- 開戶:控制檯輸入所有的賬戶信息,使用PreparedStatement添加至t_account表
- 存款:控制檯輸入卡號、密碼、存儲金額進行修改
- 取款:輸入卡號、密碼、取款金額
- 轉賬:輸入卡號、密碼、對方卡號、轉賬金額進行修改
- 修改密碼:控制檯輸入卡號、密碼,再輸入新密碼進行修改
- 註銷:控制檯輸入卡號、密碼,刪除對應的賬戶信息
try-catch 快速按鍵-- ctrl + alt + t
代碼格式化 ctrl + alt + l
快速定位異常處, F2
提醒解決方案 alt+ enter
*/
public class AccountSystem {
Scanner sc = new Scanner(System.in);
PreparedStatement pre = null;
private static Connection conn = null;
ResultSet re = null;
static {
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/account?useUnicode=true&characterEncoding=utf8","root","740056981");
} catch (Exception e) {
e.printStackTrace();
}
}
//開戶
public void refiter(){
System.out.println("請輸入卡號");
String cid = sc.next();
System.out.println("請輸入密碼:");
String password = sc.next();
System.out.println("請輸入用戶名:");
String username = sc.next();
System.out.println("請輸入存款金額:");
double balance = sc.nextDouble();
System.out.println("請輸入手機號:");
String phone = sc.next();
try {
String sql = "insert into t_account(cardid,password,username,balance,phone) values (?,?,?,?,?)";
pre = conn.prepareStatement(sql);
pre.setString(1,cid);
pre.setString(2,password);
pre.setString(3,username);
pre.setDouble(4,balance);
pre.setString(5,phone);
int re = pre.executeUpdate();
if(re>0){
System.out.println("開戶成功");
}else{
System.out.println("開戶失敗");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if(pre !=null){
pre.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
//存款
public void saveMoney(){
System.out.println("請輸入卡號:");
String cid = sc.next();
System.out.println("請輸入密碼:");
String password = sc.next();
System.out.println("請輸入存款金額:");
double balance = sc.nextDouble();
if (balance > 0) {
try {
String sql = "update t_account set balance = balance+? where cardid=? and password = ?";
pre = conn.prepareStatement(sql);
pre.setDouble(1,balance);
pre.setString(2,cid);
pre.setString(3,password);
int i = pre.executeUpdate();
if(i>0){
System.out.println("存款成功");
}else{
System.out.println("存款失敗");
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if(pre != null){
pre.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
} else {
System.out.println("輸入存款金額錯誤,重新輸入");
}
}
//取款
public void takeMoney(){
System.out.println("請輸入卡號:");
String cid = sc.next();
System.out.println("請輸入密碼:");
String password = sc.next();
System.out.println("請輸入取款金額:");
double balance = sc.nextDouble();
if (balance > 0) {
try {
String sql1= "select balance from t_account where cardid = ? and password = ?";
pre = conn.prepareStatement(sql1);
pre.setString(1,cid);
pre.setString(2,password);
re = pre.executeQuery();
if(re.next()) {
double money = re.getDouble("balance");
if (balance <= money) {
String sql2 = "update t_account set balance = balance-? where cardid=? and password = ?";
pre = conn.prepareStatement(sql2);
pre.setDouble(1, balance);
pre.setString(2, cid);
pre.setString(3, password);
int i = pre.executeUpdate();
if (i > 0) {
System.out.println("取款成功");
}
}else{
System.out.println("餘額不足");
}
}else{
System.out.println("輸入的用戶名或密碼錯誤");
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
try {
if(re != null){
re.close();
}
if(pre != null){
pre.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
} else {
System.out.println("輸入存款金額錯誤,重新輸入");
}
}
//轉賬
public void tranSefer(){
System.out.println("請輸入自字的卡號:");
String cid = sc.next();
System.out.println("請輸入密碼:");
String password = sc.next();
System.out.println("請輸入轉賬的卡號:");
String cid2 = sc.next();
System.out.println("請輸入轉賬金額:");
double money = sc.nextDouble();
if(money > 0){
try {
String sql1 = "select balance from t_account where cardid=? and password = ?";
pre = conn.prepareStatement(sql1);
pre.setString(1,cid);
pre.setString(2,password);
re = pre.executeQuery();
if(re.next()){
double money1 = re.getDouble("balance");//獲取自己賬號裏的餘額
if(money <= money1){//判斷轉賬金額是否小於餘額
try {
conn.setAutoCommit(false);
String sql2 = "update t_account set balance = balance-? where cardid = ? and password =?";
pre = conn.prepareStatement(sql2);
pre.setDouble(1,money);
pre.setString(2,cid);
pre.setString(3,password);
int i = pre.executeUpdate();
String sql3 = "update t_account set balance = balance + ? where cardid = ?";
pre = conn.prepareStatement(sql3);
pre.setDouble(1,money);
pre.setString(2,cid2);
int i1 = pre.executeUpdate();
if(i>0 && i1>0){
conn.commit();
System.out.println("轉賬成功");
}else{
conn.rollback();
System.out.println("轉賬失敗");
}
} catch (Exception e) {
conn.rollback();
System.out.println("轉賬失敗");
e.printStackTrace();
}
}else{
System.out.println("餘額不足");
}
}else{
System.out.println("賬號或密碼錯誤");
}
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
if(re!=null){
re.close();
}
if(pre!=null){
pre.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}else {
System.out.println("轉賬金額錯誤");
}
}
//修改密碼
public void setPas(){
System.out.println("請輸入卡號:");
String cid = sc.next();
System.out.println("請輸入密碼:");
String password = sc.next();
System.out.println("請輸入新密碼:");
String newpas = sc.next();
try {
String sql = "update t_account set password=? where cardid = ? and password = ?";
pre = conn.prepareStatement(sql);
pre.setString(1,newpas);
pre.setString(2,cid);
pre.setString(3,password);
int i = pre.executeUpdate();
if(i>0){
System.out.println("修改成功");
}else{
System.out.println("修改失敗");
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
if(pre != null){
pre.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
//註銷
public void logOff(){
System.out.println("請輸入卡號:");
String cid = sc.next();
System.out.println("請輸入密碼:");
String password = sc.next();
try {
String sql = "delete from t_account where cardid = ? and password = ?";
pre = conn.prepareStatement(sql);
pre.setString(1,cid);
pre.setString(2,password);
int i = pre.executeUpdate();
if(i>0){
System.out.println("註銷成功");
}else{
System.out.println("註銷失敗");
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if(pre != null){
pre.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
//退出
public void exit(){
try {
if(conn != null){
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
TestAccount代碼
package com.qf.day2.t3;
import java.sql.SQLOutput;
import java.util.Scanner;
public class TestAccount {
public static void main(String[] args) {
AccountSystem as = new AccountSystem();
Scanner sc = new Scanner(System.in);
System.out.println("歡迎來到英雄聯盟銀行系統");
int choice = 0;
do{
System.out.println("1、開戶 2、存款 3、取款 4、轉賬 5、修改密碼 6、註銷 0、退出");
System.out.println("請選擇");
choice = sc.nextInt();
switch (choice){
case 1:
as.refiter();
break;
case 2:
as.saveMoney();
break;
case 3:
as.takeMoney();
break;
case 4:
as.tranSefer();
break;
case 5:
as.setPas();
break;
case 6:
as.logOff();
break;
case 0:
as.exit();
return;
}
}while (choice !=0);
}
}