如何修复 Java 中指出变量无法解析的错误?
我正在开发一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您在
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.定义 rs1 之后直接有一个右括号。
所以这个括号后面的rs1不在访问范围之内。
There is a closing bracket directly after rs1 is defined.
So rs1 is not within the scope of access after this bracked.