javams-accessjackcess

Creating .accdb database using Java


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();
    }
}

Solution

  • 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.