English 中文(简体)
连接java的MS接入
原标题:connect to MS access from java

I tried to connect my java program to Access DB using jdbc-odbc bridge when I write my connection code this Exception is thrown and I could not solve the problem

java.sql.SQLException: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
    at sun.jdbc.odbc.JdbcOdbc.createSQLException(JdbcOdbc.java:6957)
    at sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java:7114)
    at sun.jdbc.odbc.JdbcOdbc.SQLDriverConnect(JdbcOdbc.java:3073)
    at sun.jdbc.odbc.JdbcOdbcConnection.initialize(JdbcOdbcConnection.java:323)
    at sun.jdbc.odbc.JdbcOdbcDriver.connect(JdbcOdbcDriver.java:174)
    at java.sql.DriverManager.getConnection(DriverManager.java:582)
    at java.sql.DriverManager.getConnection(DriverManager.java:207)
    at jdbc.TestAccesDBCon.getConnection(TestAccesDBCon.java:44)
    at jdbc.TestAccesDBCon.main(TestAccesDBCon.java:21)

这是我的法典:

public static void main(String[] args) throws Exception {
    Connection conn = getConnection();
    Statement st = conn.createStatement();
    st = conn.createStatement();
    ResultSet rs = st.executeQuery("SELECT * FROM users");
    ResultSetMetaData rsMetaData = rs.getMetaData();
    int numberOfColumns = rsMetaData.getColumnCount();
    System.out.println("resultSet MetaData column Count=" + numberOfColumns);
    st.close();
    conn.close();
  }

  private static Connection getConnection() throws Exception {
    String driver = "sun.jdbc.odbc.JdbcOdbcDriver";  
    String connectionString = "jdbc:odbc:Driver= " +
"{Microsoft Access Driver (*.accdb)};DBQ=TestDB.accdb;DriverID=01";
    Class.forName(driver);
    return DriverManager.getConnection(connectionString);
  }

为什么这一例外被推翻? 请帮助?

最佳回答

Seems that you don t have installed the ODBC Access Driver (ODBC drivers are optional when Access is installed !), try to install drivers from Access installation media, if you are on a 64 bit system you should install the 64 bit version of the driver.

如果你在控制小组下管理工具的ODBC经理GUI中获胜,则在来源下发现:

Microsoft Access Driver (*.accdb)

这意味着司机没有安装在你的系统中,需要安装。

http://www.microsoft.com/download/en/details.aspx?id=13255“rel=“nofollow”>here

问题回答

您可以尝试这一法典。

        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        String DBpath = "Your Database Path";
        String driver = "jdbc:odbc:Driver={Microsoft Access Driver (*.accdb)};DBQ=";
        driver += DBpath.trim() + ";DriverID=22;READONLY=true}";
        Connection con = DriverManager.getConnection(driver, "", "");
        Statement s = con.createStatement();
        con = DriverManager.getConnection(driver, "", "");
        return (Statement) s;




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签