创建DSNless连接时的Java SQL异常

时间:2013-01-06 14:17:11

标签: java sql jdbc odbc

这是我的代码:

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但是它只是一个猜测。

Nailed它,我自己

我将支持的扩展程序从(*.mdb,*.accdb)更改为仅(*.mdb),并且有效。知道为什么会发生这种事吗???

1 个答案:

答案 0 :(得分:2)

此处有一个额外的冒号,将其删除

String database = "jdbc:odbc:Driver="+
              "{Microsoft Access Driver(*.mdb,*.accdb)};" +
              ":DBQ=" + pathToDatabase;
               ^