//处理投票信息
public int doVote(ArrayList voteInfo) {
int ret = 0;
//分别组织对三道题目做更改的预编译sql语句
/*String[] sqlC = {
"update book set ? = ? + 1",
"update lang set ? = ? + 1",
"update borrow set ? = ? + 1"
};*/
try {
if(voteInfo.size() != 0) {
con = db.getCon();
//
设置手动事务处理模式
con.setAutoCommit(false);
//每一道题目做一个事务处理
for(int i = 0; i < voteInfo.size(); i++) {
PreparedStatement[] ps = {con.prepareStatement(sqlC[i].toString())};
//转换成字符串数组
String[] voteResult;
voteResult = (String[])voteInfo.get(i);
//将预编译好的sql语句添加到批处理当中去
for(int j = 0; j < voteResult.length; j++ ){
ps[i].setString(1, voteResult[j].toString());
ps[i].setString(2, voteResult[j].toString());
ps[i].addBatch();
}
//提交事务并返回所更新的影响行数
ret += ps[i].executeBatch().length;
}
con.commit(); //切记切记要提交
con.setAutoCommit(true);
}
}catch(SQLException se){
try {
if(con != null) {
//在这里一定要记着回滚事务处理
con.rollback();
////warning!!!!
con.setAutoCommit(true);
////warning!!!!
}
} catch(Exception e) {
System.out.println(e.getMessage());
}
se.printStackTrace();
}catch(Exception ex) {
ex.printStackTrace();
}finally {
db.close(con);
}
return ret;
}
过程就略了,简单说就是一个用PreparedStatement进行循环事务处理方法
用来进行更改在线投票系统的update事务处理.在客户端获得一系列的
checkBox值,由ArrayList封装传入此方法进行处理,比如调查几道读者喜欢
的哪些书问卷调查吧,
问题:
为什么总是在第二次运行时st[i].addBatch(sql);时出现如下的异常?
PreparedStatement是否可以这样用??
是在lomboz eclipse3.2 + tomcat5.5环境调试,
异常代码如下;
Server startup in 3594 ms
0java.sql.SQLException: [Microsoft][SQLServer JDBC Driver]Can't start a cloned connection while in manual transaction mode.
at com.microsoft.jdbc.base.BaseExceptions.getException(Unknown Source)
at com.microsoft.jdbc.base.BaseConnection.getImplConnection(Unknown Source)
at com.microsoft.jdbc.base.BaseStatement.getImplConnection(Unknown Source)
at com.microsoft.jdbc.base.BaseStatement.getImplStatement(Unknown Source)
at com.microsoft.jdbc.base.BaseStatement.addBatch(Unknown Source)
at bean.DbMgr.doVote(DbMgr.java:73)
at contr.VoteServlet.doGet(VoteServlet.java:25)
at contr.VoteServlet.doPost(VoteServlet.java:33)
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 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:595)
求大神指教!~~