我从数据库表中使用按钮选择数据集,但是每次单击按钮时,它会加载我已经拥有的相同数据集:
public void actionPerformed(ActionEvent e) {
java.sql.Connection con;
try {
con = DriverManager.getConnection ("jdbc:mysql://localhost:3307/lessons","root","");
String query =" SELECT * FROM lessons";
PreparedStatement statement = con.prepareStatement(query);
ResultSet rs = statement.executeQuery(query);
while(rs.next())
{
int lesson = rs.getInt("lessons");
String name= rs.getString("names");
String number= rs.getString("numbers");
model.addRow(new Object[]{lessons, names, numbers});
table.setVisible(true);
}
我只想打印唯一的数据集。
谢谢大家。
答案 0 :(得分:2)
您必须先删除所有可用的行,然后才能将数据加载到表中
DefaultTableModel model = (DefaultTableModel)your_table.getModel();
...
model.setRowCount(0); // add this line
while(rs.next()) {
int lesson = rs.getInt("lessons");
String name= rs.getString("names");
String number= rs.getString("numbers");
....
model.addRow(new Object[]{lessons, names, numbers});
}