Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Java database statment problem

    • 0
    • 0
    • 0
    • 2
    • 0
    • 0
    • 0
    • 539
    Answer it

    Hello all,

    I dont know what im doing wrong, i only want to check the username and password in the database if they exist show, you are logged in but if they do not exist to show.. you have no access to db

    1. public void logIn(Student student) {
    2. Connection connection = null;
    3. String sql = "SELECT * FROM Student WHERE userName=? AND password=?";
    4. try
    5. {
    6. connection = getConnection();
    7. PreparedStatement statement = connection.prepareStatement(sql);
    8. ResultSet resultSet = statement.executeQuery();
    9. while (resultSet.next())
    10. {
    11.  
    12. student.setUserName(resultSet.getString("userName"));
    13. student.setPassword(resultSet.getString("password"));
    14. System.out.println("loggin in");
    15. }
    16.  
    17. }
    18.  
    19. catch (SQLException ex)
    20. {
    21. System.out.println("Something wnt wrong loggin in");
    22. } finally
    23. {
    24. closeConnection(connection);
    25. }
    26.  
    27. }
    28.  
    29.  
    30. AND THAN TEST IT WITH THIS MAIN THING;
    31.  
    32. public static void main(String[] args)
    33. {
    34. StudentDAOImpl dao = new StudentDAOImpl();
    35. Student st = new Student();
    36. st.setUserName("Tanya");
    37. st.setPassword("1234");
    38. dao.logIn(st);
    39.  
    40.  
    41. }

 2 Answer(s)

  • Update your section of code with :

    PreparedStatement statement = connection.prepareStatement(sql);

    //here username and password should be replaced with the variables that hold their values in your code

    **statement .setParameterList(1,username);

    statement .setParameterList(2,password);**

    ResultSet resultSet = statement.executeQuery();

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Reset Password
Fill out the form below and reset your password: