1

I am trying to automatically create an .accdb database but I get a compile error using the create() function:

Library Folder

Error Overview

I use the code from this SO answer: Create an Access database file (.mdb or .accdb) using Java

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/abdulwhab/Desktop/database/db.accdb"; // 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();
    }
}
Community
  • 1
  • 1
Abdul Wahab
  • 99
  • 1
  • 2
  • 11

2 Answers2

4

You use the brand new version 2.1.3 of Jackcess, wheras the five-year old answer you linked uses 1.2.6. The API of Jackcess underwent several changes when introducing version 2.

In the version you use a database is created by using a builder:

DatabaseBuilder.create(FileFormat, File)

For more on how to use the Jackcess API, see http://jackcess.sourceforge.net/cookbook.html.

Leviathan
  • 2,468
  • 1
  • 18
  • 24
2

for others clarity and easiness in future just do following changes in the Database creating function

    File file = new File("C:/Users/abdulwhab/Desktop/database/test.accdb");
    Database db = new DatabaseBuilder(file).setFileFormat(Database.FileFormat.V2000).create();
    return db;

Note: i didn't chnage the jackess jar file version to previous one.

No chnages were made except the code :)

Abdul Wahab
  • 99
  • 1
  • 2
  • 11
  • This is a good way to create a database if you want to specify various other options, like the charset, timezone etc. If you just want to provide file format and the file, you should use the static convenience method `create()` from my answer instead. This is much shorter: `return DatabaseBuilder.create(Database.FileFormat.V2000, file);` – Leviathan Feb 19 '17 at 11:21