java用swing创建通讯录,有编号、姓名、性别、爱好、学历、电话,实现新增、修改、删除功能
时间: 2023-07-04 12:18:47 浏览: 166
好的,以下是一个使用Java Swing创建通讯录并实现新增、修改、删除功能的示例代码。
首先,你需要创建一个JFrame窗口,包含一个JTable表格和一些JButton按钮。你可以使用以下代码创建一个JFrame窗口:
```
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class AddressBook extends JFrame {
private JTable table;
private JButton addButton;
private JButton editButton;
private JButton deleteButton;
public AddressBook() {
// 设置窗口大小
setSize(600, 400);
// 创建表格和数据模型
String[] columns = {"编号", "姓名", "性别", "爱好", "学历", "电话"};
Object[][] data = {};
DefaultTableModel model = new DefaultTableModel(data, columns);
table = new JTable(model);
// 创建按钮
addButton = new JButton("新增联系人");
editButton = new JButton("修改联系人");
deleteButton = new JButton("删除联系人");
// 将组件添加到窗口中
JPanel buttonPanel = new JPanel();
buttonPanel.add(addButton);
buttonPanel.add(editButton);
buttonPanel.add(deleteButton);
add(new JScrollPane(table), BorderLayout.CENTER);
add(buttonPanel, BorderLayout.SOUTH);
// 添加事件监听器
addButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// 处理新增联系人事件
// ...
}
});
editButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// 处理修改联系人事件
// ...
}
});
deleteButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// 处理删除联系人事件
// ...
}
});
}
public static void main(String[] args) {
// 创建并显示窗口
AddressBook addressBook = new AddressBook();
addressBook.setVisible(true);
}
}
```
接下来,你需要连接MySQL数据库,并实现新增、修改、删除功能。以下是一个示例代码:
```
import java.sql.*;
public class AddressBook {
private JTable table;
private JButton addButton;
private JButton editButton;
private JButton deleteButton;
private Connection connection;
public AddressBook() {
// ...
// 连接数据库
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/address_book",
"root",
"password"
);
} catch (Exception e) {
e.printStackTrace();
}
}
public void addContact(String name, String gender, String hobby, String education, String phone) {
try {
// 创建SQL语句并执行
String sql = "INSERT INTO contacts (name, gender, hobby, education, phone) VALUES (?, ?, ?, ?, ?)";
PreparedStatement statement = connection.prepareStatement(sql);
statement.setString(1, name);
statement.setString(2, gender);
statement.setString(3, hobby);
statement.setString(4, education);
statement.setString(5, phone);
statement.executeUpdate();
statement.close();
// 刷新表格
refreshTable();
} catch (Exception e) {
e.printStackTrace();
}
}
public void editContact(int id, String name, String gender, String hobby, String education, String phone) {
try {
// 创建SQL语句并执行
String sql = "UPDATE contacts SET name=?, gender=?, hobby=?, education=?, phone=? WHERE id=?";
PreparedStatement statement = connection.prepareStatement(sql);
statement.setString(1, name);
statement.setString(2, gender);
statement.setString(3, hobby);
statement.setString(4, education);
statement.setString(5, phone);
statement.setInt(6, id);
statement.executeUpdate();
statement.close();
// 刷新表格
refreshTable();
} catch (Exception e) {
e.printStackTrace();
}
}
public void deleteContact(int id) {
try {
// 创建SQL语句并执行
String sql = "DELETE FROM contacts WHERE id=?";
PreparedStatement statement = connection.prepareStatement(sql);
statement.setInt(1, id);
statement.executeUpdate();
statement.close();
// 刷新表格
refreshTable();
} catch (Exception e) {
e.printStackTrace();
}
}
public void refreshTable() {
try {
// 查询所有联系人
String sql = "SELECT * FROM contacts";
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(sql);
// 更新表格数据模型
DefaultTableModel model = (DefaultTableModel)table.getModel();
model.setRowCount(0);
while (resultSet.next()) {
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
String gender = resultSet.getString("gender");
String hobby = resultSet.getString("hobby");
String education = resultSet.getString("education");
String phone = resultSet.getString("phone");
Object[] data = {id, name, gender, hobby, education, phone};
model.addRow(data);
}
// 关闭连接和语句对象
resultSet.close();
statement.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
```
在上面的代码中,你需要替换连接MySQL数据库时使用的URL、用户名和密码。另外,你需要将“name”、“gender”、“hobby”、“education”和“phone”分别替换为输入的通讯录信息。
希望这些代码能够帮助你创建一个具有编号、姓名、性别、爱好、学历和电话的通讯录,使用Java Swing连接MySQL数据库,并实现新增、修改、删除功能。
阅读全文