日志报错:set.java.sql.SQLException: Illegal operation on empty result set.org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot get a connection, pool error Timeout waiting for idle object不能连接到数据源
怀疑有连接没有关闭.请帮忙看看代码.谢谢
DBConnSource.java:/*
 * 数据源连接BEAN
  */
package mybean;import java.sql.*;
import javax.sql.*;
import javax.naming.*;public class DBConnSource {
    private Connection conn;
    private Statement stmt;
    private PreparedStatement pstmt;
    public DBConnSource(String dsName){
     try{
     Context initCtx = new InitialContext();
     Context ctx =(Context)initCtx.lookup("java:comp/env");
     DataSource ds =(DataSource)ctx.lookup(dsName);
     conn = ds.getConnection();
     }
     catch(Exception e)
     {
     System.out.print(e.toString());
     }
    }
    public synchronized Statement getStmt()throws Exception
    {
     stmt=conn.createStatement();
     return stmt;
    }
    public synchronized PreparedStatement getPstmt(String sql)throws Exception
    {
     pstmt=conn.prepareStatement(sql);
     return pstmt;
    }
    public void DBclose(){
     try{             //关闭 Connection conn;
     if(conn!=null){
     conn.close();
     }
     }catch(Exception e){
     System.out.print(e.toString());
     }finally{
     conn=null;
     }
    
     try{               //关闭Statement stmt;
     if(stmt!=null){
     stmt.close();
     }
     }catch(Exception e){
     System.out.print(e.toString());
     }finally{
     stmt=null;
     }
    
     try{               //关闭 PreparedStatement pstmt;
     if(pstmt!=null){
     pstmt.close();
     }
     }catch(Exception e){
     System.out.print(e.toString());
     }finally{
     pstmt=null;
     }
    }
}stylelist.java:/*
 * 网站JAVABEAN
 * 所有帖子分类列表.
 * 使用数据源连接.
 * 前后台共用JAVABEAN.
 */
package mybean;import java.sql.*;import mybean.DBConnSource;public class StyleList {
private String tableName;
    private Statement stmt;
    private ResultSet rs;
    private DBConnSource dbc; 
    
    public StyleList(){}
    
    public void setTableName(String n){
     this.tableName=n;
    }
    
    public StringBuffer getBuffer(){
     StringBuffer buffer=new StringBuffer();
     dbc=new DBConnSource("jdbc/myweb");
        try{
        
         stmt=dbc.getStmt(); 
            }catch(Exception e){
                        System.out.print("不能连接到数据源");
            }
             
         try{
             String strSql="SELECT * FROM "+tableName; 
             rs = stmt.executeQuery(strSql);
      
             while(rs.next()){         
             buffer.append("<a href='style.jsp?style="+rs.getString("style")+"' target=_blank>"+rs.getString("style")+"</a>");
             buffer.append("&nbsp;&nbsp;&nbsp;");
             }        
     }catch(SQLException e){
     System.out.print(e.toString());
     }finally{
     try{               //关闭 ResultSet rs.
        if(rs!= null){
        rs.close();}
     }catch(SQLException ex){
        System.out.print(ex.toString());
     }finally{
        rs=null;
     }    
      
     try{               // 关闭 Statement stmt. 
         if(stmt!= null){
         stmt.close();}
      }catch(SQLException ex){
         System.out.print(ex.toString());
      }finally{
         stmt=null;
      }    
      dbc.DBclose();  //关闭 DBConnsource dbc;
     }
      return buffer;
    }
}重点是几个关闭连接的部分.这样写逻辑上有没有漏洞.既然报错,看来一定在什么地方还有问题?谢谢

解决方案 »

  1.   


    if(conn!=null){
                conn.close();
                }
    应该改成
    if(conn!=null && !conn.closed()){
                conn.close();
                }
      

  2.   

    set.java.sql.SQLException: Illegal operation on empty result set.org.apache.tomcat.dbcp.dbcp.SQLNestedException
    不合法的操作空的返回结果集
      

  3.   

    if(conn!=null){
                conn.close();
                }
    应改为:
    if(conn!=null){
                conn.close();
                conn=null;
                }
      

  4.   

    Illegal operation on empty result 非法操作一个空的result
      

  5.   

    我知道是操作一个空的result.
    但是到底是哪里出问题.要怎么修改呢.
    请各位高人说的详细一点好不好?
      

  6.   

    难道CSDN这个就没人能解决我这个问题吗
      

  7.   

    怎么没有打开数据库的操作第二个类  ,还有返回值是resultset怎么是StringBuffer   肯定操作一个空的集合了
      

  8.   

    楼上的能否说的清楚一些.我是新手.
    第二个类指的是什么?
    resultset 与StringBuffer的定义有什么冲突吗?
    谢谢
      

  9.   

    另外还有一个问题:如果说是我的程序代码有问题.那为什么我重启一下TOMCAT就可以正常访问.
    我这个问题出在远程空间的TOMCAT上.每隔一段时间远程空间就能访问.需要重启一下TOMCAT才行?
    如果是resultset的定义有问题,重启TOMCAT就可以吗?
      

  10.   

    你这问题显而易见,有些conn占有资源去没有被释放,所以会出现conn获取不到的时候。你看看你所加的同步方法,synchronized。
      

  11.   

    解决了一个空指针的问题.
    去掉rs=null,在rs.close()后面不需要再加rs=null;
    是我自己画蛇添足了.
    谢谢大家.