При нажатии на кнопку, он не заменяет таблицу, а внизу приписывает новую.
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
| import java.awt.*;
import java.sql.*;
import java.util.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
import javax.swing.table.*;
public class Arsti2 {
JFrame main = new JFrame("Ārst");
JPanel tP = new JPanel();
JPanel bP = new JPanel();
JButton one = new JButton("Test");
JTable table = new JTable();
DefaultTableModel model;
Vector columnNames = new Vector();
Vector data = new Vector();
Arsti2() {
main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
main.setSize(840,300);
try {
reloadData();
model = new DefaultTableModel(data,columnNames);
table.setModel(model);
model.fireTableDataChanged();
tP.add(new JScrollPane(table));
bP.add(one);
main.add(tP,BorderLayout.NORTH);
main.add(bP,BorderLayout.SOUTH);
} catch(Exception e) {
e.printStackTrace();
}
main.setVisible(true);
one.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evnt1) {
try {
reloadData();
model.fireTableDataChanged();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
});
}
private void reloadData() throws ClassNotFoundException, SQLException {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String Base = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)}; DBQ=SL.mdb";
Connection con = DriverManager.getConnection(Base,"","");
Statement st = con.createStatement();
ResultSet res = st.executeQuery("SELECT * FROM Arsti");
ResultSetMetaData rsmd = res.getMetaData();
int column = rsmd.getColumnCount();
columnNames.addElement("ID");
columnNames.addElement("Vards");
columnNames.addElement("Uzvards");
columnNames.addElement("Dzimums");
columnNames.addElement("Personas kods");
columnNames.addElement("Telefona numurs");
columnNames.addElement("Nodalas ID");
columnNames.addElement("Amata ID");
while(res.next()) {
Vector row = new Vector(column);
for(int i=1; i<=column; i++) {
row.addElement(res.getObject(i));
}
data.addElement(row);
}
}
public static void main(String[] args) {
new Arsti2();
}
} |
|