I am using UCanAccess JDBC-driver (version 3.0.3.1) to connect to the mdb-file. And I need to add the column to existing table. The problem is that the statement
Ucanaccess can't support this very requested feature until the underlying jackcess library doesn't support it. We (Ucanaccess team) could automate the above mentioned steps, but for the same reason, we can't create fk and so, in many cases, reproduce an exact copy of an original table. While altering a table, we would lose the referential integrity constraints... so, for now, it's better to do nothing. So sorry, no solution currently.
UCanAccess versions 4.0.0 and above now support ALTER TABLE, e.g.,
Statement stmt = conn.createStatement();
stmt.execute("ALTER TABLE TableName ADD COLUMN newCol LONG");
UCanAccess now supports ALTER TABLE. See my other answer to this question.
If your Java app is running under Windows then you can use the following workaround. It creates a little VBScript and invokes CSCRIPT.EXE to run it
String dbFileSpec = "C:\\Users\\Public\\mdbTest.mdb";
// write a temporary VBScript file ...
File vbsFile = File.createTempFile("AlterTable", ".vbs");
vbsFile.deleteOnExit();
PrintWriter pw = new PrintWriter(vbsFile);
pw.println("Set conn = CreateObject(\"ADODB.Connection\")");
pw.println("conn.Open \"Driver={Microsoft Access Driver (*.mdb)};Dbq=" + dbFileSpec + "\"");
pw.println("conn.Execute \"ALTER TABLE TEmployee ADD COLUMN NotificationsEnabled YESNO\"");
pw.println("conn.Close");
pw.println("Set conn = Nothing");
pw.close();
// ... and execute it
Process p = Runtime.getRuntime().exec("CSCRIPT.EXE \"" + vbsFile.getAbsolutePath() + "\"");
p.waitFor();
BufferedReader rdr =
new BufferedReader(new InputStreamReader(p.getErrorStream()));
int errorLines = 0;
String line = rdr.readLine();
while (line != null) {
errorLines++;
System.out.println(line); // display error line(s), if any
line = rdr.readLine();
}
if (errorLines == 0) {
System.out.println("The operation completed successfully.");
}
Notes:
The above code will work for updating an .mdb file if the Java app is running under a 32-bit JVM. If you need to update an .accdb file or if the Java app is running under a 64-bit JVM then the appropriate version of Microsoft's Access Database Engine (32-bit or 64-bit, same as the JVM) will have to be installed and you will need to use Driver={Microsoft Access Driver (*.mdb, *.accdb)}
.
This workaround uses ODBC but it does not use Java's JDBC-ODBC Bridge, so it will work with Java 8.