使用java和mysql编写银行管理系统的代码
时间: 2024-01-20 07:06:54 浏览: 89
由于银行管理系统是一个比较复杂的系统,代码量比较大,这里我提供一个简单的代码示例,仅供参考:
1. Customer实体类
```
public class Customer {
private int id;
private String name;
private String address;
private String phone;
public Customer(int id, String name, String address, String phone) {
this.id = id;
this.name = name;
this.address = address;
this.phone = phone;
}
// getters and setters
}
```
2. CustomerDAO类
```
public class CustomerDAO {
private Connection conn;
public CustomerDAO(Connection conn) {
this.conn = conn;
}
public void addCustomer(Customer customer) throws SQLException {
PreparedStatement ps = conn.prepareStatement("INSERT INTO customer (id, name, address, phone) VALUES (?, ?, ?, ?)");
ps.setInt(1, customer.getId());
ps.setString(2, customer.getName());
ps.setString(3, customer.getAddress());
ps.setString(4, customer.getPhone());
ps.executeUpdate();
}
public void deleteCustomer(int id) throws SQLException {
PreparedStatement ps = conn.prepareStatement("DELETE FROM customer WHERE id = ?");
ps.setInt(1, id);
ps.executeUpdate();
}
public void updateCustomer(Customer customer) throws SQLException {
PreparedStatement ps = conn.prepareStatement("UPDATE customer SET name = ?, address = ?, phone = ? WHERE id = ?");
ps.setString(1, customer.getName());
ps.setString(2, customer.getAddress());
ps.setString(3, customer.getPhone());
ps.setInt(4, customer.getId());
ps.executeUpdate();
}
public Customer getCustomerById(int id) throws SQLException {
PreparedStatement ps = conn.prepareStatement("SELECT * FROM customer WHERE id = ?");
ps.setInt(1, id);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
return new Customer(rs.getInt("id"), rs.getString("name"), rs.getString("address"), rs.getString("phone"));
} else {
return null;
}
}
}
```
3. CustomerService类
```
public class CustomerService {
private CustomerDAO customerDAO;
public CustomerService(CustomerDAO customerDAO) {
this.customerDAO = customerDAO;
}
public void addCustomer(int id, String name, String address, String phone) throws SQLException {
Customer customer = new Customer(id, name, address, phone);
customerDAO.addCustomer(customer);
}
public void deleteCustomer(int id) throws SQLException {
customerDAO.deleteCustomer(id);
}
public void updateCustomer(int id, String name, String address, String phone) throws SQLException {
Customer customer = new Customer(id, name, address, phone);
customerDAO.updateCustomer(customer);
}
public Customer getCustomerById(int id) throws SQLException {
return customerDAO.getCustomerById(id);
}
}
```
4. 用户界面代码
```
public class BankManagementSystemGUI {
private CustomerService customerService;
public BankManagementSystemGUI(CustomerService customerService) {
this.customerService = customerService;
}
public void showCustomerForm() {
// create and show customer form
}
public void addCustomer(int id, String name, String address, String phone) throws SQLException {
customerService.addCustomer(id, name, address, phone);
}
public void deleteCustomer(int id) throws SQLException {
customerService.deleteCustomer(id);
}
public void updateCustomer(int id, String name, String address, String phone) throws SQLException {
customerService.updateCustomer(id, name, address, phone);
}
public Customer getCustomerById(int id) throws SQLException {
return customerService.getCustomerById(id);
}
}
```
以上是一个简单的银行管理系统的Java和MySQL实现的代码示例,仅供参考。实际实现中,需要根据具体需求进行更加详细的设计和实现。
阅读全文