-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathadduser.java
103 lines (86 loc) · 3.83 KB
/
adduser.java
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
package net.javaguides.swing;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
public class adduser extends JFrame implements ActionListener {
private JTextField idField, nameField, passwordField;
private JButton addButton, removeButton, displayButton;
private JTextArea resultArea;
public adduser(String userSes) {
super("Add User");
idField = new JTextField(10);
nameField = new JTextField(10);
passwordField = new JTextField(10);
addButton = new JButton("Add User");
addButton.addActionListener(this);
removeButton = new JButton("Remove User");
removeButton.addActionListener(this);
displayButton = new JButton("Display Users");
displayButton.addActionListener(this);
resultArea = new JTextArea(10, 20);
resultArea.setEditable(false);
JPanel inputPanel = new JPanel(new GridLayout(3, 2));
inputPanel.add(new JLabel("ID:"));
inputPanel.add(idField);
inputPanel.add(new JLabel("Name:"));
inputPanel.add(nameField);
inputPanel.add(new JLabel("Password:"));
inputPanel.add(passwordField);
JPanel buttonPanel = new JPanel(new GridLayout(1, 3));
buttonPanel.add(addButton);
buttonPanel.add(removeButton);
buttonPanel.add(displayButton);
JPanel outputPanel = new JPanel();
outputPanel.add(new JScrollPane(resultArea));
Container contentPane = getContentPane();
contentPane.add(inputPanel, BorderLayout.NORTH);
contentPane.add(buttonPanel, BorderLayout.CENTER);
contentPane.add(outputPanel, BorderLayout.SOUTH);
setSize(400, 300);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
try {
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/swing_demo", "root", "Aben5099");
PreparedStatement statement;
ResultSet resultSet;
String query;
if (e.getSource() == addButton) {
query = "INSERT INTO student (id, name, password) VALUES (?, ?, ?)";
statement = connection.prepareStatement(query);
statement.setInt(1, Integer.parseInt(idField.getText()));
statement.setString(2, nameField.getText());
statement.setString(3, passwordField.getText());
statement.executeUpdate();
resultArea.setText("User added.");
} else if (e.getSource() == removeButton) {
query = "DELETE FROM student WHERE id=?";
statement = connection.prepareStatement(query);
statement.setInt(1, Integer.parseInt(idField.getText()));
statement.executeUpdate();
resultArea.setText("User removed.");
} else if (e.getSource() == displayButton) {
query = "SELECT id, name, password FROM student";
statement = connection.prepareStatement(query);
resultSet = statement.executeQuery();
StringBuilder resultText = new StringBuilder();
while (resultSet.next()) {
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
String password = resultSet.getString("password");
resultText.append(id).append("\t").append(name).append("\t").append(password).append("\n");
}
resultArea.setText(resultText.toString());
}
connection.close();
} catch (SQLException ex) {
ex.printStackTrace();
resultArea.setText("Error: " + ex.getMessage());
}
}
public static void main(String[] args) {
String userSes = null;
new adduser(userSes);
}
}