问题
I have a program in Netbeans that uses SQL to connect to an Access Database. I am ready to finalize and make a jar file out of the program, but when I 'build and clean' and then try to run the program in command I get:
java.sql.SQLException: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
In my code I access the Database directly using:
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};Dbq=C:\\Users\\d9t\\Desktop\\Resource_DB.accdb");
回答1:
I have not done this (JDBC-ODBC) for many many years, but I think in addition to what you have done, you also need to add the data source like this: http://msdn.microsoft.com/en-us/library/ca6axakh(v=vs.80).aspx
回答2:
It sounds like your driver jar may need to be added to the classpath when you are running the program. Run the program specifying a classpath (-classpath or -cp)
java -classpath driver.jar;myprogram.jar my.package.MainClass
where my.package.MainClass if the class you want to run
回答3:
I just tried to recreate your issue using NetBeans 7.4 and was unsuccessful. My project ran correctly in the NetBeans IDE, and after doing a "Clean and Build" it also ran successfully from the command line via
java -jar AccessTest.jar
For diagnostic purposes, please try this:
Add the following two lines of code as the very first lines in your main
method:
System.out.println(System.getProperty("sun.arch.data.model"));
System.exit(0);
Run your code in the IDE and note the number that it prints to the console. (It will either be "32" or "64".)
Then re-build your project and run the .jar
file from the command line. Does it report the same value when you run it that way?
Edit re: comment
The output from the above diagnostics confirmed the problem. The 32-bit version of the Access Database Engine (a.k.a. "ACE") was installed on the machine and the NetBeans configuration was set to run the application in a 32-bit JVM from within NetBeans itself, so that worked fine. However, when the .jar file was run from the command line it was running in a 64-bit JVM so the Access ODBC driver was not available.
The key point here is that the "bitness" of the installed ACE driver will have to match the "bitness" of the JVM under which the Java application is running. That is not really something that you, as the developer, can control. Even if you were to use the System.getProperty
test to identify the "bitness" under which the application is running, you can't guarantee that the corresponding ACE driver will be installed.
What you could do is build your application to use the older 32-bit "Jet" driver...
{Microsoft Access Driver (*.mdb)}
...because that is available on all Windows machines. Your database file would have to use the older .mdb
file format (because Jet cannot read .accdb
files) and you could use the System.getProperty
test to ensure that the application is running under a 32-bit JVM. (That is, if the call returned "64" just tell the user that they must run the application under a 32-bit JVM and then exit.)
回答4:
I stumbled upon this fix http://ucanaccess.sourceforge.net/site.html
Install UCanAccess-2.0.1-bin.zip and include all the files. I encountered some minor bugs such as not being able to pass ' and " into Access, but it successfully allows 64bit computers to connect 32bit Access.
Hope this helps you Access programmers!
来源:https://stackoverflow.com/questions/21289516/making-a-jar-file-containing-sql-commands-in-netbeans