Java 从多个sqlite表填充Jtable
我有三个数据库表,它们各自的列如下:Java 从多个sqlite表填充Jtable,java,sqlite,Java,Sqlite,我有三个数据库表,它们各自的列如下: 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.setValu
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
}
学生信息:(学生ID,姓名)
平衡:(学生ID,平衡)
金额:(学生ID,金额)
我想直接从数据库在JTable上显示studentid、姓名、余额和金额。我从表中检索这些信息,并将其存储在地图中,然后显示在我的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);
}
}
string=“选择studentinfo.studentid、名称、余额、studentinfo上的studentinfo内部联接余额中的金额studentinfo.studentid=balance.studentid上的studentinfo内部联接金额studentid=amount.studentid”
while(rs.next()){
对于(i=0,k=1,p=2,m=3;i<1&&k<2&&p而言,问题是因为您在一次又一次地迭代同一行,并且我认为这使事情变得复杂,如果您将值设置为表
,代码可能会简单得多,如下所示:
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
}
非常感谢,它工作得很好。但是因为setValueAt()使用Object,int,int作为参数。因此,我将代码重写如下:感谢alot@javaguy现在整个代码将是5-6行,非常清晰易懂