I am trying to write a java application to connect to in memory sqlite db using maven.I added SQLite dependency to pom.xml file too. When I run the program I get the errors:
"java.lang.ClassNotFoundException: org.sqlite.JDBC" and "No suitable driver found for jdbc:sqlite:myDB.sqlite".
Please, let me know if I am missing something. Thank YOU!
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);
}
}
}
}
My POM file depenedency is:
<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>
remove maven-jar-plugin
And use this plugin to generate your jar with all dependencies, and run the 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>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.