//一次执行多条SQL,如果传来的SQL如果有一条不成功将显示失败
//Arraylist 为已封装的SQL语句
//sMsg 为信息提示。当执行出问题时,输出相应的信息。(如:输入类名方法名,出错时可以看到出问题的方法名称)
public boolean exeManySQL(ArrayList arr,String sMsg)
{
boolean bool=false;
int nSize=arr.size();
if(nSize>0)
{
//自定义链接数据库方法
openDB();
try {
//不自动提交
con.setAutoCommit(false);
for(int i=0;i<nSize;i++)
{
String sTempSQL=(String)arr.get(i);
//System.out.println(sTempSQL);
st.addBatch(sTempSQL);
}
// System.out.println("开始执行了...");
int [] numArr=st.executeBatch();
// System.out.println("numArr长度为:"+numArr.length);
//如果有一条出错时就执行回滚
if(numArr.length==nSize)
{
con.commit();//提交
bool=true;
}else
{
con.rollback();
//回滚
}
}
catch (Exception ex) {
tempBean.systemOutException(sMsg);
tempBean.systemOutException("执行多条SQL语句时出错啦!");
Debugger.printStackTrace(ex);
try {
con.rollback();
//回滚
}
catch (SQLException ex1) {
if(arr.size()>0)
{
sMsg=(String)arr.get(0);
}
tempBean.systemOutException("执行多条SQL语句回滚时出错了! ");
tempBean.systemOutException(ex1.getMessage());
Debugger.printStackTrace(ex1);
}
}
finally
{
closeDB();
}
}
return bool;
}