Не найден подходящий драйвер для "jdbc: sqlite: myDB.sqlite" с Java-приложением, использующим maven для подключения к SQLite в памяти - PullRequest
0 голосов
/ 04 октября 2018

Я пытаюсь написать приложение Java для подключения к памяти в sqlite db с помощью maven. Я также добавил зависимость 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>

1 Ответ

0 голосов
/ 05 октября 2018

remove 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>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...