用Java实现会员管理系统并实现窗体功能
时间: 2024-02-01 13:13:56 浏览: 77
首先,需要设计数据库表结构,例如设计一个名为“member”的表,包含字段:
- id:会员编号,主键,自增长
- name:会员姓名,字符串类型
- gender:会员性别,字符串类型
- age:会员年龄,整型
- phone:会员电话,字符串类型
- address:会员地址,字符串类型
- email:会员邮箱,字符串类型
接下来,可以使用Java Swing创建窗体界面,通过JDBC连接数据库实现对数据的增删改查操作。
以下是一个简单的会员管理系统窗体代码示例:
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
public class MemberManagementSystem extends JFrame implements ActionListener {
private JLabel labelName, labelGender, labelAge, labelPhone, labelAddress, labelEmail, labelInfo;
private JTextField textFieldName, textFieldGender, textFieldAge, textFieldPhone, textFieldAddress, textFieldEmail;
private JButton buttonAdd, buttonDelete, buttonUpdate, buttonSearch, buttonClear;
private JPanel panelInput, panelButton;
private JTextArea textArea;
private Connection conn;
private Statement stmt;
public MemberManagementSystem() {
super("会员管理系统");
// 创建组件
labelName = new JLabel("姓名:");
labelGender = new JLabel("性别:");
labelAge = new JLabel("年龄:");
labelPhone = new JLabel("电话:");
labelAddress = new JLabel("地址:");
labelEmail = new JLabel("邮箱:");
labelInfo = new JLabel("会员信息:");
textFieldName = new JTextField(10);
textFieldGender = new JTextField(10);
textFieldAge = new JTextField(10);
textFieldPhone = new JTextField(10);
textFieldAddress = new JTextField(10);
textFieldEmail = new JTextField(10);
buttonAdd = new JButton("添加");
buttonDelete = new JButton("删除");
buttonUpdate = new JButton("更新");
buttonSearch = new JButton("查询");
buttonClear = new JButton("清空");
textArea = new JTextArea(10, 30);
textArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(textArea);
panelInput = new JPanel(new GridLayout(6, 2));
panelInput.add(labelName);
panelInput.add(textFieldName);
panelInput.add(labelGender);
panelInput.add(textFieldGender);
panelInput.add(labelAge);
panelInput.add(textFieldAge);
panelInput.add(labelPhone);
panelInput.add(textFieldPhone);
panelInput.add(labelAddress);
panelInput.add(textFieldAddress);
panelInput.add(labelEmail);
panelInput.add(textFieldEmail);
panelButton = new JPanel(new GridLayout(2, 3));
panelButton.add(buttonAdd);
panelButton.add(buttonDelete);
panelButton.add(buttonUpdate);
panelButton.add(buttonSearch);
panelButton.add(buttonClear);
// 添加组件
Container container = getContentPane();
container.setLayout(new BorderLayout());
container.add(panelInput, BorderLayout.NORTH);
container.add(panelButton, BorderLayout.CENTER);
container.add(labelInfo, BorderLayout.WEST);
container.add(scrollPane, BorderLayout.SOUTH);
// 添加事件处理
buttonAdd.addActionListener(this);
buttonDelete.addActionListener(this);
buttonUpdate.addActionListener(this);
buttonSearch.addActionListener(this);
buttonClear.addActionListener(this);
// 连接数据库
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "");
stmt = conn.createStatement();
} catch (Exception e) {
e.printStackTrace();
System.exit(0);
}
// 设置窗体大小并可见化
setSize(400, 400);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == buttonAdd) {
int result = JOptionPane.showConfirmDialog(this, "确认添加该会员信息吗?", "添加会员", JOptionPane.YES_NO_OPTION);
if (result == JOptionPane.YES_OPTION) {
String name = textFieldName.getText().trim();
String gender = textFieldGender.getText().trim();
String age = textFieldAge.getText().trim();
String phone = textFieldPhone.getText().trim();
String address = textFieldAddress.getText().trim();
String email = textFieldEmail.getText().trim();
String sql = "INSERT INTO member(name, gender, age, phone, address, email) VALUES('" + name + "', '" + gender + "', " + age + ", '" + phone + "', '" + address + "', '" + email + "')";
try {
stmt.executeUpdate(sql);
textArea.setText("添加会员成功");
} catch (SQLException ex) {
ex.printStackTrace();
textArea.setText("添加会员失败");
}
}
} else if (e.getSource() == buttonDelete) {
int result = JOptionPane.showConfirmDialog(this, "确认删除该会员信息吗?", "删除会员", JOptionPane.YES_NO_OPTION);
if (result == JOptionPane.YES_OPTION) {
String name = textFieldName.getText().trim();
String sql = "DELETE FROM member WHERE name='" + name + "'";
try {
stmt.executeUpdate(sql);
textArea.setText("删除会员成功");
} catch (SQLException ex) {
ex.printStackTrace();
textArea.setText("删除会员失败");
}
}
} else if (e.getSource() == buttonUpdate) {
int result = JOptionPane.showConfirmDialog(this, "确认更新该会员信息吗?", "更新会员", JOptionPane.YES_NO_OPTION);
if (result == JOptionPane.YES_OPTION) {
String name = textFieldName.getText().trim();
String gender = textFieldGender.getText().trim();
String age = textFieldAge.getText().trim();
String phone = textFieldPhone.getText().trim();
String address = textFieldAddress.getText().trim();
String email = textFieldEmail.getText().trim();
String sql = "UPDATE member SET gender='" + gender + "', age=" + age + ", phone='" + phone + "', address='" + address + "', email='" + email + "' WHERE name='" + name + "'";
try {
stmt.executeUpdate(sql);
textArea.setText("更新会员成功");
} catch (SQLException ex) {
ex.printStackTrace();
textArea.setText("更新会员失败");
}
}
} else if (e.getSource() == buttonSearch) {
String name = textFieldName.getText().trim();
String sql = "SELECT * FROM member WHERE name='" + name + "'";
try {
ResultSet rs = stmt.executeQuery(sql);
if (rs.next()) {
String gender = rs.getString("gender");
int age = rs.getInt("age");
String phone = rs.getString("phone");
String address = rs.getString("address");
String email = rs.getString("email");
textArea.setText("姓名:" + name + "\n性别:" + gender + "\n年龄:" + age + "\n电话:" + phone + "\n地址:" + address + "\n邮箱:" + email);
} else {
textArea.setText("查无此人");
}
} catch (SQLException ex) {
ex.printStackTrace();
textArea.setText("查询会员失败");
}
} else if (e.getSource() == buttonClear) {
textFieldName.setText("");
textFieldGender.setText("");
textFieldAge.setText("");
textFieldPhone.setText("");
textFieldAddress.setText("");
textFieldEmail.setText("");
textArea.setText("");
}
}
public static void main(String[] args) {
new MemberManagementSystem();
}
}
```
需要注意的是,这仅仅是一个简单的示例,实际的系统需要根据需求进行更加详细的设计和实现。同时,还需要注意数据库的安全性和数据的完整性,例如对输入数据进行验证和过滤,防止SQL注入等攻击。
阅读全文