JDBC使用事务实例

WBOY
リリース: 2016-06-07 14:50:40
オリジナル
1049 人が閲覧しました

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 までご連絡ください。
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート