关键词

java模拟ATM功能(控制台连接Mysql数据库)

以下是详细讲解“java模拟ATM功能(控制台连接Mysql数据库)”的完整攻略:

系统要求

  • JDK 1.8 或以上版本
  • Mysql 5.0 或以上版本

准备工作

  1. 创建一个名为 atm 的 Mysql 数据库
    CREATE DATABASE atm;
  2. 创建一个名为 users 的表,用于储存 ATM 用户信息
    USE atm;
    CREATE TABLE users (
    id INT(11) PRIMARY KEY NOT NULL AUTO_INCREMENT,
    card_id VARCHAR(20) NOT NULL,
    password CHAR(32) NOT NULL,
    balance DECIMAL(10,2) NOT NULL
    );

连接数据库

  1. 导入 jdbc 驱动
    java
    // 加载驱动,需要将驱动 JAR 包放入工程的 CLASSPATH 中
    Class.forName("com.mysql.jdbc.Driver");
  2. 建立数据库连接
    java
    String url = "jdbc:mysql://localhost:3306/atm?useSSL=false&characterEncoding=utf-8";
    String username = "root";
    String password = "123456";
    Connection connection = DriverManager.getConnection(url, username, password);

    其中,url 表示连接字符串,其中 3306 是数据库服务的端口号,atm 是数据库名,useSSL=false 表示不使用 SSL 连接,characterEncoding=utf-8 表示使用 UTF-8 编码。
  3. 执行 SQL 语句
    java
    Statement statement = connection.createStatement();
    ResultSet resultSet = statement.executeQuery("SELECT * FROM users");
    while (resultSet.next()) {
    int id = resultSet.getInt("id");
    String cardId = resultSet.getString("card_id");
    String password = resultSet.getString("password");
    double balance = resultSet.getDouble("balance");
    System.out.printf("ID:%d,卡号:%s,密码:%s,余额:%.2f\n", id, cardId, password, balance);
    }

ATM 功能设计

ATM 功能主要分为登陆、查询、取款、存款、转账、修改密码和退出等功能。

登陆

登陆需要用户输入银行卡号和密码,然后验证是否匹配数据库中的信息。

// 判断是否存在该用户
PreparedStatement checkUserStmt = connection.prepareStatement("SELECT * FROM users WHERE card_id = ? AND password = ?");
checkUserStmt.setString(1, cardId);
checkUserStmt.setString(2, password);
ResultSet resultSet = checkUserStmt.executeQuery();
if (resultSet.next()) {
    // 登陆成功
} else {
    // 登陆失败
}

查询

查询需要显示当前账户的余额。

PreparedStatement checkBalanceStmt = connection.prepareStatement("SELECT balance FROM users WHERE card_id = ?");
checkBalanceStmt.setString(1, cardId);
ResultSet resultSet = checkBalanceStmt.executeQuery();
if (resultSet.next()) {
    double balance = resultSet.getDouble("balance");
    System.out.printf("您的余额为:%.2f\n", balance);
} else {
    System.out.println("查询失败,请重新尝试!");
}

取款

取款需要输入取款金额,并进行余额的检查。如果余额足够,就会更新数据库中的余额信息。

// 检查余额是否足够
PreparedStatement checkBalanceStmt = connection.prepareStatement("SELECT balance FROM users WHERE card_id = ?");
checkBalanceStmt.setString(1, cardId);
ResultSet resultSet = checkBalanceStmt.executeQuery();
if (resultSet.next()) {
    double balance = resultSet.getDouble("balance");
    if (balance >= amount) {
        // 更新余额
        PreparedStatement updateBalanceStmt = connection.prepareStatement("UPDATE users SET balance = balance - ? WHERE card_id = ?");
        updateBalanceStmt.setDouble(1, amount);
        updateBalanceStmt.setString(2, cardId);
        updateBalanceStmt.executeUpdate();
        System.out.printf("取款成功,当前余额为:%.2f\n", balance - amount);
    } else {
        System.out.println("余额不足!");
    }
} else {
    System.out.println("查询失败,请重新尝试!");
}

存款

存款需要输入存款金额,并将其添加到数据库中。

// 更新余额
PreparedStatement updateBalanceStmt = connection.prepareStatement("UPDATE users SET balance = balance + ? WHERE card_id = ?");
updateBalanceStmt.setDouble(1, amount);
updateBalanceStmt.setString(2, cardId);
updateBalanceStmt.executeUpdate();
System.out.printf("存款成功,当前余额为:%.2f\n", currentBalance + amount);

转账

转账需要输入目标账户的银行卡号和转账金额,并进行余额的检查。如果余额足够,就会更新数据库中的余额信息。

// 判断目标账户是否存在
PreparedStatement checkTargetUserStmt = connection.prepareStatement("SELECT balance FROM users WHERE card_id = ?");
checkTargetUserStmt.setString(1, targetCardId);
ResultSet targetResultSet = checkTargetUserStmt.executeQuery();
if (targetResultSet.next()) {
    double targetBalance = targetResultSet.getDouble("balance");
    if (currentBalance >= amount) {
        // 更新余额
        PreparedStatement updateBalanceStmt = connection.prepareStatement("UPDATE users SET balance = balance - ? WHERE card_id = ?");
        updateBalanceStmt.setDouble(1, amount);
        updateBalanceStmt.setString(2, cardId);
        updateBalanceStmt.executeUpdate();
        PreparedStatement updateTargetBalanceStmt = connection.prepareStatement("UPDATE users SET balance = balance + ? WHERE card_id = ?");
        updateTargetBalanceStmt.setDouble(1, amount);
        updateTargetBalanceStmt.setString(2, targetCardId);
        updateTargetBalanceStmt.executeUpdate();
        System.out.printf("转账成功,当前余额为:%.2f\n", currentBalance - amount);
    } else {
        System.out.println("余额不足!");
    }
} else {
    System.out.println("目标账户不存在!");
}

修改密码

修改密码需要输入旧密码和新密码两次,并进行检查后更新密码。

// 判断旧密码是否正确
PreparedStatement checkPasswordStmt = connection.prepareStatement("SELECT * FROM users WHERE card_id = ? AND password = ?");
checkPasswordStmt.setString(1, cardId);
checkPasswordStmt.setString(2, oldPassword);
ResultSet resultSet = checkPasswordStmt.executeQuery();
if (resultSet.next()) {
    // 更新密码
    PreparedStatement updatePasswordStmt = connection.prepareStatement("UPDATE users SET password = ? WHERE card_id = ?");
    updatePasswordStmt.setString(1, newPassword);
    updatePasswordStmt.setString(2, cardId);
    updatePasswordStmt.executeUpdate();
    System.out.println("密码修改成功!");
} else {
    System.out.println("旧密码错误!");
}

退出

退出功能仅需跳出 ATM 功能即可。

System.exit(0);

示例代码

查询所有用户信息

Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/atm?useSSL=false&characterEncoding=utf-8";
String username = "root";
String password = "123456";
Connection connection = DriverManager.getConnection(url, username, password);

Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("SELECT * FROM users");
while (resultSet.next()) {
    int id = resultSet.getInt("id");
    String cardId = resultSet.getString("card_id");
    String password = resultSet.getString("password");
    double balance = resultSet.getDouble("balance");
    System.out.printf("ID:%d,卡号:%s,密码:%s,余额:%.2f\n", id, cardId, password, balance);
}

resultSet.close();
statement.close();
connection.close();

查找银行卡号为 1 的用户信息

Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/atm?useSSL=false&characterEncoding=utf-8";
String username = "root";
String password = "123456";
Connection connection = DriverManager.getConnection(url, username, password);

PreparedStatement statement = connection.prepareStatement("SELECT * FROM users WHERE id = ?");
statement.setInt(1, 1);
ResultSet resultSet = statement.executeQuery();
if (resultSet.next()) {
    int id = resultSet.getInt("id");
    String cardId = resultSet.getString("card_id");
    String password = resultSet.getString("password");
    double balance = resultSet.getDouble("balance");
    System.out.printf("ID:%d,卡号:%s,密码:%s,余额:%.2f\n", id, cardId, password, balance);
} else {
    System.out.println("没有该用户!");
}

resultSet.close();
statement.close();
connection.close();

本文链接:http://task.lmcjl.com/news/13309.html

展开阅读全文