Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7590865
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T20:30:42+00:00 2026-05-30T20:30:42+00:00

Quick question. I got a code below, which checks with a database whether a

  • 0

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. 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. 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();
        }
        }
        });
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T20:30:44+00:00Added an answer on May 30, 2026 at 8:30 pm

    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. Why would you expect it to return true then?

    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");
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

quick question regarding Array's in xcode. I have ht efollowing code, which is supposed
Alright, I've got a quick question. I'm currently working with a legacy database, so
Hopefully a quick URL rewrite question for you all. I've got a page on
I've got a quick question about default values in PL/SQL functions in Oracle. Take
I got a quick question about running a directX application (C++) in a managed
Quick question, I was curious if there is any difference between a database table
Quick question, If I want to document some code on a basic HTML and
A quick question on binding to a command in WPF. I've got a command
I've got a quick question that I hope someone can help me with. I'm
I got a quick question, I am building an ASP.NET website and have a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.