简体   繁体   中英

trying get all the rows in mysql table then do something

我想创建一个注册/登录系统,在我执行脚本后出现问题,当我想查看我使用的 mysql 数据库中是否存在登录电子邮件或登录密码时(而 resultset.next())然后检查如果登录电子邮件存在于表中,但问题是,它需要每一行,然后像它一样执行 if confition,看看第一个 raw 是否具有相同的登录值,然后做一些事情,如果不是,它会做错误按摩,但我想要它在查看所有行之后执行此操作,如果所有行都没有登录信息,则发送错误消息

Instead of iterating over the entire ResultSet , you should have the email and (hashed) password in the where clause:

Connection con = ...;
try (PreparedStatement ps = 
     con.prepareStatement("SELECT * FROM users WHERE email = ? AND password = ?") {
    ps.setString(1, loginEmail);
    ps.setString(2, hashedPassword);
    try (ResultSet rs = ps.executeQuery()) {
        if (rs.next()) {
            // login successful, perform some logic
        } else {
           // login not successful, perform some different logic
        }
    }
}

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