[英]Unable to connect to database (No suitable driver found)
我的連接代碼:
try {
Connection con = DriverManager.getConnection("jdbc:sqlite:myDB.sqlite");
PreparedStatement pstm = con.prepareStatement("insert into hell(username,pssword) " +
"values ('"+tfUname.getText()+"','"+tfUpass.getText()+"')");
pstm.close();
con.close();
JOptionPane.showMessageDialog(null,"Congrats, you have been registered succesfully");
RegisterWindow rw = new RegisterWindow();
rw.setVisible(false);
pack();
dispose();
} catch(SQLException ex) {
setTitle(ex.toString());
}
這只是一個將用戶名和密碼插入數據庫的窗口。 當我單擊按鈕時出現以下異常:
"java.sql.SQLException: No suitable driver found for jdbc:sqlite:C\\LoginJava2\\myDB.sqlite"
我找到了一個如何連接到 Java 中運行良好的 SQLite 數據庫的示例。 我在 WindowBuilder (Eclipse) 中執行此操作,使用示例中的相同驅動程序。 我嘗試了不同的驅動程序,但仍然出現該消息。
您的類路徑缺少包含 sqlite 類和驅動程序的 jar。 您需要類似sqlite-jdbc-3.7.2.jar或您適用的版本。
如果您確定 jar 在那里,請嘗試在創建連接之前添加這行代碼:
Class.forName("org.sqlite.JDBC");
我遇到了同樣的問題。 我使用了 Maven 並添加了依賴項:
<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
<version>3.15.1
</version>
</dependency>
它可以編譯,我得到:
找不到適合 jdbc:sqlite:xx.db 的驅動程序
我檢查了類路徑,我確定 sqlite-jdbc-3.15.1.jar 在那里。 我想由於某種原因,該類沒有加載,我不知道為什么。 所以我加了
Class.forName("org.sqlite.JDBC");
在我的代碼的開頭。 有效!
而且,我刪除了上面的行。 它仍然有效! 我清理了項目並重建它,不再需要 Class.forName() !!! 我還是不知道為什么。 但是問題解決了。 我認為 Class.forName() 可用於診斷您需要的類是否在類路徑中。
除了 Class.forName,還有其他東西。
如果您做了以下兩件事: - 將 sqlite jar 庫添加到項目下的 lib 文件夾中,在項目構建路徑中引用它。 - 添加了 Class.forName("org.sqlite.JDBC") 語句。 並且仍然出現“No suit driver”的錯誤信息,可能是你的數據庫路徑引起的。 如果您使用的是 Windows:而不是:
DriverManager.getConnection("D:\\db\\my-db.sqlite").
你應該使用:
DriverManager.getConnection("jdbc:sqlite:D:\\db\\my-db.sqlite").
“jdbc:sqlite:”可以解決問題。
如果您使用的是 Linux,只需更改分隔符:DriverManager.getConnection("jdbc:sqlite:/your/somepath/my-db.sqlite")。
如果你使用 Maven 並且想要構建一個可執行的 jar,你可以決定將 sqlite jar 的內容導入到你自己生成的 jar 中:
<plugins>
<!-- any other plugins -->
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<archive>
<manifest>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
<addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
<addClasspath>true</addClasspath>
<mainClass>MyPackage.Main</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
您不必像其他答案中建議的那樣添加特定的類路徑或隱式用法。
下面是中央存儲庫的鏈接,您可以從中下載最新的驅動程序文件。
https://repo1.maven.org/maven2/org/xerial/sqlite-jdbc/3.32.3.2/
確保下載sqlite-jdbc-version.jar文件而不是其他文件。 希望這可能會有所幫助
我在使用簡單的 gradle 配置時遇到了類似的問題,如下所示
apply plugin: 'java'
repositories {
mavenCentral()
}
dependencies {
compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.0'
testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.2.0'
compile group: 'org.xerial', name: 'sqlite-jdbc', version: '3.23.1'
}
jar {
manifest {
attributes 'Main-Class': 'rewards.simulator.MainSimulator'
}
}
后來我發現 gradle build 正在創建一個不包含任何外部依賴項的 jar。 以下配置用於在生成的 jar 文件中包含所有依賴庫以及源文件,以創建 fat-jar:
apply plugin: 'java'
repositories {
mavenCentral()
}
dependencies {
compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.0'
testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.2.0'
compile group: 'org.xerial', name: 'sqlite-jdbc', version: '3.23.1'
}
jar {
manifest {
attributes 'Main-Class': 'rewards.simulator.MainSimulator'
}
from {
configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
}
}
3.7.2版本的問題嘗試安裝3.21.0吧
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.