<%@page contentType="text/html;charset=gbk" %>
<%@ page import="java.sql.*"%><% 
   String username=request.getParameter("name");
   String userpass=request.getParameter("password");
   System.out.print(username);
  System.out.print(userpass);
   String result=null;
   String Login=username.trim();
   String Password=userpass.trim();
  Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn= DriverManager.getConnection("jdbc:odbc:user");
 Statement stmt=conn.createStatement();
String sql="select * from user";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {%>if(!rs.next())
  {result="no user";}else if(!rs.getString("password").equals(Password))
 {result="password error";}
else {result="success";} rs.close();
 stmt.close();
 conn.close();if(result.equals("success"))
{
response.sendRedirect("body.jsp");
}
%>
<html>
<title>用户登录</title>
<body>
<hr>
<center>
<%=result%>
<p></p>
<a href="regigster.jsp">重新登录</a>
</center>
</body>
</html>各位帮我看看这个代码哪有错误呀我怎么也不能进行用户验证!急呀!先谢谢各位了!!!!

解决方案 »

  1.   

    while(rs.next()) {%> if(!rs.next()) 
      {result="no user";} else if(!rs.getString("password").equals(Password)) 
    {result="password error";} 
    else {result="success";} 这个地方.当rs.next()之后,你的游标已经在第一条记录了,你后面再调用rs.next(),游标已经在第2条记录上了(如果你有的话),没有的话则游标在最后一条记录之后.
    这样当然不能验证了.仔细点..
      

  2.   

    难道把while(rs.next())循环去掉吗?直接调用符合条件的那条记录?我觉得我的select * from user好像有问题是不是把*改成password呀?
      

  3.   

    你都用while(rs.next()){
    if(rs.getString("password").equals("passowrd")){
      return true;//找到匹配的就退出去了
    }
    }
      

  4.   

    你直接写sql语句
    String sql="elect * from users where username='+username+' and password='+password+'";
      

  5.   

     System.out.print(username); 
      System.out.print(userpass); ???这样用不能打印出语句吧?直接用out.println();好像。可能它影响到后面的了
      

  6.   

    1对了,经过试验那语句可以打印,汗
    不过有个建议,不要在jsp页面上写任何java代码
      

  7.   

    一楼正解。调用ResultSet的next()方法后游标就会后移一行,第二次肯定返回false了
      

  8.   

      用JSP+Servlet+Bean 可以  MVC
      

  9.   

    while(rs.next()){
    if(rs.getString("password").equals("passowrd")){
      return true;//找到匹配的就退出去了
    }
    }
    应该是
    while(rs.hasNext()){
    if(rs.next().getString("password").equals("passowrd")){
      return true;//找到匹配的就退出去了
    }
    }
    吧!