jdbc学习:jdbc事务操作

package test;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Savepoint;

import org.junit.Test;

import util.DbHelper;

public class TestTranscational {
    public static void main(String[] args) {
        testpoint();
    }

    /**
     * @Title: test
     * @Description: TODO(这里用一句话描述这个方法的作用)
     * @param 设定文件
     * @return void 返回类型
     * @throws
     */
    @Test
    public  void test() {
        Connection conn = null;
        ResultSet rs = null;
        PreparedStatement ps = null;
        try {
            conn = DbHelper.getConnection();
            conn.setAutoCommit(false);
            String sql = "update wangcc_user set salary=salary+100 where id=?";
            ps = conn.prepareStatement(sql);
            ps.setInt(1, 1);
            ps.executeUpdate();
            sql = "select salary from wangcc_user where id=?";
            ps = conn.prepareStatement(sql);
            ps.setInt(1, 2);
            rs = ps.executeQuery();
            float salary = 0.0f;
            if (rs.next()) {
                salary = rs.getFloat(1);

            }
            if (salary > 20000) {
                throw new RuntimeException("已超过最大值");
            }
            System.out.println("ddd");
            conn.commit();

        } catch (SQLException e) {
            // TODO: handle exception
        } finally {
            DbHelper.free(rs, ps, conn);
        }
    }

    @Test
    public static void testpoint() {
        Connection conn = null;
        ResultSet rs = null;
        PreparedStatement ps = null;
        Savepoint sp = null;
        try {
            conn = DbHelper.getConnection();
            conn.setAutoCommit(false);
            String sql = "update wangcc_user set salary=salary+100 where id=?";
            ps = conn.prepareStatement(sql);
            ps.setInt(1, 1);
            ps.executeUpdate();
            sp = conn.setSavepoint();
            sql = "update wangcc_user set salary=salary+100 where id=3";
            ps = conn.prepareStatement(sql);
            ps.executeUpdate();
            sql = "select salary from wangcc_user where id=?";
            ps = conn.prepareStatement(sql);
            ps.setInt(1, 2);
            rs = ps.executeQuery();
            float salary = 0.0f;
            if (rs.next()) {
                salary = rs.getFloat(1);

            }
            if (salary > 20000) {
                throw new RuntimeException("已超过最大值");
            }

            System.out.println("ddd");
            sql = "update wangcc_user set salary=salary+100 where id=2";
            ps = conn.prepareStatement(sql);
            ps.executeUpdate();
            conn.commit();

        } catch (SQLException e) {
            // TODO: handle exception
            if (conn != null && sp != null) {
                try {
                    conn.rollback(sp);
                    conn.commit();
                } catch (SQLException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }

            }
        } finally {
            DbHelper.free(rs, ps, conn);
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值