我正在尝试编写一个 java 应用程序以使用 maven 连接到内存中的 sqlite db。我也向 pom.xml 文件添加了 SQLite 依赖项。当我运行程序时,我收到错误:
“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>
牧羊人nacy
相关分类