以下是一个简单的客户管理模块的代码示例,使用Java的GUI类实现客户信息的展示及增删改查操作,同时使用JDBC与数据库进行交互。这个示例中使用了MySQL数据库,所以需要先安装MySQL数据库,并创建一个名为“customers”的数据库,并在其中创建一个名为“customer_info”的表,表结构如下:
| Field | Type | Null | Key | Default | Extra |
|-------------|--------------|-------|-----|---------|----------------|
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(255)| YES | | NULL | |
| email | varchar(255)| YES | | NULL | |
| phone | varchar(255)| YES | | NULL | |
| address | varchar(255)| YES | | NULL | |
| created_at | timestamp | NO | | NULL | |
| updated_at | timestamp | NO | | NULL | |
代码如下:
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
public class CustomerManagementSystem extends JFrame implements ActionListener {
private JLabel nameLabel, emailLabel, phoneLabel, addressLabel;
private JTextField nameField, emailField, phoneField, addressField;
private JButton addButton, updateButton, deleteButton, searchButton;
private JTable table;
private JScrollPane scrollPane;
private Connection connection;
private PreparedStatement preparedStatement;
public CustomerManagementSystem() {
setTitle("Customer Management System");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(800, 500);
nameLabel = new JLabel("Name:");
emailLabel = new JLabel("Email:");
phoneLabel = new JLabel("Phone:");
addressLabel = new JLabel("Address:");
nameField = new JTextField(20);
emailField = new JTextField(20);
phoneField = new JTextField(20);
addressField = new JTextField(20);
addButton = new JButton("Add");
updateButton = new JButton("Update");
deleteButton = new JButton("Delete");
searchButton = new JButton("Search");
addButton.addActionListener(this);
updateButton.addActionListener(this);
deleteButton.addActionListener(this);
searchButton.addActionListener(this);
JPanel inputPanel = new JPanel(new GridLayout(4, 2));
inputPanel.add(nameLabel);
inputPanel.add(nameField);
inputPanel.add(emailLabel);
inputPanel.add(emailField);
inputPanel.add(phoneLabel);
inputPanel.add(phoneField);
inputPanel.add(addressLabel);
inputPanel.add(addressField);
JPanel buttonPanel = new JPanel(new GridLayout(1, 4));
buttonPanel.add(addButton);
buttonPanel.add(updateButton);
buttonPanel.add(deleteButton);
buttonPanel.add(searchButton);
JPanel formPanel = new JPanel(new BorderLayout());
formPanel.add(inputPanel, BorderLayout.CENTER);
formPanel.add(buttonPanel, BorderLayout.SOUTH);
table = new JTable();
scrollPane = new JScrollPane(table);
JPanel panel = new JPanel(new BorderLayout());
panel.add(formPanel, BorderLayout.NORTH);
panel.add(scrollPane, BorderLayout.CENTER);
add(panel);
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://localhost/customers", "root", "");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
updateTable();
}
private void updateTable() {
try {
preparedStatement = connection.prepareStatement("SELECT * FROM customer_info");
ResultSet resultSet = preparedStatement.executeQuery();
table.setModel(DbUtils.resultSetToTableModel(resultSet));
} catch (SQLException e) {
e.printStackTrace();
}
}
private void clearFields() {
nameField.setText("");
emailField.setText("");
phoneField.setText("");
addressField.setText("");
}
private void addCustomer() {
try {
preparedStatement = connection.prepareStatement("INSERT INTO customer_info (name, email, phone, address, created_at, updated_at) VALUES (?, ?, ?, ?, NOW(), NOW())");
preparedStatement.setString(1, nameField.getText());
preparedStatement.setString(2, emailField.getText());
preparedStatement.setString(3, phoneField.getText());
preparedStatement.setString(4, addressField.getText());
int result = preparedStatement.executeUpdate();
if (result > 0) {
JOptionPane.showMessageDialog(this, "Customer added successfully!");
} else {
JOptionPane.showMessageDialog(this, "Error adding customer!");
}
clearFields();
updateTable();
} catch (SQLException e) {
e.printStackTrace();
}
}
private void updateCustomer() {
int row = table.getSelectedRow();
if (row == -1) {
JOptionPane.showMessageDialog(this, "Please select a customer to update!");
} else {
try {
preparedStatement = connection.prepareStatement("UPDATE customer_info SET name = ?, email = ?, phone = ?, address = ?, updated_at = NOW() WHERE id = ?");
preparedStatement.setString(1, nameField.getText());
preparedStatement.setString(2, emailField.getText());
preparedStatement.setString(3, phoneField.getText());
preparedStatement.setString(4, addressField.getText());
preparedStatement.setInt(5, (int) table.getValueAt(row, 0));
int result = preparedStatement.executeUpdate();
if (result > 0) {
JOptionPane.showMessageDialog(this, "Customer updated successfully!");
} else {
JOptionPane.showMessageDialog(this, "Error updating customer!");
}
clearFields();
updateTable();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
private void deleteCustomer() {
int row = table.getSelectedRow();
if (row == -1) {
JOptionPane.showMessageDialog(this, "Please select a customer to delete!");
} else {
int result = JOptionPane.showConfirmDialog(this, "Are you sure you want to delete this customer?", "Delete Customer", JOptionPane.YES_NO_OPTION);
if (result == JOptionPane.YES_OPTION) {
try {
preparedStatement = connection.prepareStatement("DELETE FROM customer_info WHERE id = ?");
preparedStatement.setInt(1, (int) table.getValueAt(row, 0));
int deleteResult = preparedStatement.executeUpdate();
if (deleteResult > 0) {
JOptionPane.showMessageDialog(this, "Customer deleted successfully!");
} else {
JOptionPane.showMessageDialog(this, "Error deleting customer!");
}
clearFields();
updateTable();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
private void searchCustomer() {
String name = nameField.getText();
String email = emailField.getText();
String phone = phoneField.getText();
String address = addressField.getText();
try {
String sql = "SELECT * FROM customer_info WHERE 1=1";
if (!name.isEmpty()) {
sql += " AND name LIKE '%" + name + "%'";
}
if (!email.isEmpty()) {
sql += " AND email LIKE '%" + email + "%'";
}
if (!phone.isEmpty()) {
sql += " AND phone LIKE '%" + phone + "%'";
}
if (!address.isEmpty()) {
sql += " AND address LIKE '%" + address + "%'";
}
preparedStatement = connection.prepareStatement(sql);
ResultSet resultSet = preparedStatement.executeQuery();
table.setModel(DbUtils.resultSetToTableModel(resultSet));
} catch (SQLException e) {
e.printStackTrace();
}
}
public void actionPerformed(ActionEvent event) {
if (event.getSource() == addButton) {
addCustomer();
} else if (event.getSource() == updateButton) {
updateCustomer();
} else if (event.getSource() == deleteButton) {
deleteCustomer();
} else if (event.getSource() == searchButton) {
searchCustomer();
}
}
public static void main(String[] args) {
CustomerManagementSystem cms = new CustomerManagementSystem();
cms.setVisible(true);
}
}
```
这个代码示例中,我们使用了Java的GUI类来实现客户管理模块的用户界面,包括文本框、标签、按钮和表格等控件。我们还使用了JDBC来与MySQL数据库进行交互,包括查询、插入、更新和删除等操作。在程序运行时,我们首先需要建立与数据库的连接,并在表格中显示所有客户的信息。当用户点击“添加”按钮时,我们会将用户在文本框中输入的客户信息插入到数据库中,并在表格中添加一行新的记录。当用户点击“更新”按钮时,我们会更新表格中被选中的记录对应的数据库记录。当用户点击“删除”按钮时,我们会删除表格中被选中的记录对应的数据库记录。当用户点击“搜索”按钮时,我们会根据用户在文本框中输入的关键字查询数据库,并在表格中显示查询结果。