看了1个小时,写了1个小时,哎,第一次写事务就出这问题,那个大哥帮帮忙。
问题说明:conn是连结对象conn.setAutoCommit(false);//开始事务
conn.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
pstmtEmp = conn.prepareStatement(strAddEmpSql);//得到一个预处理,strAddEmpSql是SQl预处理字符串
//然后在用一个预处理,时在这里就有错误,  说是什么不能克隆
pstmtPower = conn.prepareStatement( strAddEmpPowerSql );//又是不个预处理,错误!!!!strAddEmpPowerSql是SQl预处理字符串最后就是conn.commit();
--------------------------------------------
这是源码
{
EmpViewBean empViewVo = (EmpViewBean) beanVo; EmpBean empVo = null;
Vector powerAllVo = null; Connection conn = null;
PreparedStatement pstmtEmp = null;
PreparedStatement pstmtPower = null; try {
empVo = empViewVo.getEmpVo();
powerAllVo = empViewVo.getPowerVo();
conn = DBConnection.getConnection();

// conn.setAutoCommit(false);//开始事务
// conn.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE); String strAddEmpSql = "insert into emp values( ?, ?, ?, ?, ?, CONVERT(varchar,getdate(),20) )"; pstmtEmp = conn.prepareStatement(strAddEmpSql);
pstmtEmp.setInt(1, empVo.getEmpId() );
pstmtEmp.setString(2, empVo.getEmpName());
pstmtEmp.setString(3, empVo.getEmpPass());
pstmtEmp.setInt(4, empVo.getEmpSex());
pstmtEmp.setInt(5, empVo.getEmpLevelId());
int b = pstmtEmp.executeUpdate();
if (b != 1) {
return false;
}
System.out.println("emp intser");
for (int i = 0; i < powerAllVo.size(); i++) {
PowerBean powerVo = (PowerBean) powerAllVo.elementAt(i);
String strAddEmpPowerSql = "insert into emp_power values( ?, ? )";
pstmtPower = conn.prepareStatement( strAddEmpPowerSql );
pstmtPower.setInt(1, empVo.getEmpId());
pstmtPower.setInt(2, powerVo.getPowerId());
int b1 = pstmtPower.executeUpdate();
if (b1 != 1) {
return false;
}
}
// conn.commit();
System.out.println("return add Ok!!!!!");
return true;//返回 } catch (Exception e) {
// TODO: handle exception
// try {
// conn.rollback();
// } catch (SQLException e1) {
// // TODO 自动生成 catch 块
// System.out.println("EmpOperator.add3:" + e.getMessage());
// e1.printStackTrace();
// }
System.out.println("EmpOperator.add1:" + e.getMessage());
e.printStackTrace();
} finally {
try {
if (pstmtPower != null) {
pstmtPower.close();
}
if (pstmtEmp != null) {
pstmtEmp.close();
}
if (conn != null) {
conn.close();
}
} catch (Exception e) {
// TODO: handle exception
System.out.println("EmpOperator.add2:" + e.getMessage());
e.printStackTrace();
}
} return false; }