1

So I making this login form where the credentials (username and password) are stored in MYSQL. I want the username and password to be case sentive.

AuthDAO.java

public static int checkUserPass(String username, String password){
    int userId = -1;
    try{
        Class.forName("com.mysql.jdbc.Driver");
        cn = DriverManager.getConnection("jdbc:mysql://localhost/b440", "root", "b440");

        String q="SELECT userId FROM user WHERE username='"+username+"' AND password='"+password+"'";

        Statement st = cn.createStatement();
        ResultSet rs = st.executeQuery(q);
        while(rs.next()){
            userId=Integer.parseInt(rs.getString("userId"));
        }
        rs.close();
        st.close();         
    }catch(Exception e){
        System.err.println(e.getMessage());
        e.printStackTrace();
    }
    DB_close();
    if(userId!=-1)
        return userId;
    return -1;
}

LoginServlet.java

else{
    int ID = checkUserPass(username, password, role);
    if(ID!=-1 ){ //User Is Legit!!
        loggedIn = "true";
        HttpSession se = request.getSession();
        se.setAttribute("user", getUserById(ID));
        se.setAttribute("loggedIn", loggedIn);
        url = "/index.jsp";
        msg = "Login Successful!";

        request.setAttribute("loggedIn", loggedIn);
        request.setAttribute("msg", msg);
    }
    else{
        msg = "Invalid Login, please check username or password";
        url = "/login.jsp";
        loggedIn = "false";
        request.setAttribute("loggedIn", loggedIn);
        request.setAttribute("msg", msg);           
    }       
4
  • What do you mean by case sensitive? Commented Oct 4, 2015 at 18:20
  • 4
    Not what you asked - but PLEASE read all about SQL injection attacks. Commented Oct 4, 2015 at 18:23
  • It means it should differentiate between capital and lower case letters. For eg: if the username and passwords are Flash and faST respectively then it shouldn't accept flash, FLASH or fast, FAST as the login credentials. Commented Oct 4, 2015 at 18:24
  • 1
    For god's sake please change to prepared statements to fill in username and password and use a secure hashing algorihm with salting to store passwords. Commented Oct 4, 2015 at 18:25

1 Answer 1

2

By default mysql queries are case-insensitive.

though you can use BINARY like this -

SELECT userId FROM user WHERE BINARY username='"+username+"' AND  BINARY password='"+password+"'

Or there are some other ways -

http://dev.mysql.com/doc/refman/5.0/en/case-sensitivity.html

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.