以下是使用 JDBC 中的事务来实现银行转账功能,并将数据库连接信息配置在外部配置文件中的示例代码:
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Properties;
public class BankTransactionExample {
public static void main(String[] args) {
// 加载配置文件
Properties properties = new Properties();
try {
properties.load(new FileInputStream("config.properties"));
} catch (IOException e) {
System.out.println("无法加载配置文件");
e.printStackTrace();
return;
}
// 获取配置
String url = properties.getProperty("url");
String username = properties.getProperty("username");
String password = properties.getProperty("password");
// 注册JDBC驱动程序
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
System.out.println("无法找到JDBC驱动程序");
e.printStackTrace();
return;
}
// 建立连接
Connection connection = null;
try {
connection = DriverManager.getConnection(url, username, password);
System.out.println("成功连接到数据库");
// 开启事务
connection.setAutoCommit(false);
// 执行转账操作
transferFunds(connection, "account1", "account2", 100);
// 提交事务
connection.commit();
System.out.println("转账成功");
} catch (SQLException e) {
System.out.println("转账失败,进行回滚");
e.printStackTrace();
if (connection != null) {
try {
connection.rollback();
System.out.println("事务回滚成功");
} catch (SQLException ex) {
System.out.println("事务回滚失败");
ex.printStackTrace();
}
}
} finally {
// 关闭连接
if (connection != null) {
try {
connection.close();
System.out.println("成功关闭数据库连接");
} catch (SQLException e) {
System.out.println("无法关闭数据库连接");
e.printStackTrace();
}
}
}
}
private static void transferFunds(Connection connection, String fromAccount, String toAccount, double amount) throws SQLException {
// 查询转出账户余额
double fromBalance = getAccountBalance(connection, fromAccount);
if (fromBalance < amount) {
throw new SQLException("转出账户余额不足");
}
// 更新转出账户余额
updateAccountBalance(connection, fromAccount, fromBalance - amount);
// 查询转入账户余额
double toBalance = getAccountBalance(connection, toAccount);
// 更新转入账户余额
updateAccountBalance(connection, toAccount, toBalance + amount);
}
private static double getAccountBalance(Connection connection, String account) throws SQLException {
String query = "SELECT balance FROM accounts WHERE account_number = ?";
try (PreparedStatement statement = connection.prepareStatement(query)) {
statement.setString(1, account);
try (ResultSet resultSet = statement.executeQuery()) {
if (resultSet.next()) {
return resultSet.getDouble("balance");
} else {
throw new SQLException("未找到账户:" + account);
}
}
}
}
private static void updateAccountBalance(Connection connection, String account, double newBalance) throws SQLException {
String query = "UPDATE accounts SET balance = ? WHERE account_number = ?";
try (PreparedStatement statement = connection.prepareStatement(query)) {
statement.setDouble(1, newBalance);
statement.setString(2, account);
statement.executeUpdate();
}
}
}
在上述代码中,我们使用 Properties
类来加载外部的配置文件 config.properties
,该文件包含了数据库连接信息。可以根据需要调整配置文件的路径和格式。
在配置文件中,可以指定以下属性:
url=jdbc:mysql://localhost:3306/bank
username=root
password=password
请确保配置文件中的属性与你的实际数据库连接信息相匹配。
通过这种方式,可以将数据库连接信息从代码中分离出来,使代码更具可维护性和灵活性。