Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connecting to access database from linux

I've created my application and tested it under windows, which writes/reads to/from a access DB file.

But in the real world it will be ran in the linux environment, and I have a big issue now, it appears that there are no drivers for linux to access ms acess db, here is how I make the connection now :

private static Connection getConnection() {
        if (connection == null) {
            try {
                Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                String conStr = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=" + configuration.getAccessDbFile();
                connection = DriverManager.getConnection(conStr);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return connection;
    }

Has anyone encountered something similar to this, does anybody have a suggestion what could I do ?

This is the exception I get on the linux :

java.lang.NullPointerException
        at sun.jdbc.odbc.JdbcOdbcDriver.initialize(JdbcOdbcDriver.java:436)
        at sun.jdbc.odbc.JdbcOdbcDriver.connect(JdbcOdbcDriver.java:153)
        at java.sql.DriverManager.getConnection(DriverManager.java:582)
        at java.sql.DriverManager.getConnection(DriverManager.java:207)
like image 361
London Avatar asked Apr 21 '11 09:04

London


People also ask

Does Microsoft Access work on Linux?

Microsoft Office Access is not available for Linux but there are plenty of alternatives that runs on Linux with similar functionality. The best Linux alternative is LibreOffice - Base, which is both free and Open Source.

How do I link my ODBC database to access?

Open your Microsoft Access database. Select the External Data tab in the ribbon. Expand the New Data Source drop-down and select From Other Sources, then select ODBC Dababase. In the Get External Data - ODBC Database dialog box, select Link to the data source by creating a linked table.


2 Answers

It's uncommon for an application running on Linux to access an MS Access database or use ODBC. ODBC is a windows technology. There are some options for linux, but it's not a common scenario.

As you've discovered, there are no Access ODBC drivers on your linux machine, so the JDBC-ODBC bridge fails. You might be able to install a suitable ODBC driver, but I don't know of any that are free or open-source. The defacto ODBC option for linux is:

  • http://www.unixodbc.org/

A commercial driver for Access based on UnixODBC:

  • http://www.easysoft.com/products/data_access/index.html

A type 4 JDBC driver that can supposedly connect to Access databases:

  • http://www.hxtt.com/access.html

I don't have personal experience with any of these. The type 4 JDBC driver would be ideal, but I'd be skeptical that it works as perfectly as advertised.

(I am sure you have reasons, but I have to wonder why you are using an Access database if you plan on deploying to Linux machines. I believe the only good reason to have a Java application use an Access database is if it's an existing Access application with custom programming used for purposes beyond the Java application. Otherwise, there are a number of better options. Also consider that if your main reason for using Access is just so you can use Access as a user-friendly GUI tool for forms & reporting, you could still store the data in less restrictive database (Derby, SQLLite, MySQL, PostgreSQL, MS SQL Server, etc) and then connect via ODBC from Access to the database.) This would allow you to deploy your Java application on linux, your database wherever makes most sense, and still use Access to connect to the database from windows. I've done this a number of times.)

like image 140
kaliatech Avatar answered Oct 21 '22 04:10

kaliatech


Use http://jackcess.sourceforge.net/

You can read / write a Acceess database from Linux or Windows using Java.

like image 36
Abel Avatar answered Oct 21 '22 05:10

Abel