Connecting to access database from linux

旧时模样 提交于 2019-12-30 08:07:28

问题


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)

回答1:


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




回答2:


Use http://jackcess.sourceforge.net/

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




回答3:


If you are stuck with Access, but your interactions with the database file are pretty "simple" (e.g. you aren't doing complex SQL queries), you could use something like Jackcess. It is a pure-java library which allows you to manipulate an Access database, but it does not provide a jdbc interface, so you have to write code using the Jackcess api.

(disclaimer, i am a jackcess developer)




回答4:


UCanAccess is a free, open-source pure Java JDBC driver for Access databases. For more information on how to use it, see

Manipulating an Access database from Java without ODBC




回答5:


Go through thus link which will help you in this.

http://www.neowin.net/forum/topic/887410-java-ms-access-driver-for-maclinux/

and can you debug and let us know what is the connection string you are passing to get the connection. Because I think you should call getConnection as below

DriverManager.getConnection(conStr, "", "");


来源:https://stackoverflow.com/questions/5742322/connecting-to-access-database-from-linux

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!