powerwind 发表于 2006-3-23 15:50

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

没想到才开始做JSP,难题又出现了.
我在做完登录检查,如果正确,就转到欢迎页面,在欢迎页面里我想让它显示新闻消息.
为了动态,我把新闻消息在做在一个数据表里,然后从数据厍读出来显示.代码如下:

String newsInfo=null;
Connection conn2=null;
Statement stmt=null;
try
                {
                        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                        conn2= DriverManager.getConnection("jdbc:odbc:Test","sa","");
                        stmt=conn2.createStatement();
                        ResultSet rs=stmt.executeQuery("select * from news");
                        while(rs.next()){
                                String title=rs.getString("title");
                                newsInfo=title;
                                stmt.close();
                                conn2.close();
                        }
                }catch(Exception ex)
                {
                        if(stmt!=null)stmt.close();
                        if(conn2!=null)conn2.close();
                        String errorMsg=ex.getMessage();
                        newsInfo=new String(errorMsg.getBytes("GBK"),"ISO-8859-1");
                }

%>
<table><%=newsInfo%></table>

可是,一执行起来,newsInfo总是"ResultSet is closed".我根本提前没关闭过它.真是百思不解.唯一关闭过的是上一个页面的conn.(注:登录处理的代码在"JSP入门一例"那个帖里)
我百度过了,好像很多人都有遇过这样的问题.可我就是没有找到解决方法.如果你知道,请告诉我!先谢谢了!

wool王 发表于 2006-3-23 15:59

楼主变量名使conn2?是否一个页面有两个conetion对象?

wool王 发表于 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();
                }

这样试试看...

powerwind 发表于 2006-3-23 16:05

楼主变量名使conn2?是否一个页面有两个conetion对象?
没有啊,是转到这个页面前的那个页面有个变量叫conn,我开始以为它们重名,就换了名,结果一样不行

powerwind 发表于 2006-3-23 17:29

原来ResultSet给悄悄closed,都怪我贪简单,把以前的代码拷来,没作好修改工作
页: [1]
查看完整版本: [Help]jsp连接数据厍的"ResultSet is closed"问题