首页 >数据库 >mysql教程 >JDBC使用事务实例

JDBC使用事务实例

WBOY
WBOY原创
2016-06-07 14:50:401061浏览

包 qddx .JDBC ;导入java.sql.*; public class useTransaction { public static void main(String[] args) { // TODO 自动生成的方法存根 Connection conn = null ;语句 st = null ;准备语句 pst = null ;结果集 rs

<code class=" hljs avrasm">package qddx<span class="hljs-preprocessor">.JDBC</span><span class="hljs-comment">;</span>
import java<span class="hljs-preprocessor">.sql</span>.*<span class="hljs-comment">;</span>
public class useTransaction {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Connection conn = null<span class="hljs-comment">;</span>
        Statement <span class="hljs-keyword">st</span> = null<span class="hljs-comment">;</span>
        PreparedStatement pst = null<span class="hljs-comment">;</span>
        ResultSet rs = null<span class="hljs-comment">;</span>
        Savepoint sp = null<span class="hljs-comment">;</span>
        try{
        conn = JDBC_Connection<span class="hljs-preprocessor">.getConnection</span>()<span class="hljs-comment">;</span>
        //指定事务隔离级别
        conn<span class="hljs-preprocessor">.setTransactionIsolation</span>(conn<span class="hljs-preprocessor">.TRANSACTION</span>_READ_UNCOMMITTED)<span class="hljs-comment">;</span>
        pst = conn<span class="hljs-preprocessor">.prepareStatement</span>(<span class="hljs-string">"create table users (id smallint,username text)"</span>)<span class="hljs-comment">;</span>
        pst<span class="hljs-preprocessor">.execute</span>()<span class="hljs-comment">;</span>
        //提交事务
        conn<span class="hljs-preprocessor">.commit</span>()<span class="hljs-comment">;</span>
        pst<span class="hljs-preprocessor">.close</span>()<span class="hljs-comment">;</span>
        }catch(SQLException e){
            System<span class="hljs-preprocessor">.err</span><span class="hljs-preprocessor">.println</span>(<span class="hljs-string">"连接数据库或者建表失败"</span>)<span class="hljs-comment">;</span>
            System<span class="hljs-preprocessor">.err</span><span class="hljs-preprocessor">.println</span>(<span class="hljs-string">"事务回滚到回滚点"</span>)<span class="hljs-comment">;</span>
            try{
            conn<span class="hljs-preprocessor">.rollback</span>()<span class="hljs-comment">;</span>
            }catch(SQLException ex){
                //ex<span class="hljs-preprocessor">.printStackTrace</span>()<span class="hljs-comment">;</span>
                System<span class="hljs-preprocessor">.out</span><span class="hljs-preprocessor">.println</span>(<span class="hljs-string">"回滚失败"</span>)<span class="hljs-comment">;</span>
            }
            try{
            conn<span class="hljs-preprocessor">.setSavepoint</span>()<span class="hljs-comment">;//设置一个存储点</span>
            <span class="hljs-keyword">st</span> = conn<span class="hljs-preprocessor">.createStatement</span>()<span class="hljs-comment">;</span>
            <span class="hljs-keyword">st</span><span class="hljs-preprocessor">.executeUpdate</span>(<span class="hljs-string">"insert into users values(110,'Janes')"</span>)<span class="hljs-comment">;//执行更新语句</span>
            //<span class="hljs-keyword">st</span><span class="hljs-preprocessor">.executeUpdate</span>(<span class="hljs-string">"insert into users values('shibai','Janes')"</span>)<span class="hljs-comment">;//执行更新语句 失败的例子</span>
            conn<span class="hljs-preprocessor">.commit</span>()<span class="hljs-comment">;//提交事务</span>
            conn<span class="hljs-preprocessor">.releaseSavepoint</span>(sp)<span class="hljs-comment">;//释放存储点</span>
            <span class="hljs-keyword">st</span><span class="hljs-preprocessor">.close</span>()<span class="hljs-comment">;</span>
            conn<span class="hljs-preprocessor">.close</span>()<span class="hljs-comment">;</span>

            }catch(SQLException et){
                System<span class="hljs-preprocessor">.err</span><span class="hljs-preprocessor">.println</span>(<span class="hljs-string">"操作失败"</span>)<span class="hljs-comment">;</span>
                System<span class="hljs-preprocessor">.err</span><span class="hljs-preprocessor">.println</span>(<span class="hljs-string">"事务回滚到存储点"</span>)<span class="hljs-comment">;</span>
                try{
                conn<span class="hljs-preprocessor">.rollback</span>(sp)<span class="hljs-comment">;</span>
                <span class="hljs-keyword">st</span><span class="hljs-preprocessor">.close</span>()<span class="hljs-comment">;</span>
                conn<span class="hljs-preprocessor">.close</span>()<span class="hljs-comment">;</span>
                }catch(SQLException exc){
                    System<span class="hljs-preprocessor">.out</span><span class="hljs-preprocessor">.println</span>(<span class="hljs-string">"回滚到存储点失败"</span>)<span class="hljs-comment">;</span>
                    //exc<span class="hljs-preprocessor">.printStackTrace</span>()<span class="hljs-comment">;;</span>
                }
                //et<span class="hljs-preprocessor">.printStackTrace</span>()<span class="hljs-comment">;</span>
            }
            //e<span class="hljs-preprocessor">.printStackTrace</span>()<span class="hljs-comment">;</span>
        }

    }

}
</code>
声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn