gpt4 book ai didi

创建 DSNless 连接时出现 Java SQL 异常

转载 作者:行者123 更新时间:2023-12-02 07:21:16 24 4
gpt4 key购买 nike

这是我的代码:

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

这是一个异常(exception):

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)

还有一个新的异常(exception):

[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)

问题

请告诉我出了什么问题。我怀疑它是字符串数据库,但它仍然只是一个猜测。

我自己搞定了

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

最佳答案

这里多了一个冒号,请将其删除

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

关于创建 DSNless 连接时出现 Java SQL 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14182977/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com