-
Notifications
You must be signed in to change notification settings - Fork 0
/
GusetView.java
106 lines (91 loc) · 2.73 KB
/
GusetView.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
package DemoJava;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.JScrollPane;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.awt.event.ActionEvent;
import javax.swing.JTable;
public class GusetView extends JFrame {
private JPanel contentPane;
private DefaultTableModel model;
private JTable table;
private void itimModel() 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 GusetView() {
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);
JPanel panel = new JPanel();
contentPane.add(panel, BorderLayout.SOUTH);
JButton btnRefresh = new JButton("Refresh");
btnRefresh.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
itimModel();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
panel.add(btnRefresh);
JButton btnAdd = new JButton("Add");
btnAdd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
AddFrame frame = new AddFrame();
frame.setVisible(true);
try {
itimModel();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
panel.add(btnAdd);
JButton btnExit = new JButton("Exit");
btnExit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
panel.add(btnExit);
}
}