0

Please help me: here is a sample of the code. Everytime i press the login button it gives me java.sql.SQLExcpetion illegal operation on empty result set

Code for the Login Button:

 private void loginBtnActionPerformed(java.awt.event.ActionEvent evt) {                                         
    // TODO add your handling code here:

    con = DatabaseConnection.DBConnect();
    String sql = "SELECT * FROM user_table WHERE user_name = ? AND user_pass = ?";

    try
    {
        getPrivilege(username_Textfield.getText());

        prepStat = con.prepareStatement(sql);
        prepStat.setString(1, username_Textfield.getText());
        prepStat.setString(2,password_Textfield.getText());
        rs = prepStat.executeQuery();

        if(rs.next())
        {
            if (isAdmin == true)
            {
                JOptionPane.showMessageDialog(null,"Login Successful");
                mainDashboard mD = new mainDashboard();
                mD.setVisible(true);
                mD.setPrivilege(isAdmin);
                dispose();


            }else if(isAdmin == false)
            {
                JOptionPane.showMessageDialog(null,"Login Successful");
                userDashboard uD= new userDashboard();
                uD.setVisible(true);
                uD.setPrivilege(isAdmin);
                dispose();
            }

        }else
        {


                JOptionPane.showMessageDialog(null,"The username or password is incorrect","Access Denied", JOptionPane.ERROR_MESSAGE);
                username_Textfield.setBackground(Color.red);
                password_Textfield.setBackground(Color.red);
        }





    }catch(Exception e)
    {
        JOptionPane.showMessageDialog(null, e);
    }


}                                        

code for the connection class: the code here is for the DB connection i have a table called user which consists of user_id, user_name,user_pass, emp_id. all of which are set to not null

package guest_house;

//imports needed for the classs

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import javax.swing.JOptionPane;


 /**
 *
 * @author POWER
 */
public class DatabaseConnection {



public static Connection DBConnect()
{
    Connection con = null;
    PreparedStatement preStat = null;
    ResultSet rs = null;

    try
    {
        Class.forName("com.mysql.jdbc.Driver");
        con = DriverManager.getConnection("jdbc:mysql://localhost:3306/guest_house?","root","");
        return con;
    }
    catch(Exception e)
    {
        JOptionPane.showMessageDialog(null, e);
        return null;
    }
}



}
2
  • At which line is the exception happening? Commented Nov 8, 2017 at 11:03
  • It doesnt show me where the Exception is Commented Nov 8, 2017 at 13:29

1 Answer 1

3

Your column's name are user_id, user_name,user_pass, emp_id and table's name is user. But your sql sentence use userName, userPass and users words. You must change the sql sentence like this:

"SELECT * FROM user WHERE user_name =? AND user_pass=?"
Sign up to request clarification or add additional context in comments.

4 Comments

sorry i edited the post now, i had copied the wrong code. please revise it again gimme feedback
What is your table's name? user or user_table?
Table name is user_table. This exception pops up only when I click the login button without any credentials
What is database's name? Is it guest_house? or guest_house

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.