Discussion:
Netbeans Java to connect MS Access Database error
EminOz
2011-12-16 19:07:09 UTC
Permalink
Hi,

I have ms access connection database with java, I used the this code

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url = "jdbc:odbc:Driver={Microsoft Access Driver " +
"(*.mdb, *.accdb)};DBQ=C:\\Users\\SERVER\\Desktop\\Law Sistem\\Database1.accdb";
Connection con = DriverManager.getConnection(url);
System.out.println("Connected!");
con.close();


and I toke this error

Exception in thread "main" java.sql.SQLException: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
at sun.jdbc.odbc.JdbcOdbc.createSQLException(JdbcOdbc.java:6956)
at sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java:7113)
at sun.jdbc.odbc.JdbcOdbc.SQLDriverConnect(JdbcOdbc.java:3072)
at sun.jdbc.odbc.JdbcOdbcConnection.initialize(JdbcOdbcConnection.java:323)
at sun.jdbc.odbc.JdbcOdbcDriver.connect(JdbcOdbcDriver.java:174)
at java.sql.DriverManager.getConnection(DriverManager.java:579)
at java.sql.DriverManager.getConnection(DriverManager.java:243)
at javaapplication4.JavaApplication4.main(JavaApplication4.java:19)
Java Result: 1



How can I fix this ? Or connect that ?
ebaumann
2011-12-19 09:33:29 UTC
Permalink
The Exception Messages contains the reason: "Data source name not found". The sun.jdbc.odbc.JdbcOdbcDriver is a bridge and depends on an existing ODBC driver.

On the windows operating system the Microsoft Access Driver is only 32 Bit. So you have to start the appropriate ODBC data source Administrator odbcad32.exe, on 64 Bit Windows whithin the %WINDIR%\SysWOW64 directory and the Java JVM running your application must run in 32 Bit mode.

With the ODBC data source Administrator you have to create a data source with the "Microsoft Access Driver" and give it a name, e.g. "MyAppDataSource". After creating a data source, you can access ist with the JdbcOdbcDriver throuhg it's name via the URL "jdbc:odbc:MyAppDataSource".

You can't define an Access file and hope, that the JdbcOdbcDriver creates a connection. Because it is a bridge, it will use an existing ODBC driver - the "Microsoft Access Driver" or any other which is capable to manage Access databases and redirect JDBC calls to that ODBC driver as ODBC calls. The ODBC driver accesses data sources through their names - maybe that some can create them automatically by giving them a file name, but the Microsoft Access ODBC driver doesn't that.
Continue reading on narkive:
Search results for 'Netbeans Java to connect MS Access Database error' (Questions and Answers)
3
replies
Java Help Again?
started 2007-04-12 23:41:49 UTC
programming & design
Loading...