用Java 编写一个模拟转账的应用软件。 实验步骤: (1)创建数据库和表 (2)编写Swing代码 (3)编写JDBC代码 (4)调试、运行
时间: 2024-06-08 14:12:07 浏览: 137
好的,根据您的需求,我将为您提供一个简单的Java应用程序,模拟转账过程。请按照以下步骤进行操作:
1. 创建数据库和表
首先,我们需要在MySQL数据库中创建一个名为“bank”的数据库,并在其中创建一个名为“account”的表。表中应包含“id”、“name”和“balance”三个字段,其中“id”为自增主键,name为varchar类型,balance为double类型。
CREATE DATABASE bank;
USE bank;
CREATE TABLE account (
id INT(11) PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(50),
balance DOUBLE
);
2. 编写Swing代码
接下来,我们将编写一个简单的Swing用户界面来启动我们的应用程序。在以下代码中,我们创建了一个JFrame窗口,包含两个JLabel标签,两个JTextField文本框,一个JButton按钮和一个JPanel面板。用户可以在文本框中输入转账金额和转账接收方名称,并单击按钮以执行转账操作。
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TransferApp extends JFrame {
private JLabel amountLabel = new JLabel("转账金额:");
private JTextField amountField = new JTextField(10);
private JLabel recipientLabel = new JLabel("转账接收方:");
private JTextField recipientField = new JTextField(10);
private JButton transferButton = new JButton("转账");
private JPanel panel = new JPanel();
public TransferApp() {
panel.setLayout(new GridLayout(3, 2));
panel.add(amountLabel);
panel.add(amountField);
panel.add(recipientLabel);
panel.add(recipientField);
panel.add(transferButton);
transferButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// TODO: 调用JDBC代码进行转账操作
}
});
add(panel);
setTitle("转账应用程序");
setSize(300, 150);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new TransferApp();
}
});
}
}
3. 编写JDBC代码
现在,我们将编写JDBC代码来执行转账操作。在以下代码中,我们首先连接到MySQL数据库,然后查询发送方账户的余额。如果余额足够,我们将从发送方账户中扣除转账金额,并将其添加到接收方账户中。最后,我们将更新数据库中的账户余额。
import java.sql.*;
public class TransferService {
private static final String DB_URL = "jdbc:mysql://localhost:3306/bank";
private static final String DB_USER = "root";
private static final String DB_PASSWORD = "password";
public boolean transfer(String sender, String recipient, double amount) {
Connection conn = null;
try {
conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
conn.setAutoCommit(false);
double senderBalance = getBalance(conn, sender);
if (senderBalance < amount) {
return false;
}
updateBalance(conn, sender, senderBalance - amount);
updateBalance(conn, recipient, getBalance(conn, recipient) + amount);
conn.commit();
return true;
} catch (SQLException e) {
if (conn != null) {
try {
conn.rollback();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
e.printStackTrace();
return false;
} finally {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
private double getBalance(Connection conn, String accountName) throws SQLException {
String sql = "SELECT balance FROM account WHERE name = ?";
PreparedStatement stmt = conn.prepareStatement(sql);
stmt.setString(1, accountName);
ResultSet rs = stmt.executeQuery();
if (rs.next()) {
return rs.getDouble("balance");
} else {
throw new RuntimeException("账户不存在: " + accountName);
}
}
private void updateBalance(Connection conn, String accountName, double newBalance) throws SQLException {
String sql = "UPDATE account SET balance = ? WHERE name = ?";
PreparedStatement stmt = conn.prepareStatement(sql);
stmt.setDouble(1, newBalance);
stmt.setString(2, accountName);
int rowsUpdated = stmt.executeUpdate();
if (rowsUpdated != 1) {
throw new RuntimeException("更新账户余额失败: " + accountName);
}
}
}
4. 调试、运行
现在,我们可以将JDBC代码添加到Swing应用程序中,并在按钮单击事件处理程序中调用它。完成后,我们可以运行我们的应用程序并进行测试。以下是完整的代码:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
public class TransferApp extends JFrame {
private JLabel amountLabel = new JLabel("转账金额:");
private JTextField amountField = new JTextField(10);
private JLabel recipientLabel = new JLabel("转账接收方:");
private JTextField recipientField = new JTextField(10);
private JButton transferButton = new JButton("转账");
private JPanel panel = new JPanel();
private TransferService transferService = new TransferService();
public TransferApp() {
panel.setLayout(new GridLayout(3, 2));
panel.add(amountLabel);
panel.add(amountField);
panel.add(recipientLabel);
panel.add(recipientField);
panel.add(transferButton);
transferButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String sender = "张三";
String recipient = recipientField.getText();
double amount = Double.parseDouble(amountField.getText());
boolean success = transferService.transfer(sender, recipient, amount);
if (success) {
JOptionPane.showMessageDialog(panel, "转账成功!");
} else {
JOptionPane.showMessageDialog(panel, "余额不足,转账失败!");
}
}
});
add(panel);
setTitle("转账应用程序");
setSize(300, 150);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new TransferApp();
}
});
}
}
请注意,您需要将JDBC驱动程序添加到您的类路径中,以便您的应用程序能够连接到MySQL数据库。您可以从以下链接下载MySQL JDBC驱动程序:https://dev.mysql.com/downloads/connector/j/
阅读全文