我正在尝试将存储在我的数据库中的数据填充到 JTable 中。我有一条println
语句“将记录添加到数据库”,它打印了 18 次,因此数据库中的行数与打印的语句数相匹配。但是,我的 JTable 中没有显示任何内容,而且我不知道错误在哪里。
代码:
try {
conn = SQLConnect.ConnectDb();
String sql = "SELECT * FROM CriminalRecords WHERE FName = ? and Sname = ?";
pst = conn.prepareStatement(sql);
pst.setString(1, NameSearch.getText());
pst.setString(2, SurnameSearch.getText());
rs = pst.executeQuery();
//display data from db into table hmm
ResultSetMetaData md = rs.getMetaData();
int columnNumber = md.getColumnCount();
Vector columns = new Vector(columnNumber);
// Get column names
//store column names
for(int i=1; i<=columnNumber; i++)
{
columns.add(md.getColumnName(i));
}
Vector data = new Vector();
Vector row;
//store row data
while(rs.next())
{
row = new Vector(columnNumber);
for(int i=1; i<=columnNumber; i++)
{
row.add(rs.getObject(i));
System.out.println("Records added to Table");
}
data.add(row);
}
JTable table1 = new JTable(data, columns);
this.add(table1);
rs.close();
pst.close();
conn.close();
JTable 代码
/---- table1 ----
table1.setModel(new DefaultTableModel(
new Object[][] {
{" ", null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
{null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
{null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
{null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
{null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
{null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
{null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
},
new String[] {
"First Name", "Middle Name", "Surname", "Date of Birth", "Address", "Home Phone", "Business Phone", "Mobile Phone", "Resident Status", "Sex", "Race", "Incident Location", "Zone", "Premise Type", "Date Recorded", "Time Recorded", "Weapons", "Crime Offences"
}
));
table1.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
scrollPane1.setViewportView(table1);