-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearchRecord.java
More file actions
57 lines (54 loc) · 3.34 KB
/
SearchRecord.java
File metadata and controls
57 lines (54 loc) · 3.34 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
package SMS;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.sql.*;
import java.util.Vector;
public class SearchRecord {
public static void searchRecord(JTextField nameField, JTextField fatherField, JTextField motherField, JTextField dobField, JTextField classField, JTextField phoneField, JTextField idField, DefaultTableModel tableModel) {
String id = idField.getText().trim();
String name = nameField.getText().trim();
String fatherName = fatherField.getText().trim();
String motherName = motherField.getText().trim();
String dob = dobField.getText().trim();
String className = classField.getText().trim();
String phoneNumber = phoneField.getText().trim();
try {
Connection connection = DB.getConnection();
StringBuilder queryBuilder = new StringBuilder("SELECT * FROM student WHERE 1=1 ");
if (!id.isEmpty()) queryBuilder.append("AND ID = ? ");
if (!name.isEmpty()) queryBuilder.append("AND Student_Name = ? ");
if (!fatherName.isEmpty()) queryBuilder.append("AND Father_Name = ? ");
if (!motherName.isEmpty()) queryBuilder.append("AND Mother_Name = ? ");
if (!dob.isEmpty()) queryBuilder.append("AND DOB= ? ");
if (!className.isEmpty()) queryBuilder.append("AND Class = ? ");
if (!phoneNumber.isEmpty()) queryBuilder.append("AND Phone_Number = ? ");
PreparedStatement preparedStatement = connection.prepareStatement(queryBuilder.toString());
int parameterIndex = 1;
if (!id.isEmpty()) preparedStatement.setString(parameterIndex++, id);
if (!name.isEmpty()) preparedStatement.setString(parameterIndex++, name);
if (!fatherName.isEmpty()) preparedStatement.setString(parameterIndex++, fatherName);
if (!motherName.isEmpty()) preparedStatement.setString(parameterIndex++, motherName);
if (!dob.isEmpty()) preparedStatement.setString(parameterIndex++,dob);
if (!className.isEmpty()) preparedStatement.setString(parameterIndex++, className);
if (!phoneNumber.isEmpty()) preparedStatement.setString(parameterIndex++, phoneNumber);
ResultSet resultSet = preparedStatement.executeQuery();
tableModel.setRowCount(0);
boolean found = false;
while (resultSet.next()) {
found = true;
Vector<String> row = new Vector<>();
row.add(resultSet.getString("ID"));
row.add(resultSet.getString("Student_Name"));
row.add(resultSet.getString("Father_Name"));
row.add(resultSet.getString("Mother_Name"));
row.add(resultSet.getString("DOB"));
row.add(resultSet.getString("Class"));
row.add(resultSet.getString("Phone_Number"));
tableModel.addRow(row);}
if (!found) { JOptionPane.showMessageDialog(null, "No records found.", "Search Result", JOptionPane.INFORMATION_MESSAGE);}
resultSet.close();
preparedStatement.close();
connection.close();
} catch (SQLException ex) {
JOptionPane.showMessageDialog(null, "Error: " + ex.getMessage(), "Database Error", JOptionPane.ERROR_MESSAGE);
}}}