如何将java连接到Ms Access 2010?

时间:2011-06-14 04:27:58

标签: java jdbc odbc

有没有人对如何将Access 2010连接到java jdbc有任何想法。我使用这种方法,但是当我调用它时,它不起作用:

public void loadDb(){
   try{
       Class.forName("sun.jdbc.JdbcOdbcDriver");
       File f = new File(System.getProperty("user.dir"))       
       con = DriverManager.getConnection("jdbc:odbc:Driver={Microsoft Acess Driver (*.mdb, *.accdb)}; DBQ="+ f.getPath() + "//db//JavaAccess.accd","","");
       st = con. createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
   }catch(ClassNotFoundException e){e.printStackTrace();
   }catch(SQLException e){e.printStackTrace();}
}

//con and st are already defined

6 个答案:

答案 0 :(得分:8)

根据msdn,它应该是sun.jdbc.odbc.JdbcOdbcDriver。所以替换这行代码:

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

答案 1 :(得分:6)

拼写错误?也许这一行:

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

应该是

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

Access有2个C

答案 2 :(得分:5)

建立连接

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;
    }

如何致电:

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());
 }
}

答案 3 :(得分:2)

使用UCanAccess JDBC驱动程序:

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); 

例如:

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

因此,对于您的示例,它将是

con = DriverManager.getConnection("jdbc:ucanaccess://"+f.getPath()+"/db/JavaAccess.accd")

答案 4 :(得分:0)

Rishab的回复帮助我连接到我的访问数据库。

我在代码中进行了以下更正:

而不是

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

我做了

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

我使用路径和扩展名明确定义了驱动程序和完整数据库名称。

答案 5 :(得分:-1)

仅在今天,我们面临同样的问题,并发现如果您的 Java版本如果Java版本高于7,则将不支持sun.jdbc.odbc.JdbcOdbcDriver,因此只需检查Java版本即可。