简体   繁体   中英

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

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; 
    }




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");

Maybe here:

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

Should be user_id or just id ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM