DBMNG数据库管理与应用

所谓独创的能力,就是经过深思的模仿。
当前位置:首页 > 经验分享 > Java组件

java原生代码实现JDBC使用事务示例

下面代码演示如何使用JDBC的事务。JDBC事务操作需要在执行操作之前调用Connection类的setAutoCommit(false)方法。

在执行完操作之后,需要调用Connection实例的commit()方法来提交事务。

下面是示例代码:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

/**
 *
 * @author outofmemory.cn
 */
public class Main {

    /**
     * 事务使用示例
     */
    public void updateDatabaseWithTransaction() {

        Connection connection = null;
        Statement statement = null;

        try {
            Class.forName("[nameOfDriver]");

            connection = DriverManager.getConnection("[databaseURL]",
                    "[userid]",
                    "[password]");

            //此处调用setAutoCommit(false)指定要在事务中提交
            connection.setAutoCommit(false);

            statement = connection.createStatement();

            //Execute the queries
            statement.executeUpdate("UPDATE Table1 SET Value = 1 WHERE Name = 'foo'");
            statement.executeUpdate("UPDATE Table2 SET Value = 2 WHERE Name = 'bar'");

            //No changes has been made in the database yet, so now we will commit
            //the changes.
            connection.commit();

        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        } catch (SQLException ex) {
            ex.printStackTrace();

            try {
                //An error occured so we rollback the changes.
                connection.rollback();
            } catch (SQLException ex1) {
                ex1.printStackTrace();
            }
        } finally {
            try {
                if (statement != null)
                    statement.close();
                if (connection != null)
                    connection.close();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }

    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        new Main().updateDatabaseWithTransaction();

    }
}
本站文章内容,部分来自于互联网,若侵犯了您的权益,请致邮件chuanghui423#sohu.com(请将#换为@)联系,我们会尽快核实后删除。
Copyright © 2006-2023 DBMNG.COM All Rights Reserved. Powered by DEVSOARTECH            豫ICP备11002312号-2

豫公网安备 41010502002439号