0

这是我的代码:

package dsnless;
import java.sql.*;
////////////////////////////////////////////////////////////
//This class will be used to fire queries to the database
////////////////////////////////////////////////////////////
public class Query {
    public Query(){
        String pathToDatabase = "E:/Eclipse Projects/JDBC/src/datasouce/School.mdb";
        String database = "jdbc:odbc:Driver="+
                      "{Microsoft Access Driver(*.mdb,*.accdb)};" +
                      ":DBQ=" + pathToDatabase;
        try{
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            con = DriverManager.getConnection(database);
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {
        new Query();
    }
    Connection con;
    Statement s;
    ResultSet r;
}//class Query end  

这是一个例外:

java.sql.SQLException: [Microsoft][ODBC Driver Manager] DRIVER keyword syntax error
    at sun.jdbc.odbc.JdbcOdbc.createSQLException(Unknown Source)
    at sun.jdbc.odbc.JdbcOdbc.standardError(Unknown Source)
    at sun.jdbc.odbc.JdbcOdbc.SQLDriverConnect(Unknown Source)
    at sun.jdbc.odbc.JdbcOdbcConnection.initialize(Unknown Source)
    at sun.jdbc.odbc.JdbcOdbcDriver.connect(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at dsnless.Query.<init>(Query.java:14)
    at dsnless.Query.main(Query.java:20)  

还有一个新的例外:

[Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
    at sun.jdbc.odbc.JdbcOdbc.createSQLException(Unknown Source)
    at sun.jdbc.odbc.JdbcOdbc.standardError(Unknown Source)
    at sun.jdbc.odbc.JdbcOdbc.SQLDriverConnect(Unknown Source)
    at sun.jdbc.odbc.JdbcOdbcConnection.initialize(Unknown Source)
    at sun.jdbc.odbc.JdbcOdbcDriver.connect(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at dsnless.Query.<init>(Query.java:14)
    at dsnless.Query.main(Query.java:20)

问题

请告诉我有什么问题。我怀疑这是String database但仍然只是一个猜测。

自己搞定了

我将支持的扩展从更改(*.mdb,*.accdb)为 only(*.mdb)并且它有效。知道为什么会这样吗???

4

1 回答 1

2

你这里有一个额外的冒号,删除它

String database = "jdbc:odbc:Driver="+
              "{Microsoft Access Driver(*.mdb,*.accdb)};" +
              ":DBQ=" + pathToDatabase;
               ^
于 2013-01-06T14:23:46.810 回答