【问题标题】:Checking database's username and data if they match the user info or not检查数据库的用户名和数据是否与用户信息匹配
【发布时间】:2015-04-25 13:58:16
【问题描述】:

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

这是我的代码:

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

【问题讨论】:

    标签: mysql sql database tomcat xampp


    【解决方案1】:

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

    除此之外:

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

    【讨论】:

      猜你喜欢
      • 2018-03-30
      • 2017-01-02
      • 1970-01-01
      • 1970-01-01
      • 2018-05-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多