我目前在显示从MS Access到Java jLabel的行值时遇到问题。
我有一个名叫Table
的Ms Access数据库,其中有3行,我只想在Java jlabels
中显示行,请参阅图片,但它显示了第一行,我目前很困惑,谢谢!
try {
resultset.first();
String name = "Name: ";
String dmg = "Damage: ";
String type = "Ammo Type: ";
//row1
weapon1.setText(name+resultset.getString("weapons"));
weapon1dmg.setText(dmg+resultset.getString("weaponDMG"));
weapon1type.setText(type+resultset.getString("weaponAmmoType"));
//row2
weapon2.setText(name+resultset.getString("weapons"));
weapon2dmg.setText(dmg+resultset.getString("weaponDMG"));
weapon2type.setText(type+resultset.getString("weaponAmmoType"));
//row3
weapon3.setText(name+resultset.getString("weapons"));
weapon3dmg.setText(dmg+resultset.getString("weaponDMG"));
weapon3type.setText(type+resultset.getString("weaponAmmoType"));
}
答案 0 :(得分:1)
您需要遍历结果集,否则您将一次又一次地读取第一行。
try {
String name = "Name: ";
String dmg = "Damage: ";
String type = "Ammo Type: ";
int count = 0;
while(resultset.next){
count++
if(count == 1){
weapon1.setText(name+resultset.getString("weapons"));
weapon1dmg.setText(dmg+resultset.getString("weaponDMG"));
weapon1type.setText(type+resultset.getString("weaponAmmoType"));
}else if(count == 2){
weapon2.setText(name+resultset.getString("weapons"));
weapon2dmg.setText(dmg+resultset.getString("weaponDMG"));
weapon2type.setText(type+resultset.getString("weaponAmmoType"));
}else if(count == 3)
weapon3.setText(name+resultset.getString("weapons"));
weapon3dmg.setText(dmg+resultset.getString("weaponDMG"));
weapon3type.setText(type+resultset.getString("weaponAmmoType"));
}
应该有一种更好的方法,但是我无权访问其余代码以了解如何简化它。