工大后院

 找回密码
 加入后院

扫一扫,访问微社区

QQ登录

只需一步,快速开始

搜索
查看: 1681|回复: 4

[Help]jsp连接数据厍的"ResultSet is closed"问题

[复制链接]
发表于 2006-3-23 15:50 | 显示全部楼层 |阅读模式
没想到才开始做JSP,难题又出现了.
我在做完登录检查,如果正确,就转到欢迎页面,在欢迎页面里我想让它显示新闻消息.
为了动态,我把新闻消息在做在一个数据表里,然后从数据厍读出来显示.代码如下:

  1. String newsInfo=null;
  2. Connection conn2=null;
  3. Statement stmt=null;
  4. try
  5.                 {
  6.                         Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  7.                         conn2= DriverManager.getConnection("jdbc:odbc:Test","sa","");
  8.                         stmt=conn2.createStatement();
  9.                         ResultSet rs=stmt.executeQuery("select * from news");
  10.                         while(rs.next()){
  11.                                 String title=rs.getString("title");
  12.                                 newsInfo=title;
  13.                                 stmt.close();
  14.                                 conn2.close();
  15.                         }
  16.                 }catch(Exception ex)
  17.                 {
  18.                         if(stmt!=null)stmt.close();
  19.                         if(conn2!=null)conn2.close();
  20.                         String errorMsg=ex.getMessage();
  21.                         newsInfo=new String(errorMsg.getBytes("GBK"),"ISO-8859-1");
  22.                 }

  23. %>
  24. <table><%=newsInfo%></table>
复制代码

可是,一执行起来,newsInfo总是"ResultSet is closed".我根本提前没关闭过它.真是百思不解.唯一关闭过的是上一个页面的conn.(注:登录处理的代码在"JSP入门一例"那个帖里)
我百度过了,好像很多人都有遇过这样的问题.可我就是没有找到解决方法.如果你知道,请告诉我!先谢谢了!
发表于 2006-3-23 15:59 | 显示全部楼层
楼主变量名使conn2?是否一个页面有两个conetion对象?
回复

使用道具 举报

发表于 2006-3-23 16:05 | 显示全部楼层
String newsInfo=null;
Connection conn2=null;
Statement stmt=null;
ResultSet rs = null;
try
                {
                        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                        conn2= DriverManager.getConnection("jdbc:odbc:Test","sa","");
                        stmt=conn2.createStatement();
                        rs=stmt.executeQuery("select * from news");
                        if(rs.next()){
                                newsInfo=rs.getString("title");
                        }
                }catch(Exception ex)
                {
                        String errorMsg=ex.getMessage();
                        newsInfo=new String(errorMsg.getBytes("GBK"),"ISO-8859-1");
                }finally
                {
                        if(rs!=null)rs.close();
                        if(stmt!=null)stmt.close();
                        if(conn2!=null)conn2.close();
                }

这样试试看...
回复

使用道具 举报

 楼主| 发表于 2006-3-23 16:05 | 显示全部楼层
楼主变量名使conn2?是否一个页面有两个conetion对象?

没有啊,是转到这个页面前的那个页面有个变量叫conn,我开始以为它们重名,就换了名,结果一样不行
回复

使用道具 举报

 楼主| 发表于 2006-3-23 17:29 | 显示全部楼层
原来ResultSet给悄悄closed,都怪我贪简单,把以前的代码拷来,没作好修改工作
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 加入后院

本版积分规则

QQ|Archiver|手机版|小黑屋|广告业务Q|工大后院 ( 粤ICP备10013660号 )

GMT+8, 2025-5-14 21:40

Powered by Discuz! X3.5

Copyright © 2001-2024 Tencent Cloud.

快速回复 返回顶部 返回列表