How to connect java to Ms Access 2010?

后端 未结 6 1938
醉酒成梦
醉酒成梦 2020-12-05 09:05

Does anyone have any ideas of how to connect Access 2010 to java jdbc. I use this method, but when I call it, it doesn\'t work:

public void loadDb(){
   try{         


        
相关标签:
6条回答
  • 2020-12-05 09:12

    Spelling error? Perhaps this line:

    con = DriverManager.getConnection("jdbc:odbc:Driver={Microsoft Acess Driver (*.mdb, *.accdb)}; DBQ="+ f.getPath() + "//db//JavaAccess.accd","","");
    

    should be

    con = DriverManager.getConnection("jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)}; DBQ="+ f.getPath() + "//db//JavaAccess.accd","","");
    

    Access has 2 C's

    0 讨论(0)
  • 2020-12-05 09:14

    Create connection

    public static Connection getConnection() {
         String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
            String url = "jdbc:odbc:anime"; //anime is the database
            String username = "ipieluser"; //leave blank if none
            String password = "ipielpassword"; //leave blank if none
            try {
          Class.forName(driver);
         } catch (ClassNotFoundException e) {
          e.printStackTrace();
         }
            try {
          return DriverManager.getConnection(url, username, password);
         } catch (SQLException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
         }
         return null;
        }
    

    How to call:

    public static void main(String args[]) {
     try {
      Connection conn = getConnection();
         Statement st = conn.createStatement();
         st = conn.createStatement();
         ResultSet rs = st.executeQuery("SELECT * FROM localTable");     
    
      //get and displays the number of columns
         ResultSetMetaData rsMetaData = rs.getMetaData();
      int numberOfColumns = rsMetaData.getColumnCount();
         System.out.println("resultSet MetaData column Count=" + numberOfColumns);
    
         st.close();
         conn.close();
     } catch(Exception e) {
      System.out.println(e.getMessage());
     }
    }
    
    0 讨论(0)
  • 2020-12-05 09:20

    According to msdn it should be sun.jdbc.odbc.JdbcOdbcDriver. So replace this line of code:

    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

    0 讨论(0)
  • 2020-12-05 09:21

    Rishab's reply helped me to connect to my access database.

    I did following correction in the code:

    Instead of

    String url = "jdbc:odbc:anime"; //anime is the database
    

    I did

    String url = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)}; DBQ=" + "d://institute//institutedata.accdb";
    

    I explicitly defined driver and full database name with path and extension.

    0 讨论(0)
  • 2020-12-05 09:34

    Use UCanAccess JDBC Driver :

    Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");  // can be omitted in most cases
    Connection conn=DriverManager.getConnection("jdbc:ucanaccess://<mdb or accdb file path>",user, password); 
    

    e.g.:

    Connection conn=DriverManager.getConnection("jdbc:ucanaccess://c:/pippo.mdb");
    

    So for your example it will be

    con = DriverManager.getConnection("jdbc:ucanaccess://"+f.getPath()+"/db/JavaAccess.accd")
    
    0 讨论(0)
  • 2020-12-05 09:35

    As today only we face the same problem and found that to check the version of java if your version of java if the version of the java is above 7 then the sun.jdbc.odbc.JdbcOdbcDriver will not be supported so just check the version of the java.

    0 讨论(0)
提交回复
热议问题