JDBC driver MS Access connection

后端 未结 9 1853
执念已碎
执念已碎 2020-12-09 06:10

I want connect my MS access file with Java GUI program,but I have problem with connection....

I have Windows 7 64b, and ms office 2007. When I opened the ODBC driver

相关标签:
9条回答
  • 2020-12-09 06:52

    If you are using Windows 64-bit you probably need to go to this path

    C:/Windows/SysWOW64/odbcad32.exe

    Then I noticed that you are using the direct path instead creating new System DSN, your direct path is correct till the path to the access file you must give the full path like this :

    jdbc:odbc:Driver= Microsoft Access Driver (*.mdb, *.accdb);DBQ=path/to/Invertory.mdb"

    To get the path you probably need to use java.io.File that have a method returns the abslute path to the file see the example :

    import java.sql.*;
    public class TestConnection {
        Connection con ;
        Statement st ;
        ResultSet rs ;
        String db;
        public TestConnection (){
            try{
                String path = new java.io.File("Invertory.mdb").getAbsolutePath();
            db ="JDBC:ODBC:Driver=Microsoft Access Driver (*.mdb, *.accdb); DBQ="+path;
                doConnection();
            } catch(NullPointerException ex){
                    ex.printStackTrace();
                }
    
        }
    
        public void doConnection(){
            try{
                Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                con = DriverManager.getConnection(db);
                st = con.createStatement();
                rs = st.executeQuery("select * from Invertory");
                while(rs.next()){
                    System.out.println(rs.getObject(1));
                }
            }catch(SQLException | ClassNotFoundException ex){
                System.out.println(ex.toString());
    
            }
    
        }
        public static void main(String...argS){
            new TestConnection();
        }
    }
    
    0 讨论(0)
  • 2020-12-09 06:53
    final String fileName = "c:/myDataBase.mdb"
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    String url = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ="+fileName;
    Connection con = DriverManager.getConnection(url,username,password);
    
    0 讨论(0)
  • 2020-12-09 06:58

    On a 64 bit system, you should:

    1. run as admin accessdatabaseengine_64.exe
    2. run java - 7-64 bit - jre.
    0 讨论(0)
  • 2020-12-09 07:01

    JDBC-ODBC MS-ACCESS CONNECTION STOPPED WORKING IN JDK8. I solved the issue by installing JDK7 along with JDK8 in the same PC, once installed JDK7 I assigned it as the JDK version to use in my project as follows in Netbeans:

    1.RIGHT CLICK THE PROJECT IN THE LIST > CLICK PROPERTIES

    2.CLICK LIBRARIES ON THE LEFT NAVIGATION TREE

    3.CLICK BUTTON MANAGE PLATFORMS > CLICK BUTTON ADD PLATFORM...

    4.FOLLOW WIZARD, DESPITE IT SHOWS JAVA STANDARD EDITION CLICK NEXT

    5.NAVIGATE TO C:\Program Files (x86)\Java AND SELECT THE FOLDER OF JDK7 > CLICK NEXT

    6.THE FIELD AUTOFILL WITH THE RIGHT INFO... > THEN CLICK FINISH

    7.SELECT THE JDK PLATFORM FROM THE LIST > CLICK CLOSE > OK

    8.JDK7 SHOULD SHOW IN LIBRARIES PACKAGE.

    JDK7 in Libraries Package Click Back in Browser to return here after looking at the image...

    From here on everything must run smoothly.

    Hope it solves your problem.

    Thanks.

    0 讨论(0)
  • 2020-12-09 07:03

    if you are working in NETBEANS then after unzipping ucanacess.zip file add all jar file in the classpath using property window of project click on compile tab and add jar file then compile and test app.

    0 讨论(0)
  • 2020-12-09 07:10

    You just missing something in your code right here :

    db ="JDBC:ODBC:Driver=Microsoft Access Driver (*.mdb, *.accdb); DBQ="+path;
    

    You need to add {} between Driver= and )=; . Like this Below

    db ="JDBC:ODBC:Driver={Microsoft Access Driver (*.mdb, *.accdb)}; DBQ="+path;
    
    0 讨论(0)
提交回复
热议问题