String time="m"+String.valueOf(month)+"d"+String.valueOf(day);
sql="insert into counte(date,today,first,secon,third,forth,fifth,sixth) values('time',0,0,0,0,0,0,0)";
smt.executeUpdate(sql);这里面date为字符型,而其它字段均为整型。为什么测试时通过,而在运行程序时总提示insert into 语句有错误呢?

解决方案 »

  1.   

    下面是我的整个程序,问题好像应该出自today=rs.getInt(2)这部分,错误结果是无效的游标状态
    <%@page contentType="text/html"%>
    <%@page pageEncoding="UTF-8"%>
    <%@page import="java.sql.*"%>
    <%@page import="java.util.*"%>
    <%@page import="java.sql.ResultSet"%>
    <%@include file="opendata.jsp"%>
    <%
     request.getSession(true);
     int month,day,hour,today,total,select;
     String time;
     String errmsg=request.getParameter("errmsg");
     GregorianCalendar calendar;
    calendar =new GregorianCalendar();
     month=calendar.get(Calendar.MONTH)+1;
     day=calendar.get(Calendar.DAY_OF_MONTH);
     hour=calendar.get(Calendar.HOUR_OF_DAY);
     time="m"+String.valueOf(month)+"d"+String.valueOf(day);
    sql="select * from counte where date='time'";
     rs=smt.executeQuery(sql);
     if(!rs.next())
     {
          sql="insert into counte(date,today,first,secon,third,forth,fifth,sixth) values('time',0,0,0,0,0,0,0)";
          smt.executeUpdate(sql);
       }
    sql="select * from counte where date='time'";
    rs=smt.executeQuery(sql);
    today=rs.getInt(2);
    select=(int)Math.floor(hour/4)+1;
    sql="select * from total";
    rs=smt.executeQuery(sql);
    total=rs.getInt(1);
    if(session.isNew())
    {
        today++;
        switch(select)
        {
            case 1:sql="update counte set today=today,first=first+1 where date='time'";
                    smt.executeUpdate(sql);
                    break;
            case 2:sql="update counte set today=today,secon=secon+1 where date='time'";
                    smt.executeUpdate(sql);
                    break;
            case 3:sql="update counte set today=today,third=third+1 where date='time'";
                    smt.executeUpdate(sql);
                    break;
            case 4:sql="update counte set today=today,forth=forth+1 where date='time'";
                    smt.executeUpdate(sql);
                    break;
            case 5:sql="update counte set today=today,fifth=fifth+1 where date='time'";
                    smt.executeUpdate(sql);
                    break;
            case 6:sql="update counte set today=today,sixtht=sixth+1 where date='time'";
                    smt.executeUpdate(sql);
            }
        total++;
        sql="update total set total=total+1";
        smt.executeUpdate(sql);
        smt.close();
        conn.close();
        }      
           %>
    <html>
             <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <title>人数浏览统计</title>
            <p align=center><img src="1.jpg"><br>
            <%
           String showcount=String.valueOf(total);
           out.print("您是本站的第");
           for(int i=0;i<showcount.length();i++)
            out.print("<img src="+showcount.charAt(i)+".jpg>");
             out.print("位上网使用者<br>");
             showcount=String.valueOf(today);
             out.print("您是今天的第");
             for(int i=0;i<showcount.length();i++)
             out.print("<img src="+showcount.charAt(i)+".jpg>");
             out.print("位上网使用者");
          %>
     <hr>
     <center>
     <form action="search.jsp" method=post>
     <table border=0>
     <tr>
     <td><font color="green">查询上网人数统计:</font></td>
     <td><select size=1 name=month>
     <option selected>1</option>
     <option>2</option>
     <option>3</option>
    <option>4</option>
    <option>5</option>
    <option>6</option>
    <option>7</option>
    <option>8</option>
    <option>9</option>
    <option>10</option>
    <option>11</option>
    <option>12</option></select>月</td>
    <td><selcet size=1 name=day>
    <option selected>1</option>
    <option>2</option>
    <option>3</option>
    <option>4</option>
    <option>5</option>
    <option>6</option><option>7</option><option>8</option><option>9</option>
    <option>10</option><option>11</option><option>12</option><option>13</option>
    <option>14</option><option>15</option><option>16</option><option>17</option><option>18</option><option>19</option>
    <option>20</option><option>21</option><option>22</option><option>23</option><option>24</option><option>25</option><option>26</option>
    <option>27</option><option>28</option><option>29</option><option>30</option><option>31</option></select>日</td>
    <td><input type=submit name=SEND value="查询"></td>
     </tr>
     <tr>
     
     <td colspan="4" align=center>
    <a href=search.jsp?month=<%=month%>&day=<%=day%>>
     <font size=3>查看今日各时段流量统计</font></a></td>
     </tr>
     </table>
     </form>
     <font color="red" size=4>
     <%if(errmsg!=null)
       out.print(errmsg);%>
     </font>
    </html>
      

  2.   

    sql="select * from counte where date='time'";
    rs=smt.executeQuery(sql);
    today=rs.getInt(2);在today=rs.getInt(2);之前应再调用一次
    rs.next();
    这个rs已经不是原来的rs了。
      

  3.   

    谢谢楼上,按您的方法第一个测试通过可以输入today的值。而total=rs.getInt(1)前面我也加上了rs.next();可仍会出现错误同样的错误提示。不知道为什么?
    today=rs.getInt(2);
    select=(int)Math.floor(hour/4)+1;
    sql="select * from total";
    rs=smt.executeQuery(sql);
    total=rs.getInt(1);
      

  4.   

    我晕......我都说过啦,执行smt.executeQuery(sql);
    后,rs就不是原来的rs啦,继续在total=rs.getInt(1);前
    加上rs.next()了.....
    凡是遇到这种情况你就next一下好了......
      

  5.   

    你的游标位置不对,你用select,检索出来的肯定是一堆数据(虽然也有可能只有一条),所以用while(rs.next())比较好些,或者用rs.first();,然后再用rs.getInt(2);等来取。老实讲,你的jsp中嵌这么多java代码真让我看得头晕。你要搞清楚rs.next(),rs.first(),rs.last(),rs.beforeFirst(),rs.absolute()等的关系,再做就不难了。
      

  6.   

    以后把java程序写到bean或servlet里去,这样看程序舒服些!
      

  7.   

    sql="select * from counte where date='time'";你这句错了
      

  8.   

    sql="select * from counte where date='time'";
    rs=smt.executeQuery(sql);
    today=rs.getInt(2);
    select=(int)Math.floor(hour/4)+1;
    sql="select * from total";
    rs=smt.executeQuery(sql);
    total=rs.getInt(1);
    查完后加一个rs.next90
    他就象链表一样,头是空的只是一个入口
      

  9.   

    sql="select * from counte where date='"+time+'"";