如何从Ms Access数据库在Java中的jLabel中显示行?

时间:2019-03-28 14:08:05

标签: java ms-access

我目前在显示从MS Access到Java jLabel的行值时遇到问题。

我有一个名叫Table的Ms Access数据库,其中有3行,我只想在Java jlabels中显示行,请参阅图片,但它显示了第一行,我目前很困惑,谢谢!

GUI WITH <code>JLABELS</code>

My MS ACCESS TABLE

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"));

    }

1 个答案:

答案 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"));
    }

应该有一种更好的方法,但是我无权访问其余代码以了解如何简化它。