生成一个用Java和mysql数据库的客户管理系统
时间: 2024-03-09 19:45:54 浏览: 24
好的,我可以为您生成一个Java和MySQL数据库的客户管理系统的初始框架。以下是一个简单的代码示例:
首先,我们需要创建一个数据库,并在其中创建一个名为`customers`的表,该表包含客户的姓名、地址和电话号码:
```sql
CREATE DATABASE customer_management_system;
USE customer_management_system;
CREATE TABLE customers (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
address VARCHAR(255) NOT NULL,
phone VARCHAR(255) NOT NULL
);
```
然后,我们可以创建一个`Customer`类来表示客户,并使用JDBC连接MySQL数据库:
```java
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
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;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public static List<Customer> getAllCustomers() throws SQLException {
List<Customer> customers = new ArrayList<>();
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
conn = DriverManager.getConnection("jdbc:mysql://localhost/customer_management_system", "root", "password");
stmt = conn.createStatement();
rs = stmt.executeQuery("SELECT * FROM customers");
while (rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
String address = rs.getString("address");
String phone = rs.getString("phone");
Customer customer = new Customer(id, name, address, phone);
customers.add(customer);
}
} finally {
if (rs != null) rs.close();
if (stmt != null) stmt.close();
if (conn != null) conn.close();
}
return customers;
}
public void save() throws SQLException {
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
conn = DriverManager.getConnection("jdbc:mysql://localhost/customer_management_system", "root", "password");
if (this.id == 0) {
stmt = conn.prepareStatement("INSERT INTO customers (name, address, phone) VALUES (?, ?, ?)", Statement.RETURN_GENERATED_KEYS);
stmt.setString(1, this.name);
stmt.setString(2, this.address);
stmt.setString(3, this.phone);
stmt.executeUpdate();
rs = stmt.getGeneratedKeys();
if (rs.next()) {
this.id = rs.getInt(1);
}
} else {
stmt = conn.prepareStatement("UPDATE customers SET name=?, address=?, phone=? WHERE id=?");
stmt.setString(1, this.name);
stmt.setString(2, this.address);
stmt.setString(3, this.phone);
stmt.setInt(4, this.id);
stmt.executeUpdate();
}
} finally {
if (rs != null) rs.close();
if (stmt != null) stmt.close();
if (conn != null) conn.close();
}
}
public void delete() throws SQLException {
Connection conn = null;
PreparedStatement stmt = null;
try {
conn = DriverManager.getConnection("jdbc:mysql://localhost/customer_management_system", "root", "password");
stmt = conn.prepareStatement("DELETE FROM customers WHERE id=?");
stmt.setInt(1, this.id);
stmt.executeUpdate();
} finally {
if (stmt != null) stmt.close();
if (conn != null) conn.close();
}
}
public static void main(String[] args) throws SQLException {
// 示例代码
Customer customer = new Customer(0, "John Doe", "123 Main St", "555-555-5555");
customer.save();
List<Customer> customers = Customer.getAllCustomers();
for (Customer c : customers) {
System.out.println(c.getName());
}
customer.setName("Jane Doe");
customer.save();
customer.delete();
}
}
```
这里定义了一个`Customer`类,它包含了客户的基本信息,并使用JDBC连接MySQL数据库。`getAllCustomers`方法用于从数据库中获取所有客户,`save`方法用于将当前对象保存到数据库中,`delete`方法用于从数据库中删除当前对象。您可以在这个框架的基础上继续完善客户管理系统的功能。