<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<%@ page import="java.io.*,java.util.*,java.sql.*"%>
<%
String username=request.getParameter(username);
String password=request.getParameter(password);
String url ="jdbc:odbc:webmail";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection(url,"sa","");
Statement stmt = con.createStatement();
String sql="select * from userinfo where userID=username and password=password";
ResultSet rs = stmt.executeQuery(sql);
int rowscount=0;
try
{
while(rs.next())
{
rowscount++;
}
}
catch(Exception e)
{
}
if(rowscount!=0)
{
session.setAttribute("username",username.trim());
session.setAttribute("password",password.trim());
//response.sendRedirect("main.jsp");
out.print("OK");
}
else
{
response.sendRedirect("index.jsp");
}
%>
请问上面的例子可不可以实现验证用户的作用??哪里有错误啊??帮我看看好吗??
可以实现。
什么叫验证用户?
楼主,应该把下面这句改为:
String sql="select * from userinfo where userID=username and password=password";
String sql="select * from userinfo where userID="+username+" and "+password=password+"";
问题应该会解决的!
对啊,你的上面String sql="select * from userinfo where userID=username and password=password";语句有问题,应该改为String sql="select *from userInfo where userID="username"and password="password"";
如果实在有点问题,你把String url ="jdbc:odbc:webmail";Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");改为String url="jdbc:odbc:datasourcename(数据源名)";Class.forName=("microsoft.jdbc.sqlserver.SQLServerDriver");看看