1

I'm trying to create a log-in page but I get this error :

error: java.sql.SQLException: Column 'user.id' not found. 

When I call the class it cannot seem to find my column Id. The Id column is in my SQL database with the correct name. However, it does not work.

I've tried looking for different solutions, but I cannot seem to find any. Please help. Below are my Java DAO classes and my jsp file.

public static user getAccountByNamePass(String user_Name, String password) {
        user result = null;
        Connection connection = null;
        PreparedStatement ps = null;

        try {
            DB_Connection obj_DB_Connection = new DB_Connection();
            connection=obj_DB_Connection.get_Connection();

            String sql = "SELECT * FROM users WHERE user_name = ? AND password = ?";

            ps = connection.prepareStatement(sql);

            ps.setString(1, user_Name);
            ps.setString(2, password);
            ResultSet res = ps.executeQuery();
            while (res.next()) {
                result = createAccountResult(res);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } 
        return result;
    }


    static user createAccountResult(ResultSet res) throws SQLException {
        user buyer = null;
        int id2 = res.getInt("user.id");
        String firstName = res.getString("user.first_name");
        String lastName = res.getString("user.last_name");
        String userName = res.getString("user.user_name");
        String password = res.getString("user.password");

        buyer = new user(id2, firstName, lastName, userName, password);
        return buyer;
    } 




    ArrayList<String> errors = new ArrayList<>();

    String user_name = "";
    String password = "";

    if (request.getParameter("login") != null) {
        user_name = request.getParameter("user_name");
        password = request.getParameter("password");

        if (user_name.trim().isEmpty()) {
            errors.add("Please enter username");
        }

        if (password.trim().isEmpty()) {
            errors.add("Please enter password");
        }

        if (errors.isEmpty()) {
            user user = usersDAO.getAccountByNamePass(user_name, password);

            if (user == null) {
                errors.add("Wrong username or password!");
            } else {
                UserSession sessionModel = (UserSession) request.getSession().getAttribute("sessionModel");
                sessionModel.login(user.getId());

                response.sendRedirect("frontPage.jsp");
                return;
            }
        }
    } else if (request.getParameter("logout") != null) {
        UserSession model1 = (UserSession) request.getSession().getAttribute("sessionModel");
        model1.logout();
        response.sendRedirect("frontPage.jsp");
        return; 
    }




2
  • 1
    Please, respect the naming conventions. It's not hard: classes always start with an uppercase letter and are CamelCase (no underscore!). Variables always start with a lowercase letter, and are camelCase (no underscore!). You're making your own life (and ours) difficult by not respecting them. Commented Jan 18, 2020 at 9:48
  • Does this answer your question? JDBC ResultSet get columns with table alias Commented Jan 18, 2020 at 9:58

2 Answers 2

3

You don't need to use the name of the table, just use the name of column like so :

int id2 = res.getInt("id");
String firstName = res.getString("first_name");
String lastName = res.getString("last_name");
String userName = res.getString("user_name");
String password = res.getString("password");
Sign up to request clarification or add additional context in comments.

2 Comments

@tobiasmadsen17 this is a new error, which mean a new problem, which mean, you have to create a new question, because each post should focus on one issue not multiple
Okay thank you. Thanks for the tip and help. I'm kinda new to the community.
0

Maybe here:

int id2 = res.getInt("user.id");

Should be user_id or just id?

1 Comment

I've changed it to just "id" and got a new error message: Message An exception occurred processing [/logIn.jsp] at line [31] Description The server encountered an unexpected condition that prevented it from fulfilling the request.

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.