只登陆一次的代码设计,请教大家
代码如下:
<%@ page contentType= "text/html;charset=GB2312 " %>
<%@ page import= "java.sql.* " %>
<%@ page import= "java.util.* "%>
<html>
<link href= "file:///G|/css/wenzi.css " rel= "stylesheet " type= "text/css ">
<body>
<%
String driverName= "com.microsoft.jdbc.sqlserver.SQLServerDriver ";
String dbURL= "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=login ";
String userName= "sa ";
String passWord= " ";
Connection con=null;
Statement sql=null;
ResultSet rs=null;
String Name=request.getParameter( "username ");
byte[] b=Name.getBytes( "ISO-8859-1 ");
Name=new String(b);
String PassWord=request.getParameter( "password ");
byte[] b1=PassWord.getBytes( "ISO-8859-1 ");
PassWord=new String(b1);
int sum=0;
int dl=0;
boolean first=true;
try
{
Class.forName(driverName);
}
catch(
ClassNotFoundException e)
{
out.print(e.toString());
}
try
{
con=DriverManager.getConnection(dbURL,userName,passWord);
sql=con.createStatement();
String execute= "select * from login where UserName= ' "+Name+ " ' and PassWord= "+PassWord+ " ";
rs=sql.executeQuery(execute);
if(rs.next())
{
dl++;
if(dl <=1)
{
sum=rs.getInt( "count ");
sum++;
out.print( "登陆成功 ");
String execute1= "update login set count= "+sum+ " where UserName= "+ " ' "+Name+ " ' ";
sql.executeUpdate(execute1);
}
else
{
out.print( "你已经登陆了 ");
}
}
else
{
out.print( " <font size=1> 登陆错误 ");
}
con.close();
}
catch(
SQLException e)
{
out.print(e.toString());
}
%>
</body>
</html>
还是实现不了原来的目的,请大家帮我解决一下,谢谢
------解决方案--------------------你这样写,当然不能实现只登陆一次的目的啊,你的dl永远到不了2。帮你修改if(rs.next())这部分的代码。
if(rs.next()){
//数据库有这个人
//Name是前面你定义了的变量,用户名
//这个人登陆了吗?HttpSession这个类自己引进来
HttpSession oldsession = (HttpSession)application.getAttribute(Name);
if ((oldsession != null) && ((oldsession.getLastAccessedTime()- oldsession.getCreationTime() < getMaxInactiveInterval()*1000)) {
//用户已经登陆了,不能同时登陆
out.print( "你已经登陆了 ");
} else {
//向服务器注册当前用户,登陆成功
application.setAttribute(Name,session);
sum=rs.getInt( "count ");
sum++;
out.print( "登陆成功 ");
String execute1= "update login set count= "+sum+ " where UserName= "+ " ' "+Name+ " ' ";
sql.executeUpdate(execute1);
}
} else {
out.print( " <font si