[英]No suitable driver found for “jdbc:sqlite:myDB.sqlite” with java application using maven to connect to in-memory SQLite
我正在尝试使用maven编写一个Java应用程序以连接到内存sqlite db。我也将SQLite依赖项添加到pom.xml文件中。 当我运行程序时,出现错误:
“ java.lang.ClassNotFoundException:org.sqlite.JDBC”和“找不到适用于jdbc:sqlite:myDB.sqlite的驱动程序”。
拜托,让我知道我是否缺少什么。 谢谢!
public class Application {
public static void main(String[] args) throws ClassNotFoundException{
System.out.println("Application class created\n");
Connection connection = null;
try {
Class.forName("org.sqlite.JDBC");
}catch (ClassNotFoundException e){
System.err.println("Class not found"+e);
}
try{
connection = DriverManager.getConnection("jdbc:sqlite::memory:");
Statement statement = connection.createStatement();
statement.setQueryTimeout(33);
statement.executeUpdate("create table demo (id integer, name string)");
statement.executeUpdate("insert into demo(1, 'A')");
ResultSet mySet = statement.executeQuery("select * from demo");
while(mySet.next()){
System.out.println("Name is : "+mySet.getString("name"));
System.out.println("Id is : "+mySet.getInt("id"));
}
}
catch (SQLException sqlException){
System.err.println(sqlException.getMessage());
}
finally {
try{
if(connection!=null)
connection.close();
}
catch (SQLException sqlException){
System.err.println(sqlException);
}
}
}
}
我的POM文件依赖性为:
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<!-- https://mvnrepository.com/artifact/org.xerial/sqlite-jdbc -->
<dependencies>
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
<version>3.23.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<mainClass>path-to-mainclass-Application</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
删除maven-jar-plugin
并使用此插件生成具有所有依赖项的jar,并运行jar-with-dependencies
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<archive>
<manifest>
<mainClass>
your.main.Class
</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</execution>
</executions>
</plugin>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.