sign in to MS Access from java

advertisements

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)

This is my code :

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

why this exception is thrown? please help?


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.

If you, going in ODBC Manager GUI in Amministrative Tools under the control panel, won't find under the sources tab the source :

Microsoft Access Driver (*.accdb)

this means that the driver are not installed on your system and you need to install it.

You can download Access 2010 driver from here .