此类问题一般由两个原因,
1、循环过程中关闭了rs
解决此问题,只要将关闭连接放到循环外即可。
2、循环中的其他操作也会关闭rs,例如:
DB_function_bean dfb = new DB_function_bean();
String sql = "select ID from t_notice";
ResultSet rs = dfb.executeQuery(sql1);
while(rs.next())
{
String N_ID = rs2.getString(1);
String sql2 = "insert into t_user_notice(USER_ID,NOTICE_ID) VALUES('"+U_ID+"','"+N_ID+"')";
dfb.execute(sql2);
}
执行dfb.execute(sql2)会关闭rs,解决办法是用一个新的连接执行execute(sql2),即:
DB_function_bean dfb = new DB_function_bean();
DB_function_bean dfb1 = new DB_function_bean();
String sql = "select ID from t_notice";
ResultSet rs = dfb.executeQuery(sql1);
while(rs.next())
{
String N_ID = rs2.getString(1);
String sql2 = "insert into t_user_notice(USER_ID,NOTICE_ID) VALUES('"+U_ID+"','"+N_ID+"')";
dfb1.execute(sql2);
}
1、循环过程中关闭了rs
解决此问题,只要将关闭连接放到循环外即可。
2、循环中的其他操作也会关闭rs,例如:
DB_function_bean dfb = new DB_function_bean();
String sql = "select ID from t_notice";
ResultSet rs = dfb.executeQuery(sql1);
while(rs.next())
{
String N_ID = rs2.getString(1);
String sql2 = "insert into t_user_notice(USER_ID,NOTICE_ID) VALUES('"+U_ID+"','"+N_ID+"')";
dfb.execute(sql2);
}
执行dfb.execute(sql2)会关闭rs,解决办法是用一个新的连接执行execute(sql2),即:
DB_function_bean dfb = new DB_function_bean();
DB_function_bean dfb1 = new DB_function_bean();
String sql = "select ID from t_notice";
ResultSet rs = dfb.executeQuery(sql1);
while(rs.next())
{
String N_ID = rs2.getString(1);
String sql2 = "insert into t_user_notice(USER_ID,NOTICE_ID) VALUES('"+U_ID+"','"+N_ID+"')";
dfb1.execute(sql2);
}