I have a ResultSet
. This ResultSet
is filling a JComboBox
with data like this:
我有一个ResultSet。这个ResultSet正在用这样的数据填充JComboBox:
DatabaseHandler dh = new DatabaseHandler();
public ResultSet Klanten;
public BestellingenNieuwPanel() {
initComponents();
//Removes all items from KlantBox.
KlantBox.removeAllItems();
//Removes all from ProductBox.
ProductBox.removeAllItems();
try {
Klanten = dh.getDataFromDB("select * from klanten");
while (Klanten.next()) {
String strKlanten = Klanten.getString("Achternaam");
KlantBox.addItem(Klanten.getString(3));
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
This workes just fine. But, whenever I try to loop through ResultSet
Klanten again like this:
这工作得很好。但是,每当我尝试再次循环ResultSet Klanten时,就像这样:
private void KlantBoxActionPerformed(java.awt.event.ActionEvent evt) {
if(Klanten != null){
String strSelected = KlantBox.getSelectedItem().toString();
try {
while(Klanten.next()){
String KlantVoornaam = Klanten.getString(1);
String KlantAchternaam = Klanten.getString(2);
String KlantWoonplaats = Klanten.getString(4);
if(strSelected == KlantAchternaam){
txtKlantNaam.setText(KlantAchternaam);
txtKlantWoonplaats.setText(KlantWoonplaats);
}
}
} catch (SQLException ex) {
}
}
}
My JComboBox
Klanten only has it's last value. I've fiddled around with the Klanten.next() function and found that that function is the source of the problem.
我的JComboBox Klanten只有它的最后一个值。我已经摆弄了Klanten.next()函数,发现该函数是问题的根源。
Are there any other ways to loop through a ResultSet
? Or is there a way to loop through a ResultSet
WITHOUT resetting my JComboBox
?
有没有其他方法来循环ResultSet?或者有没有办法循环ResultSet而不重置我的JComboBox?
2 个解决方案
#1
1
Don't compare strings like this if(strSelected == KlantAchternaam){
use equals() instead.
如果(strSelected == KlantAchternaam){use equals(),请不要比较这样的字符串。
But in fact after refreshing the combobox just call setSelectedItem(strSelected )
但事实上刷新组合框后只需调用setSelectedItem(strSelected)
#2
0
I think the error is with your comparison of string using ==
rather than with your use of Klanteen.next()
我认为错误是你用字符串比较==而不是你使用Klanteen.next()
Try using the following code
尝试使用以下代码
if(strSelected.equals(KlantAchternaam)){
txtKlantNaam.setText(KlantAchternaam);
txtKlantWoonplaats.setText(KlantWoonplaats);
}
or this one, if you want to ignore case
或者这个,如果你想忽略大小写
if(strSelected.equalsIgnoreCase(KlantAchternaam)){
txtKlantNaam.setText(KlantAchternaam);
txtKlantWoonplaats.setText(KlantWoonplaats);
}
#1
1
Don't compare strings like this if(strSelected == KlantAchternaam){
use equals() instead.
如果(strSelected == KlantAchternaam){use equals(),请不要比较这样的字符串。
But in fact after refreshing the combobox just call setSelectedItem(strSelected )
但事实上刷新组合框后只需调用setSelectedItem(strSelected)
#2
0
I think the error is with your comparison of string using ==
rather than with your use of Klanteen.next()
我认为错误是你用字符串比较==而不是你使用Klanteen.next()
Try using the following code
尝试使用以下代码
if(strSelected.equals(KlantAchternaam)){
txtKlantNaam.setText(KlantAchternaam);
txtKlantWoonplaats.setText(KlantWoonplaats);
}
or this one, if you want to ignore case
或者这个,如果你想忽略大小写
if(strSelected.equalsIgnoreCase(KlantAchternaam)){
txtKlantNaam.setText(KlantAchternaam);
txtKlantWoonplaats.setText(KlantWoonplaats);
}