Hi all,
I am an H2 newbie and am trying to create a database that can commit 
multiple SQL statements as one transaction. To do that I've set the 
connection to not auto-commit, but it appears that updates are being 
committed anyway. Example code is below.

Thanks for your help ...
-deech

// In this code I've inserted the string "hello" into the table TEST and 
then updated the 
// column to "goodbye" without commiting it. However the output is 
"goodbye". 

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


public class UpdateTest {
    private Connection conn;
    
    private Connection createConnection () throws SQLException, 
ClassNotFoundException {
        Class<?> t = Class.forName("org.h2.Driver");
        Connection conn = DriverManager.getConnection("jdbc:h2:mem", "sa", 
"");
        conn.setAutoCommit(false);
        return conn;
    }
    
    private void populateTable(Connection conn) throws SQLException {
        conn.createStatement().execute("INSERT INTO TEST VALUES('hello')");
    }
    
    private void updateTable(Connection conn) throws SQLException {
        Statement s = conn.createStatement();
        s.execute("UPDATE TEST SET string = 'goodbye'");
    }
    
    private void printTable(Connection conn) throws SQLException {
        StringBuilder s = new StringBuilder();
        try {
            ResultSet result = conn.createStatement().executeQuery("SELECT * 
FROM Test");
            result.next();
            System.out.println(result.getString(1));
                    }
        catch (SQLException e) {
            e.printStackTrace();
        }
    }
    
    public UpdateTest () throws SQLException, ClassNotFoundException {
        this.conn = this.createConnection();
        this.conn.createStatement().execute("DROP TABLE IF EXISTS TEST");
        this.conn.createStatement().execute("CREATE TABLE IF NOT EXISTS TEST 
(string VARCHAR)");
        this.populateTable(conn);
        this.updateTable(conn);
        this.printTable(conn);
    }
    
    public static void main (String[]args) throws SQLException, 
ClassNotFoundException{
        UpdateTest t = new UpdateTest();
    }
}

-- 
You received this message because you are subscribed to the Google Groups "H2 
Database" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/h2-database?hl=en.

Reply via email to