import java.sql.*; import com.mysql.jdbc.*; class MySQLLinuxTest { public static void main(String args[]) throws Exception { // Create a JDBC Connection to the server. (This test assumes // you've created a password for the default root account of // "root".) Class.forName("com.mysql.jdbc.Driver").newInstance(); java.sql.Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?user=root&password=root"); // Make sure that we do our best to turn off auto-commit. System.out.println("default auto-commit = "+conn.getAutoCommit()); conn.setAutoCommit(false); System.out.println("Turn off auto-commit via JDBC call : auto-commit = "+conn.getAutoCommit()); // Do the same with MySQL specific command. java.sql.PreparedStatement ps = conn.prepareStatement("SET AUTOCOMMIT=0"); ps.executeUpdate(); ps.close(); System.out.println("Turn off auto-commit via SQL statement : auto-commit = "+conn.getAutoCommit()); // Create a table we'll be using. (Normally in other DBs you // cannot issue a DDL unless auto-commit is set to true, // e.g. Sybase complains about DDL in a "multi-statement // transaction". String sql = "create table asset (id bigint not null auto_increment, original_filename varchar(250), primary key (id))"; ps = conn.prepareStatement(sql); ps.executeUpdate(); ps.close(); //conn.commit(); // Insert a row into said table. sql = "INSERT INTO asset (original_filename) VALUES (?)"; ps = conn.prepareStatement(sql); ps.setString (1, "Should.not.exist.jpg"); int rowCount = ps.executeUpdate(); System.out.println("Inserted "+rowCount+" row(s)."); conn.rollback(); conn.close(); } }