首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

java操作数据库有关问题

2011-12-20 
java操作数据库问题我没有关闭ResultSet,为什么会出现这样的错误?java.sql.SQLException: Operation not a

java操作数据库问题
我没有关闭ResultSet,为什么会出现这样的错误?
java.sql.SQLException: Operation not allowed after ResultSet closed
at com.mysql.jdbc.ResultSet.checkClosed(ResultSet.java:3603)
at com.mysql.jdbc.ResultSet.next(ResultSet.java:2468)
at org.apache.commons.dbcp.DelegatingResultSet.next(DelegatingResultSet.java:169)
at admin.action.selectedTeachers.execute(selectedTeachers.java:53)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:431)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at util.CharacterEncodingFilter.doFilter(CharacterEncodingFilter.java:29)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869)
at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
at java.lang.Thread.run(Thread.java:626)


程序代码:
public class selectedTeachers extends Action
{
private static final Log log = LogFactory.getLog(GetData.class);
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) 
{
ActionForward forward = null;

Statement stmt = null;  
Connection myConnection=null;
   
try
{
DataSource dataSource=getDataSource(request,"mysqlCon");
myConnection = dataSource.getConnection();
stmt = myConnection.createStatement();
}
catch(Exception e1) 
{
log.info("创建数据连接失败");
e1.printStackTrace();  
}
 
String type = request.getParameter("Action").toString();
log.info(type);
 
//为学生分配答辩评审教师

try
{
String sql="select * from information";
ResultSet rs=stmt.executeQuery(sql);

while(rs.next())
{
String sname=rs.getString("sname");
String tname=rs.getString("tname");
log.info(sname+"----------------");
log.info(tname+"----------------");

String sql1="select * from teacher where tname='"+tname+"'";
ResultSet rs2=stmt.executeQuery(sql1);

String tname1=null;
String tname2=null;


String tname3=null;
String tname4=null;
String tname5=null;
char name[]=new char[5];
int i=0;
while(rs2.next())
{
if(!rs2.getString("tname").equals(tname))
{
name[i]=rs2.getString("tname").charAt(1);
log.info(name[i]+"::::::::::::::::::::::::::::::::;");
i++;
}
if(i==5){break;}
}
rs2.first();
while(rs2.next())
{
if(!rs2.getString("tname").equals(tname))
{
name[i]=rs2.getString("tname").charAt(1);
log.info(name[i]+"::::::::::::::::::::::::::::::::;");
i++;
}
if(i==5){break;}
}


tname1=String.valueOf(name[0]); 
tname2=String.valueOf(name[1]); 
tname3=String.valueOf(name[2]); 
tname4=String.valueOf(name[3]); 
tname5=String.valueOf(name[4]); 

String sql3="insert into selectedteacher values('"+sname+"','"+tname1+"','"+tname2+"','"+tname3+"','"+tname4+"','"+tname5+"');";
 
}

}catch (Exception e) {
//e.getMessage();
e.printStackTrace();
}
 
 
 
return forward;
}
}

[解决办法]
可能是因为你在一个connection中同时执行了两个查询, 而第二个查询执行的时候, 第一个ResultSet已经销毁了.
1. 不能用一个sql查询出来么?
2. 先处理完第一个ResultSet, 把所要的值放到一个List/数组中, 然后循环再做查询.

热点排行