-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCRUDApp.java
More file actions
140 lines (117 loc) · 4.67 KB
/
Copy pathCRUDApp.java
File metadata and controls
140 lines (117 loc) · 4.67 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package App_GUI;
import User_DAOHandler.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class CRUDApp extends JFrame {
private JTextField txtId, txtName, txtEmail, txtPhoneno;
private JButton btnAdd, btnUpdate, btnDelete, btnClear;
private UserDAO userDAO;
public CRUDApp() {
userDAO = new UserDAO();
setTitle("JDBC CRUD Application");
setSize(500, 400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new GridLayout(6, 2, 5, 5));
// Labels and text fields
add(new JLabel("ID:"));
txtId = new JTextField();
add(txtId);
add(new JLabel("Name:"));
txtName = new JTextField();
add(txtName);
add(new JLabel("Email:"));
txtEmail = new JTextField();
add(txtEmail);
add(new JLabel("Phoneno:"));
txtPhoneno = new JTextField();
add(txtPhoneno);
// Buttons
btnAdd = new JButton("Add");
btnUpdate = new JButton("Update");
btnDelete = new JButton("Delete");
btnClear = new JButton("Clear");
add(btnAdd);
add(btnUpdate);
add(btnDelete);
add(btnClear);
// Button actions
btnAdd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String name = txtName.getText();
String email = txtEmail.getText();
String phonenoStr = txtPhoneno.getText();
if (name.isEmpty() || email.isEmpty() || phonenoStr.isEmpty()) {
JOptionPane.showMessageDialog(null, "Please fill all fields.");
return;
}
try {
long phoneno = Long.parseLong(phonenoStr);
if (userDAO.insertUser(name, email, phoneno) > 0) {
JOptionPane.showMessageDialog(null, "User added successfully!");
clearFields();
} else {
JOptionPane.showMessageDialog(null, "Error adding user.");
}
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(null, "Please enter a valid phoneno.");
}
}
});
btnUpdate.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
int id = Integer.parseInt(txtId.getText());
String name = txtName.getText();
String email = txtEmail.getText();
String phonenoStr = txtPhoneno.getText();
if (name.isEmpty() || email.isEmpty() || phonenoStr.isEmpty()) {
JOptionPane.showMessageDialog(null, "Please fill all fields.");
return;
}
long phoneno = Long.parseLong(phonenoStr);
if (userDAO.updateUser(id, name, email, phoneno) > 0) {
JOptionPane.showMessageDialog(null, "User updated successfully!");
clearFields();
} else {
JOptionPane.showMessageDialog(null, "Error updating user.");
}
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(null, "Please enter valid values for ID and phoneno.");
}
}
});
btnDelete.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
int id = Integer.parseInt(txtId.getText());
if (userDAO.deleteUser(id) > 0) {
JOptionPane.showMessageDialog(null, "User deleted successfully!");
clearFields();
} else {
JOptionPane.showMessageDialog(null, "Error deleting user.");
}
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(null, "Please enter a valid ID.");
}
}
});
btnClear.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
clearFields();
}
});
}
private void clearFields() {
txtId.setText("");
txtName.setText("");
txtEmail.setText("");
txtPhoneno.setText("");
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
new CRUDApp().setVisible(true);
});
}
}