String u=request.getParameter("username");
String p=request.getParameter("passwd");
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
Connection ct=DriverManager.getConnection("jdbc:microsoft:sqlserver://127.0.0.1:1433;databaseName=stuinfo","sa","123");
Statement sm=ct.createStatement();
ResultSet rs=sm.executeQuery("select userId from userInfo where userName='"+u+"'"); if(rs.next()){
//用户名存在
if(rs.getString(1).equals(p)){
//合法用户
response.sendRedirect("wel.jsp");
}else{
//不合法用户
response.sendRedirect("login.jsp?error=1");
}
这是我的用户表
001 chengge
002 chengge2
003 chengge3 当我输入用户名为chengge 密码为001 时为什么会跳转到("login.jsp?error=1");?
如果把if(rs.getString(1).equals(p)){
改成if(p.equals("001"))就能跳转到("wel.jsp");
(难道是if(rs.getString(1).equals(p))这句有问题??求解)
String p=request.getParameter("passwd");
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
Connection ct=DriverManager.getConnection("jdbc:microsoft:sqlserver://127.0.0.1:1433;databaseName=stuinfo","sa","123");
Statement sm=ct.createStatement();
ResultSet rs=sm.executeQuery("select userId from userInfo where userName='"+u+"'"); if(rs.next()){
//用户名存在
if(rs.getString(1).equals(p)){
//合法用户
response.sendRedirect("wel.jsp");
}else{
//不合法用户
response.sendRedirect("login.jsp?error=1");
}
这是我的用户表
001 chengge
002 chengge2
003 chengge3 当我输入用户名为chengge 密码为001 时为什么会跳转到("login.jsp?error=1");?
如果把if(rs.getString(1).equals(p)){
改成if(p.equals("001"))就能跳转到("wel.jsp");
(难道是if(rs.getString(1).equals(p))这句有问题??求解)
equal不会错的
那就使用if(rs.getString(1).trim().equals(p.trim())){}