我是java的新手,有一段代码用if-else编写我想把它改成switch语句但是我不知道在switch语句中写。这是我的代码。
<%
String userid = request.getParameter("username");
String pwd = request.getParameter("password");
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/auto_lube","root", "password");
Statement st = con.createStatement();
ResultSet rs; `rs = st.executeQuery("select * from users where uname='" + userid + "' and pass='" + pwd + "' and role='users'");`
if (rs.next()) {
String username = rs.getString("uname");
String email = rs.getString("email");
session.setAttribute("customer_name", username);
int user = rs.getInt("id");
session.setAttribute("customer_id", user);
if(dat.after(date)){
MailClient client = new MailClient();
String from="username@gmail.com";
String to = email;
String subject="Please Attention";
String message="Please change your vehicles oil today is expiery date?";
client.sendMail(from,to,subject,message);}
//out.println("welcome " + userid);
//out.println("<a href='logout.jsp'>Log out</a>");
response.sendRedirect("index.jsp");
} else {
response.sendRedirect("invalid.jsp");
}
答案 0 :(得分:0)
这里真正的问题是 - 当查询没有返回任何结果时,我的代码如何转移到无效页面。
你应该看看这个 - https://stackoverflow.com/a/6813771/1355930 - 它显示了一种更好的结果集处理方式。您的代码应该是这样的:
if (rs.isBeforefirst()) {
// do the stuff for logged in user
response.sendRedirect("index.jsp");
} else {
response.sendRedirect("invalid.jsp");
}
我的猜测是,问题不是你的if语句。尝试在所有情况下重定向到invalid.jsp,以确保您可以实际执行该重定向。