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
    • 243
    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

    public void logIn(Student student) {
            Connection connection = null;
                    String sql = "SELECT * FROM Student WHERE userName=? AND password=?";
            try 
                    {
                connection = getConnection();
                PreparedStatement statement = connection.prepareStatement(sql);
                             ResultSet resultSet = statement.executeQuery();
                 while (resultSet.next()) 
                             {
    
                                student.setUserName(resultSet.getString("userName"));
                                student.setPassword(resultSet.getString("password"));
                                 System.out.println("loggin in");
                            }
    
                    }    
    
                    catch (SQLException ex) 
                    {
                      System.out.println("Something wnt wrong loggin in");
            } finally 
                    {
                        closeConnection(connection);
                    }
    
                }
    
    
    AND THAN TEST IT WITH THIS MAIN THING; 
    
      public static void main(String[] args) 
        {
            StudentDAOImpl dao = new StudentDAOImpl();
            Student st = new Student();
            st.setUserName("Tanya");
            st.setPassword("1234");
            dao.logIn(st);
    
    
        }
    

 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
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: