Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
579 views
in Technique[技术] by (71.8m points)

java - 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)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:

A commercial driver for Access based on UnixODBC:

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...