java 实现通讯录管理系统代码
时间: 2023-07-03 20:31:43 浏览: 114
java通讯录系统代码
好的,以下是一个基于Java Swing和JDBC的通讯录管理系统的代码示例,仅供参考:
```java
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
public class AddressBook extends JFrame {
private JPanel contentPane;
private JTextField textFieldName;
private JTextField textFieldPhone;
private JTextField textFieldEmail;
private JTextArea textArea;
private JButton btnAdd;
private JButton btnDelete;
private JButton btnUpdate;
private JButton btnSearch;
private Connection conn;
private Statement stmt;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
AddressBook frame = new AddressBook();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public AddressBook() {
// Initialize database connection
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/addressbook", "root", "password");
stmt = conn.createStatement();
} catch (SQLException | ClassNotFoundException e) {
e.printStackTrace();
}
setTitle("Address Book");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblName = new JLabel("Name:");
lblName.setBounds(23, 20, 61, 16);
contentPane.add(lblName);
textFieldName = new JTextField();
textFieldName.setBounds(88, 15, 130, 26);
contentPane.add(textFieldName);
textFieldName.setColumns(10);
JLabel lblPhone = new JLabel("Phone:");
lblPhone.setBounds(23, 52, 61, 16);
contentPane.add(lblPhone);
textFieldPhone = new JTextField();
textFieldPhone.setBounds(88, 47, 130, 26);
contentPane.add(textFieldPhone);
textFieldPhone.setColumns(10);
JLabel lblEmail = new JLabel("Email:");
lblEmail.setBounds(23, 84, 61, 16);
contentPane.add(lblEmail);
textFieldEmail = new JTextField();
textFieldEmail.setBounds(88, 79, 130, 26);
contentPane.add(textFieldEmail);
textFieldEmail.setColumns(10);
btnAdd = new JButton("Add");
btnAdd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
addContact();
}
});
btnAdd.setBounds(23, 127, 75, 29);
contentPane.add(btnAdd);
btnDelete = new JButton("Delete");
btnDelete.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
deleteContact();
}
});
btnDelete.setBounds(110, 127, 75, 29);
contentPane.add(btnDelete);
btnUpdate = new JButton("Update");
btnUpdate.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
updateContact();
}
});
btnUpdate.setBounds(197, 127, 75, 29);
contentPane.add(btnUpdate);
btnSearch = new JButton("Search");
btnSearch.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
searchContact();
}
});
btnSearch.setBounds(284, 127, 75, 29);
contentPane.add(btnSearch);
JLabel lblContacts = new JLabel("Contacts:");
lblContacts.setBounds(23, 168, 61, 16);
contentPane.add(lblContacts);
textArea = new JTextArea();
JScrollPane scrollPane = new JScrollPane(textArea);
scrollPane.setBounds(23, 196, 414, 66);
contentPane.add(scrollPane);
}
private void addContact() {
try {
String name = textFieldName.getText();
String phone = textFieldPhone.getText();
String email = textFieldEmail.getText();
String sql = "INSERT INTO contacts (name, phone, email) VALUES ('" + name + "', '" + phone + "', '" + email + "')";
stmt.executeUpdate(sql);
JOptionPane.showMessageDialog(null, "Contact added successfully.");
} catch (SQLException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "Failed to add contact.");
}
}
private void deleteContact() {
try {
String name = textFieldName.getText();
String sql = "DELETE FROM contacts WHERE name='" + name + "'";
stmt.executeUpdate(sql);
JOptionPane.showMessageDialog(null, "Contact deleted successfully.");
} catch (SQLException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "Failed to delete contact.");
}
}
private void updateContact() {
try {
String name = textFieldName.getText();
String phone = textFieldPhone.getText();
String email = textFieldEmail.getText();
String sql = "UPDATE contacts SET phone='" + phone + "', email='" + email + "' WHERE name='" + name + "'";
stmt.executeUpdate(sql);
JOptionPane.showMessageDialog(null, "Contact updated successfully.");
} catch (SQLException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "Failed to update contact.");
}
}
private void searchContact() {
try {
String name = textFieldName.getText();
String sql = "SELECT * FROM contacts WHERE name='" + name + "'";
ResultSet rs = stmt.executeQuery(sql);
if (rs.next()) {
String phone = rs.getString("phone");
String email = rs.getString("email");
textArea.setText("Name: " + name + "\nPhone: " + phone + "\nEmail: " + email);
} else {
JOptionPane.showMessageDialog(null, "Contact not found.");
}
} catch (SQLException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "Failed to search for contact.");
}
}
}
```
以上代码实现了一个简单的通讯录管理系统,包含添加、删除、修改和查询联系人的功能。注意,该示例代码仅供参考,实际应用中还需要考虑安全性、性能优化等因素。
阅读全文