-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStudent_data.java
More file actions
245 lines (212 loc) · 11.3 KB
/
Copy pathStudent_data.java
File metadata and controls
245 lines (212 loc) · 11.3 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.ResultSet;
public class Student_data extends javax.swing.JFrame {
/**
* Creates new form Student_data
*/
public Student_data() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
insert_btn = new javax.swing.JButton();
view_btn = new javax.swing.JButton();
back_btn = new javax.swing.JButton();
search_btn = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
insert_btn.setBackground(new java.awt.Color(102, 153, 255));
insert_btn.setFont(new java.awt.Font("Segoe UI", 3, 12)); // NOI18N
insert_btn.setForeground(new java.awt.Color(255, 255, 255));
insert_btn.setText("insert");
insert_btn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
insert_btnActionPerformed(evt);
}
});
view_btn.setBackground(new java.awt.Color(51, 153, 0));
view_btn.setFont(new java.awt.Font("Segoe UI", 3, 12)); // NOI18N
view_btn.setForeground(new java.awt.Color(255, 255, 255));
view_btn.setText("view");
view_btn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
view_btnActionPerformed(evt);
}
});
back_btn.setBackground(new java.awt.Color(153, 153, 255));
back_btn.setFont(new java.awt.Font("Segoe UI", 3, 12)); // NOI18N
back_btn.setForeground(new java.awt.Color(255, 255, 255));
back_btn.setText("<");
back_btn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
back_btnActionPerformed(evt);
}
});
search_btn.setText("Search");
search_btn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
search_btnActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(65, 65, 65)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(back_btn, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(insert_btn, javax.swing.GroupLayout.DEFAULT_SIZE, 146, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(view_btn, javax.swing.GroupLayout.DEFAULT_SIZE, 146, Short.MAX_VALUE)
.addComponent(search_btn, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap(37, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(58, 58, 58)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(view_btn)
.addComponent(insert_btn))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(back_btn)
.addComponent(search_btn))
.addContainerGap(192, Short.MAX_VALUE))
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void insert_btnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_insert_btnActionPerformed
// JDBC connection parameters
String id = JOptionPane.showInputDialog(this, "Enter Student ID:");
String name = JOptionPane.showInputDialog(this, "Enter Student Name:");
String email = JOptionPane.showInputDialog(this, "Enter Student Email:");
// Perform insertion logic into the database
String url = "jdbc:mysql://localhost:3306/login_form";
String user = "root";
String pass = "";
try (Connection connection = DriverManager.getConnection(url, user, pass)) {
String query = "INSERT INTO student_table (id, name, email) VALUES (?, ?, ?)";
try (PreparedStatement preparedStatement = connection.prepareStatement(query)) {
preparedStatement.setString(1, id);
preparedStatement.setString(2, name);
preparedStatement.setString(3, email);
int rowsAffected = preparedStatement.executeUpdate();
if (rowsAffected > 0) {
JOptionPane.showMessageDialog(this, "Data inserted successfully!");
} else {
JOptionPane.showMessageDialog(this, "Failed to insert data!");
}
}
} catch (SQLException ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(this, "Error: " + ex.getMessage());
}
}//GEN-LAST:event_insert_btnActionPerformed
private void view_btnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_view_btnActionPerformed
student_view_data svd = new student_view_data();
svd.setVisible(true);
// Close or hide the registration form
this.dispose(); }//GEN-LAST:event_view_btnActionPerformed
private void back_btnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_back_btnActionPerformed
welcome ad = new welcome();
ad.setVisible(true);
// Close or hide the registration form
this.dispose(); // TODO add your handling code here:
}//GEN-LAST:event_back_btnActionPerformed
private void search_btnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_search_btnActionPerformed
String searchTerm = JOptionPane.showInputDialog(this, "Enter search term:");
// JDBC connection parameters
String url = "jdbc:mysql://localhost:3306/login_form";
String user = "root";
String pass = "";
try (Connection connection = DriverManager.getConnection(url, user, pass)) {
// SQL query to select data from the student_table based on the search term
String query = "SELECT * FROM student_table WHERE name LIKE ? OR email LIKE ?";
try (PreparedStatement preparedStatement = connection.prepareStatement(query)) {
// Set the parameters in the prepared statement
preparedStatement.setString(1, "%" + searchTerm + "%");
preparedStatement.setString(2, "%" + searchTerm + "%");
try (ResultSet resultSet = preparedStatement.executeQuery()) {
// Process the result set and display the data in a JOptionPane dialog
StringBuilder resultMessage = new StringBuilder("Search Results:\n");
while (resultSet.next()) {
String studentId = resultSet.getString("id");
String studentName = resultSet.getString("name");
String studentEmail = resultSet.getString("email");
// Append the retrieved data to the message
resultMessage.append("Student ID: ").append(studentId)
.append(", Name: ").append(studentName)
.append(", Email: ").append(studentEmail).append("\n");
}
// Display the message in a JOptionPane dialog
JOptionPane.showMessageDialog(this, resultMessage.toString());
}
}
} catch (SQLException ex) {
// Handle exceptions
ex.printStackTrace();
}
}//GEN-LAST:event_search_btnActionPerformed
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Student_data.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Student_data.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Student_data.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Student_data.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Student_data.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Student_data().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton back_btn;
private javax.swing.JButton insert_btn;
private javax.swing.JButton search_btn;
private javax.swing.JButton view_btn;
// End of variables declaration//GEN-END:variables
}