简体   繁体   English

Java IF ELSE语句

[英]Java IF ELSE statements

Quick question. 快速提问。 I got a code below, which checks with a database whether a given ID is available in the database, if so, it retrieves all attendance histories for the ID, otherwise if the ID didn't exist in the database it should display an error message. 我在下面获得了一个代码,该代码与数据库一起检查数据库中是否有给定的ID,如果有,它将检索该ID的所有出勤历史记录,否则,如果该ID在数据库中不存在,则应显示一条错误消息。 。 The code I got however, always says that the record has been attached, and then displays the error message as well regardless whether the ID exists or not. 但是,我得到的代码总是说记录已附加,然后无论ID是否存在也都会显示错误消息。 I think I probably have to change the order of the code or such? 我想我可能必须更改代码等的顺序? Could you please advise? 您能否提一些建议?

    JButton button = new JButton("Submit");
    button.addActionListener(new ActionListener() {
        @SuppressWarnings("unused")
        public void actionPerformed(ActionEvent arg0) {


        String studentID = textField.getText(); 

        try {

            Statement st = con.con.createStatement();
            ResultSet rs = st.executeQuery("SELECT StudentID, date FROM attendance WHERE StudentID ="+textField.getText());


            while ( rs.next() ) {
                String student = rs.getString("StudentID");
                String date = rs.getString("date");
                System.out.println(student+"");
                System.out.println(date);
            }

            JOptionPane.showMessageDialog(frmAttendanceHistory, "Attendance has been registered.");
            frmAttendanceHistory.setVisible(false);

            if (!rs.next()) {
            JOptionPane.showMessageDialog(frmAttendanceHistory, "A record for the given Student ID doesn't exist");
            }

        } 


        catch (SQLException e) {
        JOptionPane.showMessageDialog(frmAttendanceHistory, "Attendance couldn't be registered. Please try again!");
        e.printStackTrace();
        }
        }
        });

You're repeatedly calling rs.next() until it returns false (as otherwise you'll never get out of the while loop) - then you're calling rs.next() again. 您将反复调用rs.next()直到它返回false(否则您将永远不会退出while循环)-然后,您将再次调用rs.next() Why would you expect it to return true then? 那么,您为什么期望它返回true?

I suspect you want something like: 我怀疑您想要类似的东西:

if (rs.next()) {
    String student = rs.getString("StudentID");
    String date = rs.getString("date");
    System.out.println(student+"");
    System.out.println(date);
    JOptionPane.showMessageDialog(frmAttendanceHistory,
       "Attendance has been registered.");
    frmAttendanceHistory.setVisible(false);
} else {
    JOptionPane.showMessageDialog(frmAttendanceHistory,
        "A record for the given Student ID doesn't exist");
}

You're doing a second rs.next, which would go to a second record if there was one. 您正在执行第二个rs.next,如果存在,它将转到第二个记录。 If there's only one (which is already consumed further up in your app), then it fails giving you a success followed by a failure. 如果只有一个(已经在您的应用程序中进一步消耗了),那么它将失败并给您带来成功,然后是失败。

while ( rs.next() ) {

followed by 其次是

if (!rs.next()) {

Doesn't make any sense as the if will always execute. 没有任何意义,因为if会一直执行。 Your loop will continue until rs.next() returns false. 您的循环将继续进行,直到rs.next()返回false。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM