Skip to content

Commit 9158908

Browse files
added all source files
1 parent 5783de2 commit 9158908

File tree

4 files changed

+260
-0
lines changed

4 files changed

+260
-0
lines changed

src/AppGUI.java

Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
import javax.swing.*;
2+
3+
import java.awt.event.*;
4+
import java.sql.*;
5+
6+
public class AppGUI extends JFrame implements ActionListener {
7+
private final JLabel studentIdLabel, firstNameLabel, lastNameLabel, majorLabel, phoneLabel, gpaLabel, dobLabel;
8+
private final JTextField studentIdField, firstNameField, lastNameField, majorField, phoneField, gpaField, dobField;
9+
private final JButton addButton, displayButton, sortButton, searchButton, modifyButton;
10+
11+
private Statement stmt;
12+
13+
public AppGUI() {
14+
JFrame frame = new JFrame("Student Database");
15+
JPanel panel = new JPanel();
16+
17+
// Initialize labels
18+
studentIdLabel = new JLabel("Student ID:");
19+
firstNameLabel = new JLabel("First Name:");
20+
lastNameLabel = new JLabel("Last Name:");
21+
majorLabel = new JLabel("Major:");
22+
phoneLabel = new JLabel("Phone:");
23+
gpaLabel = new JLabel("GPA:");
24+
dobLabel = new JLabel("Date of Birth (yyyy-mm-dd):");
25+
26+
// Initialize text fields
27+
studentIdField = new JTextField(10);
28+
firstNameField = new JTextField(10);
29+
lastNameField = new JTextField(10);
30+
majorField = new JTextField(10);
31+
phoneField = new JTextField(10);
32+
gpaField = new JTextField(10);
33+
dobField = new JTextField(10);
34+
35+
// Initialize buttons
36+
addButton = new JButton("Add");
37+
displayButton = new JButton("Display");
38+
sortButton = new JButton("Sort");
39+
searchButton = new JButton("Search");
40+
modifyButton = new JButton("Modify");
41+
42+
// Add action listeners to buttons
43+
addButton.addActionListener(this);
44+
displayButton.addActionListener(this);
45+
sortButton.addActionListener(this);
46+
searchButton.addActionListener(this);
47+
modifyButton.addActionListener(this);
48+
49+
// Add components to panel
50+
panel.add(studentIdLabel);
51+
panel.add(studentIdField);
52+
panel.add(firstNameLabel);
53+
panel.add(firstNameField);
54+
panel.add(lastNameLabel);
55+
panel.add(lastNameField);
56+
panel.add(majorLabel);
57+
panel.add(majorField);
58+
panel.add(phoneLabel);
59+
panel.add(phoneField);
60+
panel.add(gpaLabel);
61+
panel.add(gpaField);
62+
panel.add(dobLabel);
63+
panel.add(dobField);
64+
panel.add(addButton);
65+
panel.add(displayButton);
66+
panel.add(sortButton);
67+
panel.add(searchButton);
68+
panel.add(modifyButton);
69+
70+
// Add panel to frame
71+
frame.add(panel);
72+
frame.pack();
73+
frame.setVisible(true);
74+
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
75+
}
76+
77+
78+
@Override
79+
public void actionPerformed(ActionEvent e) {
80+
dbConnect db = new dbConnect();
81+
Connection conn;
82+
try {
83+
conn = db.getConnection();
84+
} catch (SQLException | ClassNotFoundException ex) {
85+
throw new RuntimeException(ex);
86+
}
87+
try {
88+
stmt = conn.createStatement();
89+
} catch (SQLException e1) {
90+
e1.printStackTrace();
91+
}
92+
Table tb = new Table();
93+
if (e.getSource() == addButton) {
94+
// Insert new student into database
95+
String sql = "INSERT INTO sdata VALUES('" + studentIdField.getText() + "', '"
96+
+ firstNameField.getText() + "', '" + lastNameField.getText() + "', '" + majorField.getText()
97+
+ "', '" + phoneField.getText() + "', '" + gpaField.getText() + "', '" + dobField.getText() + "')";
98+
try {
99+
stmt.executeUpdate(sql);
100+
JOptionPane.showMessageDialog(null, "Student added successfully.");
101+
} catch (SQLException ex) {
102+
ex.printStackTrace();
103+
}
104+
} else if (e.getSource() == displayButton) {
105+
// Display all sdata in database
106+
String sql = "SELECT * FROM sdata";
107+
108+
try {
109+
ResultSet rs = stmt.executeQuery(sql);
110+
111+
// Create table to display student data
112+
JTable table = new JTable(tb.buildTableModel(rs));
113+
JOptionPane.showMessageDialog(null, new JScrollPane(table));
114+
} catch (SQLException ex) {
115+
ex.printStackTrace();
116+
}
117+
} else if (e.getSource() == sortButton) {
118+
// Sort sdata by selected column
119+
String[] options = {"First Name", "Last Name", "Major"};
120+
int choice = JOptionPane.showOptionDialog(null, "Sort by:", "Sort", JOptionPane.DEFAULT_OPTION,
121+
JOptionPane.PLAIN_MESSAGE, null, options, options[0]);
122+
123+
String sql = "";
124+
switch (choice) {
125+
case 0 -> sql = "SELECT * FROM sdata ORDER BY first_name";
126+
case 1 -> sql = "SELECT * FROM sdata ORDER BY last_name";
127+
case 2 -> sql = "SELECT * FROM sdata ORDER BY major";
128+
default -> {
129+
}
130+
}
131+
try {
132+
ResultSet rs = stmt.executeQuery(sql);
133+
134+
// Create table to display student data
135+
JTable table = new JTable(tb.buildTableModel(rs));
136+
JOptionPane.showMessageDialog(null, new JScrollPane(table));
137+
} catch (SQLException ex) {
138+
ex.printStackTrace();
139+
}
140+
} else if (e.getSource() == searchButton) {
141+
// Search for student by selected column
142+
String[] options = {"Student ID", "Last Name", "Major"};
143+
int choice = JOptionPane.showOptionDialog(null, "Search by:", "Search", JOptionPane.DEFAULT_OPTION,
144+
JOptionPane.PLAIN_MESSAGE, null, options, options[0]);
145+
146+
String column = "";
147+
switch (choice) {
148+
case 0 -> column = "student_id";
149+
case 1 -> column = "last_name";
150+
case 2 -> column = "major";
151+
default -> {
152+
}
153+
}
154+
155+
String searchTerm = JOptionPane.showInputDialog("Enter search term:");
156+
157+
String sql = "SELECT * FROM sdata WHERE " + column + " LIKE '%" + searchTerm + "%'";
158+
159+
try {
160+
ResultSet rs = stmt.executeQuery(sql);
161+
162+
// Create table to display student data
163+
JTable table = new JTable(tb.buildTableModel(rs));
164+
JOptionPane.showMessageDialog(null, new JScrollPane(table));
165+
} catch (SQLException ex) {
166+
ex.printStackTrace();
167+
}
168+
} else if (e.getSource() == modifyButton) {
169+
// Modify selected student's data
170+
String studentId = JOptionPane.showInputDialog("Enter student ID:");
171+
172+
String sql = "SELECT * FROM sdata WHERE student_id = '" + studentId + "'";
173+
174+
try {
175+
ResultSet rs = stmt.executeQuery(sql);
176+
177+
if (rs.next()) {
178+
String[] options = {"First Name", "Last Name", "Major", "Phone", "GPA", "Date of Birth"};
179+
int choice = JOptionPane.showOptionDialog(null, "Select field to modify:", "Modify",
180+
JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null, options, options[0]);
181+
182+
String column = "";
183+
switch (choice) {
184+
case 0 -> column = "first_name";
185+
case 1 -> column = "last_name";
186+
case 2 -> column = "major";
187+
case 3 -> column = "phone";
188+
case 4 -> column = "gpa";
189+
case 5 -> column = "date_of_birth";
190+
default -> {
191+
}
192+
}
193+
String newValue = JOptionPane.showInputDialog("Enter new value:");
194+
195+
sql = "UPDATE sdata SET " + column + " = '" + newValue + "' WHERE student_id = '" + studentId + "'";
196+
197+
stmt.executeUpdate(sql);
198+
JOptionPane.showMessageDialog(null, "Student data updated successfully.");
199+
} else {
200+
JOptionPane.showMessageDialog(null, "Student not found.");
201+
}
202+
} catch (SQLException ex) {
203+
ex.printStackTrace();
204+
}
205+
}
206+
}
207+
208+
}

src/Main.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import javax.swing.*;
2+
3+
public class Main {
4+
public static void main(String[] args) {
5+
AppGUI window = new AppGUI();
6+
window.pack();
7+
window.setVisible(true);
8+
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
9+
}
10+
}

src/Table.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import java.sql.*;
2+
import java.util.Vector;
3+
4+
import javax.swing.table.DefaultTableModel;
5+
6+
public class Table {
7+
DefaultTableModel buildTableModel(ResultSet rs) throws SQLException {
8+
ResultSetMetaData metaData = rs.getMetaData();
9+
10+
// Get column names
11+
int columnCount = metaData.getColumnCount();
12+
Vector<String> columnNames = new Vector<String>();
13+
for (int i = 1; i <= columnCount; i++) {
14+
columnNames.add(metaData.getColumnName(i));
15+
}
16+
17+
// Get row data
18+
Vector<Vector<Object>> rowData = new Vector<Vector<Object>>();
19+
while (rs.next()) {
20+
Vector<Object> row = new Vector<Object>();
21+
for (int i = 1; i <= columnCount; i++) {
22+
row.add(rs.getObject(i));
23+
}
24+
rowData.add(row);
25+
}
26+
27+
return new DefaultTableModel(rowData, columnNames);
28+
}
29+
}

src/dbConnect.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import java.sql.*;
2+
3+
public class dbConnect {
4+
private static Connection mycon=null;
5+
6+
public Connection getConnection() throws ClassNotFoundException, SQLException {
7+
String db="studata", user = "root", pass = "1236";
8+
String url = "jdbc:mysql://localhost:3306/"+db;
9+
Class.forName("com.mysql.cj.jdbc.Driver");
10+
Connection conn = DriverManager.getConnection(url,user,pass);
11+
return conn;
12+
}
13+
}

0 commit comments

Comments
 (0)