日期:2014-05-17  浏览次数:20930 次

【菜鸟求助】JSP页面与数据库的疑问
上代码

HTML code
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page import="java.sql.Connection"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.text.SimpleDateFormat"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'login2.jsp' starting page</title>
    
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>
  <jsp:useBean id="db" scope="page" class="com.cn.db.OpenDb"></jsp:useBean>
  <body>
    <%
        String uid = request.getParameter("uid");
        String pwd = request.getParameter("pwd");
        String sql = "select * from userinfo where uid = '" + uid + "'" +
                        " and pwd = '" + pwd +"'" ;
        Connection conn = db.getConnection();
        Statement stmt = conn.createStatement();
        ResultSet rs = stmt.executeQuery(sql);
        if(rs.next()){
            rs.close();
            
            String lastTime = new SimpleDateFormat("yyyy-MM-dd HH:ss:mm").format(new Date());
            sql = "update userinfo set loginTimes = loginTimes + 1 , lastTime = '" + lastTime +"' where uid = '"+ uid +"'";
            stmt.execute(sql);
            stmt.close();
            conn.close();
            session.putValue("uid",uid);
        
     %>
     <script type="text/javascript">
         self.location="all.jsp";
     </script>
     <%
         }else{
             rs.close();
             stmt.close();
             conn.close();
         }
      %>
      <script type="text/javascript">
          alert("对不起,您输入有错误,请重新输入");
          history.back();
      </script>
  </body>
</html>


其中loginTimes有问题 登录一次就加1,可是 没登录一次loginTimes直接加2 琢磨了半天 没琢磨出来。。求指导
有什么好办法调试吗?
我直接在数据库里运行 update userinfo set loginTimes = loginTimes + 1 没有问题,可是登录以后 无论是我在数据库里查询 还是页面显示 都是每登录一次加2.。。。。。

------解决方案--------------------
估计是你这句话执行了2次:
sql = "update userinfo set loginTimes = loginTimes + 1 , lastTime = '" + lastTime +"' where uid = '"+ uid +"'";
你在它后面加个打印,看看输出几次
Systems.out.println("sql:"+sql);
再检查下这个页面
 self.location="all.jsp";

------解决方案--------------------
<script type="text/javascript">
self.location="all.jsp";
</script>
这个的问题