提交/回滚事务例子,提交滚事务例子,package cn.o
分享于 点击 8464 次 点评:224
提交/回滚事务例子,提交滚事务例子,package cn.o
package cn.outofmemory.snippets.core;import java.sql.Connection;import java.sql.DriverManager;import java.sql.SQLException;public class CommitAndRollback { public static void main(String[] args) { Connection connection = null; try { // Load the Oracle JDBC driver String driverName = "oracle.jdbc.driver.OracleDriver"; Class.forName(driverName); // Create a connection to the database String serverName = "localhost"; String serverPort = "1521"; String sid = "mySchema"; String url = "jdbc:oracle:thin:@" + serverName + ":" + serverPort + ":" + sid; String username = "username"; String password = "password"; connection = DriverManager.getConnection(url, username, password); System.out.println("Successfully Connected to the database!"); } catch (ClassNotFoundException e) { System.out.println("Could not find the database driver " + e.getMessage()); } catch (SQLException e) { System.out.println("Could not connect to the database " + e.getMessage()); } try { // Disable auto commit connection.setAutoCommit(false); // Do SQL updates... // Commit updates connection.commit(); System.out.println("Successfully commited changes to the database!"); } catch (SQLException e) { try { // Rollback update connection.rollback(); System.out.println("Successfully rolled back changes from the database!"); } catch (SQLException e1) { System.out.println("Could not rollback updates " + e1.getMessage()); } } }}
输出:
Successfully Connected to the database!Successfully commited changes to the database!
用户点评