title: java.sql.SQLException: Illegal operation on empty result set. when authenticating
body: <p>What I'm trying to do is:</p>
  
  <ol>
  <li><p>Accept username(uname) and password(passw) as an input from the user.</p></li>
  <li><p>Using <code>ResultSet</code>, retrieve the only tuple, to which username in the database and username given by user suits. This tuple will contain username and password.</p></li>
  <li><p>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.</p></li>
  </ol>
  
  <p>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: <code>java.sql.SQLException: Illegal operation on empty result set.</code></p>
  
  <p>The code is:</p>
  
  <pre><code>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&amp;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) &amp;&amp; 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
  </code></pre>
  
  <p>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?</p>