Java-Access Bug


I've recently discovered a bug when writing JDBC programs to insert records into an Access database. Apparently, the last INSERT never gets committed. Calling the executeUpdate() method of a Statement using an INSERT query will commit only the previous INSERT.

To solve the problem, set auto-commit to false for the connection. Then, after each executeUpdate() call, do a commit.

Example (the changes required are in red):


import java.sql.*;

public class DbM {

	private final String url = "jdbc:odbc:CIS135";
	private final String cls = "sun.jdbc.odbc.JdbcOdbcDriver";
	private Connection con = null;
	private Statement stm = null;

	public DbM() throws ClassNotFoundException, SQLException {
		Class.forName(cls);
		con = DriverManager.getConnection(url);
		con.setAutoCommit(false);
		stm = con.createStatement();
	}
	public ResultSet getResultSet(String sql) throws SQLException {
		ResultSet rs = stm.executeQuery(sql);
		return rs;
	}
	public int updateRecords(String sql) throws SQLException {
		int iRowsAffected = stm.executeUpdate(sql);
		con.commit();
		return iRowsAffected;
	}
}