处理ResultSet中的每一行数据并创建一个Vector,然后使用此方法将数据插入表模型中。您正在创建新的表模型并将其设置在表上,带有数据的旧模型将丢失。
在以下要求后发表评论:
这是做到这一点的一种方法。
Vector<Vector<String>> data=new Vector<>();//Fill this Vector above with the initial dataVector<String> columns=new Vector<String>();//Fill this with column namesDefaultTableModel tableModel=new DefaultTableModel(data, columns);JTable table=new JTable(tableModel);//Display the table as you like... //Query the database and get the ResultSet (let's call it rs)while(rs.next){ Vector<String> newRow=new Vector<>(); //Get the data from the resultset and fill this new row tableModel.addRow(newRow);}//while closing


