<%@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>各位帮我看看这个代码哪有错误呀我怎么也不能进行用户验证!急呀!先谢谢各位了!!!!
<%@ 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>各位帮我看看这个代码哪有错误呀我怎么也不能进行用户验证!急呀!先谢谢各位了!!!!
{result="no user";} else if(!rs.getString("password").equals(Password))
{result="password error";}
else {result="success";} 这个地方.当rs.next()之后,你的游标已经在第一条记录了,你后面再调用rs.next(),游标已经在第2条记录上了(如果你有的话),没有的话则游标在最后一条记录之后.
这样当然不能验证了.仔细点..
if(rs.getString("password").equals("passowrd")){
return true;//找到匹配的就退出去了
}
}
String sql="elect * from users where username='+username+' and password='+password+'";
System.out.print(userpass); ???这样用不能打印出语句吧?直接用out.println();好像。可能它影响到后面的了
不过有个建议,不要在jsp页面上写任何java代码
if(rs.getString("password").equals("passowrd")){
return true;//找到匹配的就退出去了
}
}
应该是
while(rs.hasNext()){
if(rs.next().getString("password").equals("passowrd")){
return true;//找到匹配的就退出去了
}
}
吧!