2015-02-24 123 views
0

我想檢查數據庫中已存在的用戶名和數據是否與用戶信息匹配。我寫了一些代碼,但它沒有選擇一行。檢查數據庫的用戶名和數據是否與用戶信息匹配

這裏是我的代碼:

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

回答

1

您需要先打電話到rs.next()把光標在數據的第一行。如果沒有數據,這將返回false

除此之外:

  • 請使用PreparedStatement和綁定變量,以避免SQL注入
  • 你可以把支票轉換爲WHERE子句,而不是裝載整排
  • 請不要存儲明文密碼,使用加密哈希函數
+0

感謝它運行良好。 – 2015-02-24 03:04:14

相關問題