今天在做一数据库操作的时候出现了点问题,请大侠们指教下。
当程序执行到绿色行代码的时候,出现    com.microsoft.sqlserver.jdbc.SQLServerException: 该连接已关闭。
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.checkClosed(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.checkClosed(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerResultSet.checkClosed(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerResultSet.findColumn(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerResultSet.getInt(Unknown Source)
at wyrc.getType(wyrc.java:327)
at wyrc.doGet(wyrc.java:106)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:237)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:157)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:214)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:198)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:152)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:137)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:118)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:102)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:929)
at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:160)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:799)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:705)
at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:577)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:683)
at java.lang.Thread.run(Thread.java:619) 相关的代码如下:         public TypeEntity getType(int typeId) {
TypeEntity entity = null;
String sql = "select * from kidefet where dxlbid ="+typeId;
 ResultSet rs=null;
try {
rs=LookForResultSet(sql);
rs.last();
if(rs.getRow()>0)
{
rs.beforeFirst();
            while (rs.next()) {
entity = new TypeEntity();
entity.setTypeId(typeId);
entity.setTypeName(rs.getString("lbmc"));
entity.setYdId(rs.getInt("tyglejk"));
entity.setYdName(getChannelName(rs.getInt("wergltjk")));
错误提示的wyrc.getType(wyrc.java:327)行entity.setLtId(rs.getInt("kfgljttrklt"));                                entity.setLtName(getChannelName(rs.getInt("uygljklt")));
entity.setDxId(rs.getInt("mhgldxjk"));
entity.setDxName(getChannelName(rs.getInt("fghgls")));
entity.setXltId(rs.getInt("rweljkxlt"));
entity.setXltName(getChannelName(rs.getInt("ogljolt")));
entity.setBmdId(rs.getInt("iglbuy"));
entity.setBmdName(getChannelName(rs.getInt("aglbv")));
break;
}
}
rs.getStatement().close();
rs.close();
} catch (Exception e) {
e.printStackTrace();
}
return entity;
}       
         public String getChannelName(int channelId) {
String result = null;
String sql = "select zhglbt from hefeisms.uiesder where uieslid ="+channelId;
try {
ResultSet rs = LookForResultSet(sql);
rs.last();
if(rs.getRow()!=0)
{
rs.beforeFirst();
    while (rs.next()) {
result = rs.getString("zhglbt");
break;
}
     }
rs.getStatement().close();
    rs.close();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(result);
return result;
}
        
         public ResultSet LookForResultSet(String sqltext)
{  
Connection conn=null;
ResultSet rst=null;
try{
 conn=connPool.getConnection();//从连接池获取Connection
Statement stm=null;
stm=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
 rst=stm.executeQuery(sqltext);
}catch(Exception e)
{
e.printStackTrace();
}
      connPool.returnConnection(conn);
return rst;
}
请大虾们帮忙看看  

解决方案 »

  1.   

    代码重新编辑下:public TypeEntity getType(int typeId){
    TypeEntity entity = null;
    String sql = "select * from kidefet where dxlbid ="+typeId;
    ResultSet rs=null;
    try {
    rs=LookForResultSet(sql);
    rs.last();
    if(rs.getRow()>0)
    {
    rs.beforeFirst();
    while (rs.next()) {
    entity = new TypeEntity();

    entity.setYdName(getChannelName(rs.getInt("wergltjk")));
    错误提示的wyrc.getType(wyrc.java:327)行
    entity.setLtId(rs.getInt("kfgljttrklt")); 
    entity.setLtName(getChannelName(rs.getInt("uygljklt")));
    entity.setDxId(rs.getInt("mhgldxjk"));

    break;
    }
    }
    rs.getStatement().close();
    rs.close();
    } catch (Exception e) {
    e.printStackTrace();
    }
    return entity;
    }    
      public String getChannelName(int channelId) {
    String result = null;
    String sql = "select zhglbt from hefeisms.uiesder where uieslid ="+channelId;
    try {
    ResultSet rs = LookForResultSet(sql);
    rs.last();
    if(rs.getRow()!=0)
    {
    rs.beforeFirst();
    while (rs.next()) {
    result = rs.getString("zhglbt");
    break;
    }
    }
    rs.getStatement().close();
    rs.close();
    } catch (Exception e) {
    e.printStackTrace();
    }
    System.out.println(result);
    return result;
    }
        
      public ResultSet LookForResultSet(String sqltext)
    {   
    Connection conn=null;
    ResultSet rst=null;
    try{
    conn=connPool.getConnection();//从连接池获取Connection
    Statement stm=null;
    stm=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
    rst=stm.executeQuery(sqltext);
    }catch(Exception e)
    {
    e.printStackTrace();
    }
      connPool.returnConnection(conn);
    return rst;
    }
      

  2.   

    看看LookForResultSet在第326行(透过getChannelName)被调用的时候,是不是误释放了当前数据库链接。
    另外,
    ①抓ResultSet在手上不是个好习惯,最好从ResultSet中将需要的信息取出来后随即关闭之。
    ②多表查询,可以构建一个复杂一点的SQL语句,在一次数据库访问过程中搞定