Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

mysql - java.sql.SQLException: Illegal operation on empty result set. when authenticating

What I'm trying to do is:

  1. Accept username(uname) and password(passw) as an input from the user.

  2. Using ResultSet, retrieve the only tuple, to which username in the database and username given by user suits. This tuple will contain username and password.

  3. If the password given by user also suits the password in the database, the display the message that both creadentials are correct else one of them is wrong.

Everything works fine except in one case. When the username itself is wrong, the mysql will not find the attribute at all and will give this error: java.sql.SQLException: Illegal operation on empty result set.

The code is:

ok.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent ae)
        {
            String uname=jf1.getText();
            String passw=jf2.getText();
            String n;
            String m;
            try
            {
                Class.forName("com.mysql.jdbc.Driver").newInstance();
                Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/authentication?" + "user=root&password=letmein");
                PreparedStatement stmt=conn.prepareStatement("Select * from admin where id = ?");
                stmt.setString(1,uname);
                ResultSet rs=stmt.executeQuery();
                rs.next();
                n=rs.getString("id");
                m=rs.getString("pass");
                conn.close();
                if(n.equalsIgnoreCase(uname) && m.equalsIgnoreCase(passw))
                {
                    JOptionPane.showMessageDialog(null,"Username and password is correct");
                }
                else
                {
                    JOptionPane.showMessageDialog(null,"Username or password is not correct");
                }

            }
            catch(Exception ex)
            {
                  System.out.println(ex);
            }
        }//end of actionperformed
    });//end of actionlistener

Is there any way I can do both operations at a time (before closing the connection with database)?. If not, what's the alternative method?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You are supposed to use the result of rs.next() :

            if (rs.next()) {
                n=rs.getString("id");
                m=rs.getString("pass");
            }

If rs.next() returns false, this means the query returned no rows.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...