我希望有一个人可以帮助我。我正在开发一个与SQLite数据库连接的简单应用程序。以下是我的连接代码:
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数据库的例子,我发现这个例子效果很好)
这个程序我在窗口构建器(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没有加载,我不知道为什么。所以我补充道
的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>
您不必像其他答案中提出的那样添加特定的类路径或隐式用法。
我使用简单的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) }
}
}