gpt4 book ai didi

mysql - 检查数据库的用户名和数据是否与用户信息匹配

转载 作者:行者123 更新时间:2023-11-28 22:55:20 26 4
gpt4 key购买 nike

我想检查数据库中已有的用户名和数据是否与用户信息匹配。我写了一些代码,但它没有选择一行。

这是我的代码:

public boolean iscorrect(String name , String pass){

boolean check=false;

openConnection();
Statement st =null;
ResultSet rs = null;

try{
st = conn.createStatement();
String q = "Select * from signup where email = '"+name+"'" ;
rs=st.executeQuery(q);
System.out.println(rs.getString(1)+" "+rs.getString(2));
if(rs.getString(1).equals(name) && rs.getString(2).equals(pass)){
check = true;
}

}catch(SQLException e){
e.printStackTrace();
}

return check;
}

最佳答案

需要先调用rs.next()将光标定位到第一行数据。如果没有数据,这将返回 false

除此之外:

  • 请使用PreparedStatement并绑定(bind)变量以避免SQL注入(inject)
  • 您可以将检查放在 WHERE 子句中而不是加载整行
  • 请不要存储明文密码,使用加密散列函数

关于mysql - 检查数据库的用户名和数据是否与用户信息匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28687429/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com