在我的項目中,我應該同時使用SQLiteDB和MariaDB。Eclipse項目上的多個JDBC文件
因此,我在eclipse項目中導入了jdbc驅動程序文件(* .jar)。
但我得到這樣的錯誤:
代碼:
public DBHelper() {
try {
driver = (Driver) Class.forName("org.sqlite.JDBC").newInstance();
DriverManager.registerDriver(driver);
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
}
makeConnections();
}
public synchronized boolean makeConnections() {
try {
switch (ConnectionManager.getDatabaseType()) {
case SQLITE:
if (conn == null || conn.isClosed()) {
SQLiteConfig config = new SQLiteConfig();
config.setJournalMode(JournalMode.WAL);
conn = DriverManager.getConnection("jdbc:sqlite:data.sqlite", config.toProperties());
conn.setAutoCommit(false);
}
break;
case MARIADB:
if (conn == null || conn.isClosed()) {
conn = DriverManager.getConnection("jdbc:mariadb://data.mariadb");
conn.setAutoCommit(false);
}
break;
}
return true;
} catch (SQLException e) {
e.printStackTrace();
return false;
}
}
錯誤消息:
Exception in thread "main" java.lang.IllegalArgumentException: Invalid connection URL url jdbc:sqlite:data.sqlite
at org.mariadb.jdbc.JDBCUrl.parse(JDBCUrl.java:144)
at org.mariadb.jdbc.Driver.connect(Driver.java:95)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at database.DBHelper.makeConnections(DBHelper.java:96)
at database.DBHelper.<init>(DBHelper.java:60)
at database.DBHelper.getSingleton(DBHelper.java:28)
我清楚地註冊JDBC驅動程序名稱爲「 org.sqlite.JDBC「。但我不知道爲什麼日誌消息中包含「at org。mariadb .jdbc.Driver.connect(...)」。
刪除MariaDB jdbc文件(*。jar)後,程序成功執行。
修改您的DBHelper – Strelok
@Strelok的帖子全部代碼。 – ChangUk