0

I'm trying to access a Microsoft Access database using the following code:

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String database = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ="+ dbpatch + "/SilverDB.accdb";
Connection conn = DriverManager.getConnection(database, "", "");
Statement s = conn.createStatement();

The problem is that when I run it through Eclipse, everything goes fine, but when I generate the executable jar and then run it, it doesn't work. Does anyone knows the reason?

Thanks!

xadun
  • 134
  • 2
  • 11
  • is there exception , have you packaged the drivers along with executable jar – Mohammed Falha Nov 02 '14 at 23:58
  • 1
    Did you know they've removed the JdbcOdbc bridge as of Java 8? – Elliott Frisch Nov 03 '14 at 00:03
  • Well, I use the option "Package required libraries into generated JAR" when generating the executable file, and when I extract this file, everything is there. – xadun Nov 03 '14 at 00:03
  • @ElliottFrisch, it doesn't worked even using UCanAccess, as suggested [here](http://stackoverflow.com/questions/21955256/manipulating-an-access-database-from-java-without-odbc) – xadun Nov 03 '14 at 00:05
  • I would start by not using Access. I suggest [h2](http://h2database.com/html/main.html) or [derby](http://db.apache.org/derby/). If you really need to use Access, then you might try [this](https://www.easysoft.com/applications/microsoft-access/jdbc-odbc.html). – Elliott Frisch Nov 03 '14 at 00:09

1 Answers1

1

Check the version of Java you are running in Eclipse vs when you double click the jar. If you are running Java 7 in Eclipse and Java 8 when you doubleclick, then that may explain the problem.

Whatever the issue, if you don't post the stack trace of the failure no one here is going to be able to help you. "It doesn't work" isn't a description that allows someone to try to help.

Kevin Day
  • 15,263
  • 8
  • 35
  • 66
  • Eclipse: java.runtime.version=1.7.0_45-b18 Oh, and I don't have Java 8 installed in my computer.. just Java 7. Just checked it. Other strange thing is that, when I run the executable java using the CMD command (java -jar filename.jar) it works fine!!! – xadun Nov 03 '14 at 00:30
  • 1
    Sounds like you have a problem with the .jar registration in the registry. Might want to re-install Java. Regardless, you should really expand on what "doesn't work" means. You should also do a test of a super simple jar that doesn't do database access or anything like that - just put up a simple HelloWorld message... – Kevin Day Nov 03 '14 at 00:34
  • Well, after some time "googling" I found [this] (http://stackoverflow.com/questions/5827164/why-does-my-jar-file-execute-at-cmd-but-not-on-double-click?answertab=active#tab-top) post which solved my problem. Aparently the jar association registry was kind messed up, as you said, and reinstalling the Java would work but the "Jarfix" program worked like a charm! – xadun Nov 03 '14 at 01:24