如何修复 Java 中指出变量无法解析的错误?

发布于 2025-01-09 17:54:45 字数 1750 浏览 0 评论 0原文

我正在开发一个 Web 应用程序,其中不同类型的用户存储在数据库的不同表中。我正在尝试实施登录验证,其中接受电子邮件和密码。当我运行附加的代码时,错误指出变量 rs1 无法解析。我是软件开发的初学者,所以我试图使代码尽可能基本。 `// 登录验证 - 从登录表单中检索电子邮件和密码,并对照数据库的“患者”表进行检查以验证它们。如果电子邮件和密码正确,用户将登录。如果电子邮件和密码不正确,将向用户显示错误消息。

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");       
   }        
`

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");       
   }        
`

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

从﹋此江山别 2025-01-16 17:54:45

您在 ResultSet rs1 = pst1.executeQuery();} 之后添加了“}”,这将关闭 rs1 的范围直至该区域。因此,请正确格式化代码,并在 rs1 对象的 if-else 条件后使用括号“}”。

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.

野生奥特曼 2025-01-16 17:54:45

定义 rs1 之后直接有一个右括号。

ResultSet rs1 = pst1.executeQuery();}

所以这个括号后面的rs1不在访问范围之内。

There is a closing bracket directly after rs1 is defined.

ResultSet rs1 = pst1.executeQuery();}

So rs1 is not within the scope of access after this bracked.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文