Connecting a Microsoft Access Database to Java using JDBC and compiling

最后都变了- 提交于 2019-12-01 15:47:56

I know the post was years ago but I felt like answering the question for those who are just experiencing this right now. It took me a while to know the answer to the question so here's the solution:

http://wiki.netbeans.org/FaqSettingHeapSize

Follow the "Running the 32-bit JVM".

All you have to do is find the netbeans.conf in the installation folder of your netbeans and change the directory from something like this:

netbeans_jdkhome="C:\Program Files\Java\jdk1.6.0_24"

to this:

netbeans_jdkhome="C:\Program Files (x86)\Java\jdk1.6.0_21"

The problem is netbeans might be running in 64 bit but MS Access only support 32-bit. So doing this would hopefully solve the problem. Also make sure to install this:

http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=23734

Anil_irocks88

The main problem lies in the line:

String database = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=TLDATABASEDBM.mdb";
  1. Make sure that the .mdb file is in the correct directory.
  2. Check the file extension as .mdb or .mdbacc.

Also, if you want to use the same DSN every time, it is better to add the DSN(Data Source Name) into the respective system on which the mdb is stored.

I think that your app do not see TLDATABASEDBM.mdb in current directory. You can give full path to this file in connection string or add system DSN in ODBC Manager and then connect to it with connection string like: jdbc:odbc:TLDATABASEDBM

Honestly, I dont like what I am going to say... but, it solved the same issue for me... mysteriously... :(((

on the line where you are defining the database variable, I changed ...(.mdb)... into ...(.mdb, *.accdb)...

All the best for figuring out what difference that made!

package javaapplication1;

import java.sql.*;

public class MSaccess_archive {
public static void main(String[] args) {

    try {

       Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
// set this to a MS Access DB you have on your machine
String filename = "mdbTEST.mdb";
String database = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=";
database+= filename.trim() + ";DriverID=22;}"; // add on to the end 
// now we can get the connection from the DriverManager
Connection con = DriverManager.getConnection( database ,"","");

        Statement stmt = con.createStatement();

        stmt.execute("select * from student"); // execute query in table student

        ResultSet rs = stmt.getResultSet(); // get any Result that came from our query

        if (rs != null)
         while ( rs.next() ){

            System.out.println("Name: " + rs.getInt("Age") + " ID:       "+rs.getString("Course"));
            }

            stmt.close();
            con.close();
        }
        catch (Exception err) {
            System.out.println("ERROR: " + err);
        }
   }

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