> 데이터 베이스 > MySQL 튜토리얼 > JDBC使用事务实例

JDBC使用事务实例

WBOY
풀어 주다: 2016-06-07 14:50:40
원래의
1048명이 탐색했습니다.

package qddx .JDBC ; import java .sql .* ; public class useTransaction { public static void main(String[] args) { // TODO Auto-generated method stub Connection conn = null ; Statement st = null ; PreparedStatement pst = null ; ResultSet 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>
로그인 후 복사
관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿