Skip to content
Advertisement

How do I fix an error in Java which states that variable cannot be resolved?

I am developing a web application where there are different types of users stored in different tables of the database. I am trying to implement login validation, where the email and password are accepted. When I run the attached code, the error states that the variable rs1 cannot be resolved. I am a beginner in software development so I am trying to keep the code as basic as possible. `// Login validation – the Email and Password are retrieved from the Login form and checked against the ‘patient’ table of the database to validate them. If the email and password are correct, the user will be logged in. If the email and password are incorrect, the user will be displayed an error message.

Connection conn = null;
try {
                String Email = request.getParameter("Email");
                String Password = request.getParameter("Password");
                Class.forName("com.mysql.cj.jdbc.Driver");

                conn = DriverManager.getConnection(
                                "jdbc:mysql://localhost:3306/telehealthsystem?autoReconnect=true&useSSL=false",
                                "root",
                                "Phoenix_204"
                        );
        PreparedStatement pst = conn.prepareStatement("Select P_Email,P_Password from patient where P_Email=? and P_Password=?");
        pst.setString(1, Email);
        pst.setString(2, Password);
        ResultSet rs = pst.executeQuery();                        
        if(rs.next()) {          
            out.println("Valid login credentials");       
        } else {
               PreparedStatement pst1 = conn.prepareStatement("Select Admin_Email, Admin_Password from administrator where Admin_Email =? and Admin_Password =?");
               pst1.setString(1, Email);
               pst1.setString(2, Password);
               ResultSet rs1 = pst1.executeQuery();}
                if(rs1.next()) {           
                    out.println("Valid login credentials"); 
                } else {
                out.println("Invalid login credentials"); }
                      
   }
   catch(Exception e){       
       out.println("Something went wrong !! Please try again");       
   }        
`

Advertisement

Answer

you have added “}” after ResultSet rs1 = pst1.executeQuery();}, which is closing the scope of rs1 till this area. So please format the code properly and use this bracket “}” after your if-else condition of rs1 object.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement