Currently I have one application in which I am able to access .mdb or .accdb file with JdbcOdbcDriver to append some data.
Class.forName(\"sun.jdbc.odbc.Jdb
It looks like there is at least one option for connecting directly to .mdb without the JdbcOdbcDriver, that that option is commercial. See here. If the setup is what you're trying to avoid, have you considered using something an embedded database like sqlite?
download jackcess library
use this library whcih will create .mdb file.Bellow is the code snipet download jackcess libraries from above location. add required jar file in class path.
`
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.sql.Types;
import com.healthmarketscience.jackcess.ColumnBuilder;
import com.healthmarketscience.jackcess.Database;
import com.healthmarketscience.jackcess.DatabaseBuilder;
import com.healthmarketscience.jackcess.Table;
import com.healthmarketscience.jackcess.TableBuilder;
public class MDBFileGenerator {
public static void main(String[] args) throws IOException, SQLException {
Database db = DatabaseBuilder.create(Database.FileFormat.V2000,
new File("new.mdb"));
Table newTable = new TableBuilder("NewTable")
.addColumn(new ColumnBuilder("a").setSQLType(Types.INTEGER))
.addColumn(new ColumnBuilder("b").setSQLType(Types.VARCHAR))
.toTable(db);
newTable.addRow(1, "foo");
}
}
`
Update for Jackcess 2.x: Databases are now created (or opened) using DatabaseBuilder
, so to create a new database file we do
import java.io.File;
import java.io.IOException;
import com.healthmarketscience.jackcess.Database;
import com.healthmarketscience.jackcess.Database.FileFormat;
import com.healthmarketscience.jackcess.DatabaseBuilder;
public class JackcessDemoMain {
public static void main(String[] args) {
String dbPath = "C:/Users/Public/newDb.accdb";
// using try-with-resources is recommended to ensure that
// the Database object will be closed properly
try (Database db = DatabaseBuilder.create(FileFormat.V2010, new File(dbPath))) {
System.out.println("The database file has been created.");
} catch (IOException ioe) {
ioe.printStackTrace(System.err);
}
}
}
Original answer for Jackcess 1.x (deprecated):
If you would like to create the “.mdb” file through java, you can use the Jackcess Java library which is one of the pure Java Library for reading from and writing to MS Access databases. Currently supporting versions include 2000-2007 I guess. Please have a look at the below example for better understanding:
package com.jackcess.lib;
import com.healthmarketscience.jackcess.ColumnBuilder;
import com.healthmarketscience.jackcess.Database;
import com.healthmarketscience.jackcess.Table;
import com.healthmarketscience.jackcess.TableBuilder;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.sql.Types;
/**
*
* @author sarath_ivan
*/
public class JackcessLibrary {
private static Database createDatabase(String databaseName) throws IOException {
return Database.create(new File(databaseName));
}
private static TableBuilder createTable(String tableName) {
return new TableBuilder(tableName);
}
public static void addColumn(Database database, TableBuilder tableName, String columnName, Types sqlType) throws SQLException, IOException {
tableName.addColumn(new ColumnBuilder(columnName).setSQLType(Types.INTEGER).toColumn()).toTable(database);
}
public static void startDatabaseProcess() throws IOException, SQLException {
String databaseName = "C:/Users/compaq/Desktop/employeedb.mdb"; // Creating an MS Access database
Database database = createDatabase(databaseName);
String tableName = "Employee"; // Creating table
Table table = createTable(tableName)
.addColumn(new ColumnBuilder("Emp_Id").setSQLType(Types.INTEGER).toColumn())
.addColumn(new ColumnBuilder("Emp_Name").setSQLType(Types.VARCHAR).toColumn())
.addColumn(new ColumnBuilder("Emp_Employer").setSQLType(Types.VARCHAR).toColumn())
.toTable(database);
table.addRow(122875, "Sarath Kumar Sivan","Infosys Limited.");//Inserting values into the table
}
public static void main(String[] args) throws IOException, SQLException {
JackcessLibrary.startDatabaseProcess();
}
}
You can use the below method instead of configuring System DSN in your machine.
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
connection = DriverManager.getConnection("jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=C:/Users/Desktop/your-database-file.mdb", "", "");
Here "your-database-file.mdb" is your MS-Access file. You can give the full path of your database file in your code to establish the connection. You can also keep the database file in your project(application) folder. In this case you will be able to give your database file along with the application to the client and he/she can use your application without anymore DSN configuration.
Hope this serves your purpose!
Thanks you!
Now that the JDBC-ODBC Bridge has been removed from Java (as of Java 8), future readers might be interested in UCanAccess, a free and open-source pure Java JDBC driver for Access databases. UCanAccess includes a newdatabaseversion
connection parameter that will create the Access .accdb or .mdb file if it does not already exist.
Sample code:
String dbFileSpec = "C:/Users/Gord/Desktop/myDb.accdb";
try (Connection conn = DriverManager.getConnection(
"jdbc:ucanaccess://" + dbFileSpec +
";newdatabaseversion=V2010")) {
DatabaseMetaData dmd = conn.getMetaData();
try (ResultSet rs = dmd.getTables(null, null, "Clients", new String[] { "TABLE" })) {
if (rs.next()) {
System.out.println("Table [Clients] already exists.");
} else {
System.out.println("Table [Clients] does not exist.");
try (Statement s = conn.createStatement()) {
s.executeUpdate("CREATE TABLE Clients (ID COUNTER PRIMARY KEY, LastName TEXT(100))");
System.out.println("Table [Clients] created.");
}
}
}
conn.close();
}
For details on how to set up UCanAccess see
Manipulating an Access database from Java without ODBC