I have referred selected answer to this question: Java: Create MSAccess Database File (.mdb 0r .accdb) using Java.
I have MS Office 2010 in my machine. I am trying to create access database file (*.mdb / *.accdb). But, still the file is not getting created itself throwing following exception:
Exception in thread "main" java.io.FileNotFoundException: given file does not exist: C:\Users\473886\Desktop\employeedb1.mdb
at com.healthmarketscience.jackcess.impl.DatabaseImpl.open(DatabaseImpl.java:360)
at com.healthmarketscience.jackcess.DatabaseBuilder.open(DatabaseBuilder.java:170)
at mdb.MDBWriter.createDatabase(MDBWriter.java:93)
at mdb.MDBWriter.startDatabaseProcess(MDBWriter.java:107)
at mdb.MDBWriter.main(MDBWriter.java:120)
I have used the same code available in the answer with one modification that I have used file dialog that will ask me where I want to save the database file:
public class MDBWriter {
public static String saveFile(Frame f, String title, String defDir, String fileType) {
FileDialog fd = new FileDialog(f, title, FileDialog.SAVE);
fd.setFile(fileType);
fd.setDirectory(defDir);
fd.setLocation(50, 50);
fd.show();
return (fd.getDirectory() + "\\" + fd.getFile());
}
private static Database createDatabase(String databaseName) throws IOException {
// return Database.create(new File(databaseName));
File file = new File(databaseName);
return new DatabaseBuilder(file)
.setFileFormat(Database.FileFormat.V2010)
.open();
}
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 fileName = saveFile(new Frame(), "Save...", ".\\", "*.mdb");
String databaseName = "D:\\employeedb1.accdb"; // Creating an MS Access database
Database database = createDatabase(fileName);
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 {
startDatabaseProcess();
}
}
Please suggest some solution.