-
Notifications
You must be signed in to change notification settings - Fork 0
/
Runner.java
145 lines (126 loc) · 3.86 KB
/
Runner.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
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
package DemoJava;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.JTable;
import javax.swing.JScrollPane;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Runner extends JFrame {
private JPanel contentPane;
private static JTable table;
private JPanel panel;
private JButton btnRefresh;
private JButton btnAdd;
private JButton btnDelete;
private JButton btnUpdate;
private static DefaultTableModel model;
private JButton btnExit;
private static void itinModel() throws SQLException {
model = new DefaultTableModel();
model.addColumn("ID");
model.addColumn("Name");
model.addColumn("Class");
model.addColumn("Hometown");
Connection con = ConnectionUtil.getConnection();
String sql = "SELECT * FROM students";
PreparedStatement stmt = con.prepareStatement(sql);
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
String[] row = new String[4];
row[0] = String.valueOf(rs.getInt(1));
row[1] = rs.getString(2);
row[2] = rs.getString(3);
row[3] = rs.getString(4);
model.addRow(row);
}
table.setModel(model);
}
/**
* Create the frame.
*/
public Runner() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JScrollPane scrollPane = new JScrollPane();
contentPane.add(scrollPane, BorderLayout.CENTER);
table = new JTable();
scrollPane.setViewportView(table);
panel = new JPanel();
contentPane.add(panel, BorderLayout.SOUTH);
btnRefresh = new JButton("Refresh");
btnRefresh.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
try {
itinModel();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
panel.add(btnRefresh);
btnAdd = new JButton("Add");
btnAdd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
AddFrame frame = new AddFrame();
frame.setVisible(true);
try {
itinModel();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
panel.add(btnAdd);
btnDelete = new JButton("Delete");
btnDelete.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Connection con = ConnectionUtil.getConnection();
String sql = "DELETE FROM students WHERE id = ?";
try {
PreparedStatement stmt = con.prepareStatement(sql);
String sid = (String) model.getValueAt(table.getSelectedRow(), 0);
int id = Integer.parseInt(sid);
stmt.setInt(1, id);
stmt.executeUpdate();
itinModel();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
panel.add(btnDelete);
btnUpdate = new JButton("Update");
btnUpdate.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String sid = (String) model.getValueAt(table.getSelectedRow(), 0);
int id = Integer.parseInt(sid);
EdittingFrame frame = new EdittingFrame(id);
frame.setVisible(true);
}
});
panel.add(btnUpdate);
btnExit = new JButton("Exit");
btnExit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
panel.add(btnExit);
}
}