我首页取数据老是取一半自动断了,日志中说数据库连接断开了,我查代码没有断开啊,到页面
底部才断开的。麻烦大家帮帮我

解决方案 »

  1.   

    <%
                    try{
                  JdbcOdbc jdbc = new JdbcOdbc();
                 conn = jdbc.getConnection();
                 try{
                 stat=conn.createStatement();
                sql = "select top 6 zs_id,dongtai_id,zs_biaoti from zs_dongtai order by zs_time desc";
                res = stat.executeQuery(sql);
                while (res.next()) {
                  %>
                      <tr>
                        <td width="20" height="19" align="center"><img src="images/top_point.gif" width="2" height="4" alt=""/></td>
                        <td><a href="c_congpanydongxin/<%=res.getString("zs_id").trim()%>/<%=res.getString("dongtai_id")%>.htm" title="<%=res.getString("zs_biaoti")%>" target="_blank"><%if (res.getString("zs_biaoti").length() >14) {%><%=res.getString("zs_biaoti").toString().substring(0, 14) %><%}else{%><%=res.getString("zs_biaoti") %><%} %></a></td>
                      </tr>
                      <%}}catch(Exception e){System.out.println(e.getMessage());}finally{try{res.close();stat.close();}catch(Exception e){}} %><%
                      try{
                        stat=conn.createStatement();
                  sql = "select top 12 zs_id,zs_company from zs_zhuce where zs_xianshi='是' order by zs_time desc";
                  res = stat.executeQuery(sql);
                  int j=0;
                  while (res.next()){
                  j=++j;%>
                      <tr>
                        <td width="10" height="19" align="center"><img src="images/ico2.gif" alt="" /></td>
                        <td width="155"><a href="comp_companyindex/<%=res.getString("zs_id").trim()%>.htm" title="<%=res.getString("zs_company")%>" target="_blank"><% if (res.getString("zs_company").length() > 12){%><%=res.getString("zs_company").toString().substring(0,12) %><%}else{%><%=res.getString("zs_company") %><%} %> </a></td>
                      </tr>
                      <%if(j==6){%>
                  </table></td>
                  <td width="5" valign="bottom">&nbsp;</td>
                  <td width="155" valign="bottom"><table width="155" border="0" cellspacing="0" cellpadding="0">
                    <%}}}catch(Exception e){System.out.println(e.getMessage());}finally{try{res.close();stat.close();}catch(Exception e){}}%>
    我就这样一直往下取数据,最后conn.close();
    这样有问题吗?
      

  2.   

    我本地是没有错的,放到服务器之后,日志中就出现
    Invalid state, the Connection object is closed.
    这样的错误
    首页不是有好多列表要取数据,对吧,取到某个列表时就断了,代码如上
      

  3.   


    public class JdbcOdbc {
        private final String driver="net.sourceforge.jtds.jdbc.Driver";
        private final String url="jdbc:jtds:sqlserver://localhost:1433/newcsdn";
        private final String usename = "sasfsdf";
        private final String password = "sfsdf";
        private Connection conn = null;
     public JdbcOdbc() {
      try {
          Class.forName(driver);
          } catch (Exception e) {
            System.out.println(e.getMessage());
           }
     }
      public Connection getConnection() {
       try {
            conn = DriverManager.getConnection(this.url, this.usename,
                                          this.password);
         } catch (Exception e) {
              System.out.println(e.getMessage());
            }
           return conn;
       }
    }我写了这样一个类进行连接的,要用到的地方调用它
      

  4.   

     注意这里:<%}}catch(Exception e){System.out.println(e.getMessage());}finally{try{res.close();stat.close();}catch(Exception e){}} %> 把conn也关闭,下面的代码中再重新生成一个conn,试试看