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);
}
为什么这一例外被推翻? 请帮助?