我正在尝试在加载框架时在JTable
上显示记录,但是记录没有显示。到目前为止,这是我尝试过的:
public void load()
{
try {
Connection con1;
PreparedStatement insert;
Class.forName("com.mysql.jdbc.Driver");
con1 = DriverManager.getConnection("jdbc:mysql://localhost/javapos","root","");
insert = con1.prepareStatement("SELECT name,status FROM category");
ResultSet Rs = insert.executeQuery();
while(Rs.next()){
DefaultTableModel model = (DefaultTableModel) jTable1.getModel();
String name = Rs.getString("name");
String status = Rs.getString("status");
model.addRow(new Object[]{name,status });
}
jTable1.setModel(model);
} catch (Exception e) {
System.out.println("Failed " + e);
}
}
答案 0 :(得分:1)
我认为您没有以正确的方式获取表的模型。试试:
DefaultTableModel model = (DefaultTableModel) tblMenu.getModel();
选中this链接以获取有关它的更多信息。