Click here to Skip to main content
15,891,375 members
Articles / Programming Languages / Java
Tip/Trick

Java and Microsoft Access via Jackcess: How to Connect MDB or ACCDB, Get Data from Table and Display It in JTable

Rate me:
Please Sign up or sign in to vote.
3.33/5 (3 votes)
4 Mar 2015CPOL 31.5K   1.4K   1   6
Very Simply MDB/ACCDB Viewer with Swing GUI written in NetBeans IDE using GUI Builder

Introduction

This "simple-sample" demonstrates:

  • how to open MDB/ACCDB, create it if not exists, and create table if not exists
  • how to get list of its tables to JComboBox
  • how to connect a table and show data from table in JTable

Preparation

Jackcess is pure Java library, It doesn't requires any third-party programs and not-Java libraries (i.e. Microsoft Access, DAO, Microsoft Jet 4.0, etc.)

You should only:

  • download Jackcess
  • download Apache Common Lang Library (2.6 version, not 3.*, important!)
  • download Apache Common Logging Library (1.2 version)
  • add all these libraries to project

Hint: All needed libs included in two samples are attached to this tip. See the links at the top of this post.

Code

In JFrame Class

Java
private Connection connect = null;
private Statement statement = null;

Open DB, or Create If It Doesn't Exist

Java
try {
    File dbFile = new File("testDB.accdb");
    if(dbFile.exists() && !dbFile.isDirectory()) {
db = DatabaseBuilder.open(dbFile);
    } else {
db = DatabaseBuilder.create(Database.FileFormat.V2007, dbFile);
    }
} catch (IOException e) {
    JOptionPane.showMessageDialog(this,
        "Can't create or open file \"testDB.accdb\". 
        Check if it permitted by security settings of path/file.\nMore info:\n" +
        e,
        "Error", JOptionPane.ERROR_MESSAGE);
}

Create "Table 1" Table If It Does Not Exist

Java
try {
    if (!db.getTableNames().contains("Table 1")) {
        Table tblNew = new TableBuilder("Table 1")
            .addColumn(new ColumnBuilder("id", DataType.LONG)
                .setAutoNumber(true))
            .addColumn(new ColumnBuilder("name", DataType.TEXT))
            .addColumn(new ColumnBuilder("age", DataType.INT))
            .addIndex(new IndexBuilder(IndexBuilder.PRIMARY_KEY_NAME)
            .addColumns("id").setPrimaryKey())
        .toTable(db);

        // Fill table by data
        tblNew.addRow(Column.AUTO_NUMBER, "John", 27);
        tblNew.addRow(Column.AUTO_NUMBER, "Peter", 43);
    }
} catch (Exception e) { }

Get All Tables Names and Add Them to combobox

Java
try {
    Set<String> tables = db.getTableNames();
    
    for (String t : tables) {
        jComboBox1.addItem(t);
    }
} catch (Exception e) { }

Load Data from User-selected Table and Show it in JTable

Java
try {
    tbl = db.getTable(jComboBox1.getSelectedItem().toString());
    
    tm = (DefaultTableModel)jTable1.getModel();
    
    // Clear TableModel before filling
    tm.setColumnCount(0);
    tm.setRowCount(0);
    
    // Add all columns from table
    for (Column col : tbl.getColumns()) {
        tm.addColumn(col.getName());
    }
    
    // Add all rows from table
    for (Row row : tbl) {
        tm.addRow(row.values().toArray());
    }
} catch (Exception e) { }

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Comments and Discussions

 
QuestionUsed with V1997 [VERSION_3]] Pin
Orilion9-Nov-16 22:23
Orilion9-Nov-16 22:23 
QuestionI Anrew ! Pin
BJANOS7-Mar-15 7:24
BJANOS7-Mar-15 7:24 
AnswerRe: I Anrew ! Pin
IAndreev937-Mar-15 7:51
IAndreev937-Mar-15 7:51 
QuestionMessage Closed Pin
6-Mar-15 20:51
mioan6-Mar-15 20:51 
AnswerRe: would this also work on mac osx except from windows? Pin
IAndreev936-Mar-15 21:22
IAndreev936-Mar-15 21:22 
GeneralRe: would this also work on mac osx except from windows? Pin
mioan6-Mar-15 21:36
mioan6-Mar-15 21:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.