Heim >Datenbank >MySQL-Tutorial >JDBC使用事务实例

JDBC使用事务实例

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-07 14:50:401075Durchsuche

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>
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn