user5842257
user5842257

Reputation:

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

Upvotes: 1

Views: 772

Answers (2)

user5842257
user5842257

Reputation:

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 :)

Upvotes: 2

tyg
tyg

Reputation: 15888

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.

Upvotes: 3

Related Questions