SQL select不会显示所选项目

时间:2014-09-26 22:08:32

标签: java sql

当我运行此代码时,为什么我在选择时获得最后添加的记录,而不是我选择的框中的项目?

String subj = null;
String sec = null;
String fac = null;

try {
    String tmp=(String) secCombo.getSelectedItem();
    String sql="select faculty,section,subject from facload where section=?";
    pst=(PreparedStatement) conn.prepareStatement(sql);
    pst.setString(1,tmp);
    rs=pst.executeQuery();
    while(rs.next()){
        sec = rs.getString("section");
        subj = rs.getString("subject");
        fac = rs.getString("faculty");
    }
} catch(Exception e){
    JOptionPane.showMessageDialog(null, e);
}

JOptionPane.showMessageDialog(null, "prof  " +fac + "\n" + "subject  " + subj);
if (subCombo1.getSelectedItem().equals(fac) && subCombo.getSelectedItem().equals(subj)) {
    JOptionPane.showMessageDialog(null, "lalalala");
}

1 个答案:

答案 0 :(得分:0)

下面:

while(rs.next()){
    sec = rs.getString("section");
    subj = rs.getString("subject");
    fac = rs.getString("faculty");
}

您在循环中一次又一次地分配相同的变量。循环结束后,它们具有在上一次迭代中设置的任何值。