gpt4 book ai didi

java - JTable 不从 MySQL 填充数据

转载 作者:行者123 更新时间:2023-11-30 23:23:50 29 4
gpt4 key购买 nike

我正在尝试将存储在数据库中的数据填充到 JTable 中。我有一个 println 语句“添加到 db 的记录”,它打印了 18 次,因此 db 中的行数与打印语句的数量相匹配。但是,我的 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);

最佳答案

所以这个解决方案虽然不是最好的,但它确实有效,而且暂时对我有用。

import net.proteanit.sql.DbUtils;
//
//
//
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();

table1.setModel(DbUtils.resultSetToTableModel(rs));

} catch (SQLException ex) {
Logger.getLogger(DatabaseP.class.getName()).log(Level.SEVERE, null, ex);
}

关于java - JTable 不从 MySQL 填充数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14285316/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com