List of usage examples for java.sql Connection rollback
void rollback() throws SQLException;
Connection
object. From source file:Main.java
public static void main(String[] args) throws Exception { Connection conn = getHSQLConnection(); conn.setAutoCommit(false); // start a transaction Statement st = conn.createStatement(); st.executeUpdate("create table survey (id int,name varchar(30));"); st.executeUpdate("insert into survey (id,name ) values (1,'nameValue')"); st.executeUpdate("insert into survey (id,name ) values (2,'nameValue')"); conn.rollback(); // the preceding inserts will not commit; st.executeUpdate("INSERT INTO survey(id, name) VALUES('33', 'jeff')"); conn.commit(); // end the transaction st = conn.createStatement();/*from ww w.ja v a 2 s . c o m*/ ResultSet rs = st.executeQuery("SELECT * FROM survey"); outputResultSet(rs); rs.close(); st.close(); conn.close(); }
From source file:jp.co.opentone.bsol.linkbinder.CorresponBodyUpdater.java
/** * @param args/*ww w . j a va2 s . co m*/ */ public static void main(String[] args) throws Exception { Connection con = getConnection(); PreparedStatement stmt = null; boolean success = true; try { con.setAutoCommit(false); stmt = con.prepareStatement("UPDATE correspon SET body = ? WHERE id = ?"); execute(stmt); success = true; } finally { if (success) { con.commit(); } else { con.rollback(); } if (stmt != null) stmt.close(); con.close(); } }
From source file:nl.strohalm.cyclos.setup.migrations.version3_5.LoanCustomFieldMigration.java
public static void main(final String[] args) throws Exception { Class.forName("com.mysql.jdbc.Driver"); final Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/cyclos3_new", "root", ""); try {//from w w w .j av a 2s . co m conn.setAutoCommit(false); final JDBCWrapper jdbc = new JDBCWrapper(conn); new LoanCustomFieldMigration().execute(jdbc); conn.commit(); } catch (final Exception e) { conn.rollback(); throw e; } finally { conn.close(); } }
From source file:Main.java
public static void main(String[] args) throws Exception { Connection conn = getHSQLConnection(); conn.setAutoCommit(false);/*from w ww . ja va 2s . c o m*/ Statement st = conn.createStatement(); try { st.executeUpdate("create table survey (id int,name varchar(30));"); st.executeUpdate("insert into survey (id,name ) values (1,'nameValue')"); st.executeUpdate("insert into survey (id,name ) values (2,'nameValue')"); // commits all the transactions conn.commit(); } catch (Exception e) { //cancel (roll back) all the transactions conn.rollback(); // to see what went wrong e.printStackTrace(); } st = conn.createStatement(); ResultSet rs = st.executeQuery("SELECT * FROM survey"); outputResultSet(rs); rs.close(); st.close(); conn.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { Connection conn = getHSQLConnection(); conn.setAutoCommit(false);//w w w.j a v a 2 s . c o m Statement st = conn.createStatement(); try { st.executeUpdate("create table survey (id int,name varchar(30));"); st.executeUpdate("insert into survey (id,name ) values (1,'nameValue')"); st.executeUpdate("insert into survey (id,name ) values (2,'nameValue')"); // commits all the transactions conn.commit(); } catch (Exception e) { // cancel (roll back) all the transactions conn.rollback(); // to see what went wrong e.printStackTrace(); } st = conn.createStatement(); ResultSet rs = st.executeQuery("SELECT * FROM survey"); outputResultSet(rs); rs.close(); st.close(); conn.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { Connection connection = null; try {/* w ww . j ava 2 s . c o m*/ Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); connection = DriverManager.getConnection("jdbc:sqlserver://MYSERVER;databaseName=MYDATABASE", "USERID", "PASSWORD"); connection.setAutoCommit(false); Statement statement = connection.createStatement(); statement.executeUpdate("UPDATE Table1 SET Value = 1 WHERE Name = 'foo'"); statement.executeUpdate("UPDATE Table2 SET Value = 2 WHERE Name = 'bar'"); connection.commit(); } catch (SQLException ex) { connection.rollback(); } }
From source file:Main.java
public static void main(String[] args) throws Exception { Connection conn = getConnection(); conn.setAutoCommit(false);/*ww w . j av a 2s . co m*/ Statement st = conn.createStatement(); st.executeUpdate("create table survey (id int, name VARCHAR(30) );"); String INSERT_RECORD = "insert into survey(id, name) values(?,?)"; PreparedStatement pstmt = conn.prepareStatement(INSERT_RECORD); pstmt.setString(1, "1"); pstmt.setString(2, "name1"); pstmt.addBatch(); pstmt.setString(1, "2"); pstmt.setString(2, "name2"); pstmt.addBatch(); try { // execute the batch int[] updateCounts = pstmt.executeBatch(); } catch (BatchUpdateException e) { int[] updateCounts = e.getUpdateCounts(); checkUpdateCounts(updateCounts); try { conn.rollback(); } catch (Exception e2) { e.printStackTrace(); System.exit(1); } } // since there were no errors, commit conn.commit(); ResultSet rs = st.executeQuery("SELECT * FROM survey"); outputResultSet(rs); rs.close(); st.close(); conn.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { String url = "jdbc:mysql://localhost/testdb"; String username = "root"; String password = ""; Class.forName("com.mysql.jdbc.Driver"); Connection conn = null; try {/* ww w . j a v a2s . c o m*/ conn = DriverManager.getConnection(url, username, password); conn.setAutoCommit(false); Statement st = conn.createStatement(); st.execute("INSERT INTO orders (username, order_date) VALUES ('java', '2007-12-13')", Statement.RETURN_GENERATED_KEYS); ResultSet keys = st.getGeneratedKeys(); int id = 1; while (keys.next()) { id = keys.getInt(1); } PreparedStatement pst = conn.prepareStatement( "INSERT INTO order_details (order_id, product_id, quantity, price) VALUES (?, ?, ?, ?)"); pst.setInt(1, id); pst.setString(2, "1"); pst.setInt(3, 10); pst.setDouble(4, 100); pst.execute(); conn.commit(); System.out.println("Transaction commit..."); } catch (SQLException e) { if (conn != null) { conn.rollback(); System.out.println("Connection rollback..."); } e.printStackTrace(); } finally { if (conn != null && !conn.isClosed()) { conn.close(); } } }
From source file:Main.java
public static void main(String[] args) { Connection conn = null; Statement stmt = null;/* ww w . j a va2 s. com*/ boolean executeResult; try { String driver = "oracle.jdbc.driver.OracleDriver"; Class.forName(driver).newInstance(); System.out.println("Connecting to database..."); String jdbcUrl = "jdbc:oracle:thin:@localhost:1521:ORCL"; conn = DriverManager.getConnection(jdbcUrl, "yourName", "mypwd"); stmt = conn.createStatement(); conn.setAutoCommit(false); if (!conn.getAutoCommit()) System.out.println("Auto-commit is set to false"); String sql = "INSERT INTO Location VALUES(715,'Houston')"; stmt.executeUpdate(sql); sql = "INSERT INTO Employees VALUES" + "(8,'K','4351',{d '2000-02-00'},715)"; stmt.executeUpdate(sql); conn.commit(); } catch (SQLException se) { String msg = se.getMessage(); msg = "SQLException occured with message: " + msg; System.out.println(msg); System.out.println("Starting rollback operations..."); try { conn.rollback(); } catch (SQLException se2) { se2.printStackTrace(); } } catch (Exception e) { e.printStackTrace(); } }
From source file:UpdateLogic.java
public static void main(String args[]) { Connection con = null; if (args.length != 2) { System.out.println("Syntax: <java UpdateLogic [number] [string]>"); return;/*from w w w.j a v a 2 s.co m*/ } try { String driver = "com.imaginary.sql.msql.MsqlDriver"; Class.forName(driver).newInstance(); String url = "jdbc:msql://carthage.imaginary.com/ora"; Statement s; con = DriverManager.getConnection(url, "borg", ""); con.setAutoCommit(false); // make sure auto commit is off! s = con.createStatement();// create the first statement s.executeUpdate("INSERT INTO test (test_id, test_val) " + "VALUES(" + args[0] + ", '" + args[1] + "')"); s.close(); // close the first statement s = con.createStatement(); // create the second statement s.executeUpdate("INSERT into test_desc (test_id, test_desc) " + "VALUES(" + args[0] + ", 'This describes the test.')"); con.commit(); // commit the two statements System.out.println("Insert succeeded."); s.close(); // close the second statement } catch (Exception e) { if (con != null) { try { con.rollback(); } // rollback on error catch (SQLException e2) { } } e.printStackTrace(); } finally { if (con != null) { try { con.close(); } catch (SQLException e) { e.printStackTrace(); } } } }