我在用jdbc开发web时,为什么报Operation not allowed after ResultSet closed错误。
我有一个数据库工具类Mydb.java,它的代码如下:
public class Mydb {
private Connection conn = null;
private Statement stmt = null;
private ResultSet rs = null;
private PreparedStatement pstmt = null;
/**
* 这个方法不对外提供,除了这个类以外的都不需要调用
*/
private Connection getConn() {
try {
String driverClassName=Env.getInstance().getProperty("driver");
String url=Env.getInstance().getProperty("url");
String password=Env.getInstance().getProperty("password");
String user=Env.getInstance().getProperty("user");
ComboPooledDataSource cpds = new ComboPooledDataSource();
cpds.setDriverClass(driverClassName);
cpds.setJdbcUrl(url);
cpds.setUser(user);
cpds.setPassword(password);
cpds.setMaxStatements(180);
cpds.setMaxPoolSize(100);
conn=cpds.getConnection();
/*
Class.forName(driverClassName);
conn= DriverManager.getConnection(url,user,password);
*/
stmt = conn.createStatement();
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
public ResultSet getResultSet(String sql) {
try {
if(conn==null){
conn = getConn();
}
rs = stmt.executeQuery(sql);//第68行
} catch (SQLException e) {
e.printStackTrace();
}
return rs;
}
public int executeUpdate(String sql) {
int i=0;
try {
if(conn==null){
conn = getConn();
}
i=stmt.executeUpdate(sql);
} catch (SQLException e) {
e.printStackTrace();
}finally{
close();
}
return i;
}
public int execute(String sql) {
int i=0;
try {
if(conn==null){
conn = getConn();
}
i=stmt.executeUpdate(sql);
} catch (SQLException e) {
e.printStackTrace();
}
return i;
}
public void close() {
try {
if(rs != null) {
rs.close();
rs = null;
}
if(stmt != null) {
stmt.close();
stmt = null;
}
if(conn != null) {
conn.close();
conn = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
但是我在另外一下类中写了一个方法,该方法中的代码如下:
public void run() {
System.out.println("系统自动检测是否存在应放入公海中的信息!");
int tel=0;//参数一
int sm1=0;//参数二
int sm2=0;//参数三
String rq1="";
String rq2="";
String rq3="";
String rq="";
int id=0;
String gh="";
String oldywy="";
String yywy="";
String sql="";
Mydb db=new Mydb();
ResultSet r=db.getResultSet("select tel,sm1,sm2 from t_cansubiao");
try {
if(r.next()){
tel=r.getInt("tel");
sm1=r.getInt("sm1");
sm2=r.getInt("sm2");
}
rq1=ShiJian.Jianhour(tel);
rq2=ShiJian.Jianday(sm1);
rq3=ShiJian.Jianday(sm2);
sql="select t_customer.id,t_customer.lxywy,t_user.username,t_customer.yywy from t_customer ,t_user where (t_customer.lxywy=t_user.gh) and ( t_customer.lx='客户' and t_customer.dhhfsj is null and t_customer.lrsj<'"+rq1+"') or ( t_customer.lx='客户' and t_customer.dhhfsj<>'' and t_customer.lrsj<'"+rq2+"') or ( t_customer.lx='客户' and t_customer.smhfsj<>'' and t_customer.smhfsj<'"+rq3+"')";
System.out.println("检测语句="+sql);
ResultSet rs=db.getResultSet(sql);
System.out.println("1============");
while(rs.next()){
id=rs.getInt("t_customer.id");//客户id号
gh=rs.getString("t_customer.lxywy");//联系业务员
yywy=rs.getString("t_customer.yywy")+"|"+rs.getString("t_user.username");
System.out.println("2============");
//存入联系过的表中
db.execute("insert t_lianxiguodekehu(khid,ywy) values('"+id+"','"+gh+"')");
//System.out.println("---------------------------");
//查到了,修改其类型,改为公海状态
System.out.println("3============");
db.execute("update t_customer set lx='公海' , lxywy='' , yywy='"+yywy+"' where id='"+id+"'");
System.out.println("4============");
}
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}finally{
db.close();
}
}
出现的错误,为什么只循环了一次,就报错误:java.sql.SQLException: Operation not allowed after ResultSet closed
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:910)
at com.mysql.jdbc.ResultSet.checkClosed(ResultSet.java:666)
at com.mysql.jdbc.ResultSet.next(ResultSet.java:7274)
at com.mchange.v2.c3p0.impl.NewProxyResultSet.next(NewProxyResultSet.java:3066)
at com.jykj.util.AdcTimerTask.run(AdcTimerTask.java:53)
请问应如何解决,给个详细的解决方案