猿问

没有找到适合“jdbc:sqlite:myDB.sqlite”的驱动程序

我正在尝试编写一个 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>



富国沪深
浏览 146回答 1
1回答

牧羊人nacy

删除 maven-jar-plugin并使用此插件生成包含所有依赖项的 jar,并运行 jar-with-dependencies<plugin>&nbsp; &nbsp; <groupId>org.apache.maven.plugins</groupId>&nbsp; &nbsp; <artifactId>maven-assembly-plugin</artifactId>&nbsp; &nbsp; <executions>&nbsp; &nbsp; &nbsp; &nbsp; <execution>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <phase>package</phase>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <goals>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <goal>single</goal>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </goals>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <configuration>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <archive>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <manifest>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <mainClass>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; your.main.Class&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </mainClass>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </manifest>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </archive>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <descriptorRefs>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <descriptorRef>jar-with-dependencies</descriptorRef>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </descriptorRefs>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </configuration>&nbsp; &nbsp; &nbsp; &nbsp; </execution>&nbsp; &nbsp; </executions></plugin>
随时随地看视频慕课网APP

相关分类

Java
我要回答