日志报错: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(" ");
}
}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;
}
}重点是几个关闭连接的部分.这样写逻辑上有没有漏洞.既然报错,看来一定在什么地方还有问题?谢谢
怀疑有连接没有关闭.请帮忙看看代码.谢谢
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(" ");
}
}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;
}
}重点是几个关闭连接的部分.这样写逻辑上有没有漏洞.既然报错,看来一定在什么地方还有问题?谢谢
解决方案 »
- 一个导出Excel的方法会重复执行两次,求解!
- java模拟登陆,怎么才知道自己是登陆成功,我获取的是不是登陆成功后的session。
- DAO工厂类中如何将DAO实现类与DAO接口连接
- 请教jsp问题!!!!!!
- 怎样提高系统访问速度?
- 门户技术选型问题, cms 二次开发
- struts2 问题求教
- ■■由于jdk1.3不支持ReplaceAll函数,我自编了一个这函数,但是有些问题,大家给看看。有实例■■
- {200分}利用ResultSetMetaData meta1=rs2.getMetaData(),取表信息int y=meta1.getColumnCount(),出现怪问题?前面可以,往后就不行
- 在windows下开发jsp,但数据库是PostgreSQL,如何处理?
- 这个问题看看怎么解决?
- JSP中Servlet过滤问题
if(conn!=null){
conn.close();
}
应该改成
if(conn!=null && !conn.closed()){
conn.close();
}
不合法的操作空的返回结果集
conn.close();
}
应改为:
if(conn!=null){
conn.close();
conn=null;
}
但是到底是哪里出问题.要怎么修改呢.
请各位高人说的详细一点好不好?
第二个类指的是什么?
resultset 与StringBuffer的定义有什么冲突吗?
谢谢
我这个问题出在远程空间的TOMCAT上.每隔一段时间远程空间就能访问.需要重启一下TOMCAT才行?
如果是resultset的定义有问题,重启TOMCAT就可以吗?
去掉rs=null,在rs.close()后面不需要再加rs=null;
是我自己画蛇添足了.
谢谢大家.