JDBC设置回滚点

有的时候操作数据库需要先删除一个数据表的数据,然后再插入另一个表的数据,这个时候就需要使用回滚点,示例代码如下

//添加一个新的行业类型
	public int addSupplierDetailModelEnt(List<String> newName,List<String> newSelect,List<String> minLength,List<String> maxLength,List<String> chooseType,List<String> newDescribe,int Relation_Company_id) throws SQLException {
	    Savepoint sp = null;  
	    System.out.println("addSupplierDetailModelEnt begin!");
	    System.out.println("Relation_Company_id : "+Relation_Company_id);
	    //删除原来的数据	
	    String basicInfoSql2 = "delete from supplier_detail_model where Relation_Company_id = ? ";
	    	// 插入一条数据
		String basicInfoSql = "insert into supplier_detail_model(Field_name,Field_type,minLength,maxLength,chooseType,newDescribe,Relation_Company_id) values(?,?,?,?,?,?,?)";
		conn = bdConn.getConnection();
		int[] count = new int[newName.size()];
		try {
		    conn.setAutoCommit(false); 
		    pstm = conn.prepareStatement(basicInfoSql2);
			pstm.setInt(1, Relation_Company_id);
			pstm.executeUpdate();
			sp = conn.setSavepoint();    //设置回滚点  
		    pstm = conn.prepareStatement(basicInfoSql);
		    for(int i=0;i<newName.size();i++){
			System.out.println(i);
			pstm.setString(1, newName.get(i));
			pstm.setString(2, newSelect.get(i));
			pstm.setString(3, minLength.get(i));
			pstm.setString(4, maxLength.get(i));
			pstm.setString(5, chooseType.get(i)==null?"no":chooseType.get(i));
			pstm.setString(6, newDescribe.get(i)==null?"no":newDescribe.get(i));
			pstm.setInt(7, Relation_Company_id);
			pstm.addBatch();
		    }
			count = pstm.executeBatch();
			System.out.println("addSupplierDetailModelEnt commit!");
			conn.commit();
		} catch (Exception e) {
		    System.out.println("addSupplierDetailModelEnt rollback!");
		              conn.rollback(sp);//回滚到那个事务点上去
		              conn.commit();
		              e.printStackTrace();
		} finally {
			try {
				DBConnection.closeResultSet(rs);
			} catch (SQLException e) {
				e.printStackTrace();
			}
			try {
				DBConnection.closePtmt(pstm);
			} catch (SQLException e) {
				e.printStackTrace();
			}
			try {
				DBConnection.closeConnection(conn);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return count.length;
	}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值