gpt4 book ai didi

java - 从多个 sqlite 表填充 Jtable

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

我有三个数据库表,其各自的列如下:

学生信息:(学生ID,姓名)余额:(学生号,余额)金额:(学生ID,金额)

我想直接从数据库在 JTable 上显示学生 ID、姓名、余额、金额。我从表中检索这些信息并将其存储在 Map 中,然后再显示在 JTable 上。但这些值在我的 JTable 上不断重复,正如您在所附的输出图片中看到的那样。这是我的代码:

string ="select studentinfo.studentid,name,balance,amount from studentinfo     inner join Balance on studentinfo.studentid = Balance.studentid inner join Amount on studentinfo.studentid = Amount.studentid"        
while(rs.next()){
for(i = 0, k=1,p=2,m=3; i < 1 && k < 2 && p<3 && m<4; i++,k++,p++,m++) //JTable colums
{
for(j = 0; j < rs.getRow(); j++) // Jtable rows
{
id = rs.getString("studentid");
name = rs.getString("name");
amt = rs.getDouble("amount");
bal = rs.getDouble("balance");

map1.put(id, name);
map2.put(id, amt);
map3.put(id, bal);
}
j-- // change the value of j=0 to always start from the first row of a new column in the JTable;


Set set = map1.entrySet();
Iterator iterator = set.iterator();
while(iterator.hasNext()) {
Map.Entry ent = (Map.Entry)iterator.next();
table.setValueAt(ent.getKey(), j, k);
table.setValueAt(ent.getValue(), j, i);
}

Set set1 = map2.entrySet();
Iterator iterator1 = set1.iterator();
while(iterator1.hasNext()) {
Map.Entry ent1 = (Map.Entry)iterator1.next();
table.setValueAt(ent1.getValue(), j, p);
}

Set set2 = map2.entrySet();
Iterator iterator2 = set2.iterator();
while(iterator2.hasNext()) {
Map.Entry ent2 = (Map.Entry)iterator2.next();
table.setValueAt(ent2.getValue(), j, m);
}
}

}

输出:

Output table

最佳答案

问题是因为您一次又一次地迭代同一行,我认为您使事情变得复杂,如果您将值设置为 table ,代码会简单得多,如下所示:

int rowCount=0;
while(rs.next()){
table.setValueAt(rs.getString("studentid"),rowCount, 0);
table.setValueAt(rs.getString("name"), rowCount, 1);
table.setValueAt(rs.getDouble("amount"), rowCount, 2);
table.setValueAt(rs.getDouble("balance"), rowCount, 3);

rowCount++;//increment the row count for each row fetched
}

关于java - 从多个 sqlite 表填充 Jtable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42886269/

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