forked from SharonMathew4/SOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContactManager.java
More file actions
84 lines (77 loc) · 3.53 KB
/
Copy pathContactManager.java
File metadata and controls
84 lines (77 loc) · 3.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import javax.swing.*;
import java.sql.*;
public class ContactManager {
private final DefaultListModel<ContactModel> contactsListModel = new DefaultListModel<>();
private final Database db;
private final String userEmail;
public ContactManager(Database db, String userEmail) {
this.db = db;
this.userEmail = userEmail;
loadContactsFromDb();
}
public DefaultListModel<ContactModel> getContactsListModel() {
return contactsListModel;
}
public void addContact(String name, String phone) {
String sql = "INSERT INTO contacts(user_id, name, phone) SELECT id, ?, ? FROM users WHERE email=?";
try (Connection conn = db.getConnection(); PreparedStatement ps = conn.prepareStatement(sql)) {
ps.setString(1, name);
ps.setString(2, phone);
ps.setString(3, userEmail);
ps.executeUpdate();
contactsListModel.addElement(new ContactModel(name, phone));
} catch (SQLException e) {
System.err.println("Failed to add contact: " + e.getMessage());
}
}
public void deleteContact(ContactModel contact) {
String sql = "DELETE FROM contacts WHERE user_id = (SELECT id FROM users WHERE email = ?) AND name = ? AND phone = ?";
try (Connection conn = db.getConnection(); PreparedStatement ps = conn.prepareStatement(sql)) {
ps.setString(1, userEmail);
ps.setString(2, contact.getName());
ps.setString(3, contact.getPhoneNumber());
int affected = ps.executeUpdate();
if (affected > 0) {
contactsListModel.removeElement(contact);
}
} catch (SQLException e) {
System.err.println("Failed to delete contact: " + e.getMessage());
e.printStackTrace();
}
}
public void editContact(ContactModel oldContact, String newName, String newPhone) {
String sql = "UPDATE contacts SET name = ?, phone = ? WHERE user_id = (SELECT id FROM users WHERE email = ?) AND name = ? AND phone = ?";
try (Connection conn = db.getConnection(); PreparedStatement ps = conn.prepareStatement(sql)) {
ps.setString(1, newName);
ps.setString(2, newPhone);
ps.setString(3, userEmail);
ps.setString(4, oldContact.getName());
ps.setString(5, oldContact.getPhoneNumber());
int affected = ps.executeUpdate();
if (affected > 0) {
// Update in model
int idx = contactsListModel.indexOf(oldContact);
if (idx != -1) {
contactsListModel.set(idx, new ContactModel(newName, newPhone));
}
}
} catch (SQLException e) {
System.err.println("Failed to edit contact: " + e.getMessage());
e.printStackTrace();
}
}
private void loadContactsFromDb() {
contactsListModel.clear();
String sql = "SELECT c.name, c.phone FROM contacts c JOIN users u ON u.id = c.user_id WHERE u.email = ? ORDER BY c.id DESC";
try (Connection conn = db.getConnection(); PreparedStatement ps = conn.prepareStatement(sql)) {
ps.setString(1, userEmail);
try (ResultSet rs = ps.executeQuery()) {
while (rs.next()) {
contactsListModel.addElement(new ContactModel(rs.getString(1), rs.getString(2)));
}
}
} catch (SQLException e) {
System.err.println("Failed to load contacts: " + e.getMessage());
}
}
}