JDBC driver MS Access connection

后端 未结 9 1863
执念已碎
执念已碎 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();
        }
    }
    

提交回复
热议问题