如何在Java app .jar文件中使用SQLite?

时间:2018-02-23 16:55:49

标签: java sqlite jar executable-jar

我正在编写一个使用SQLite的应用程序,在它运行的Netbeans中,但是当我生成一个.jar文件来使用我的应用程序时,它不起作用。我以为该应用程序将创建一个db文件(同一文件夹).jar文件。我在AlertMessage中看到的唯一错误是:ClassNotFound异常。

private void getConnection() throws ClassNotFoundException, SQLException {
        Class.forName("org.sqlite.JDBC");
        con = DriverManager.getConnection("jdbc:sqlite:Dados");
        initialize();
    }

编辑:

我将功能编辑为:

private void getConnection() {
        try {
            JOptionPane.showMessageDialog(null, "getConnection() 1");
            Class.forName("org.sqlite.JDBC");
            JOptionPane.showMessageDialog(null, "getConnection() 2");
            con = DriverManager.getConnection("jdbc:sqlite:Dados.db");
            JOptionPane.showMessageDialog(null, "getConnection() 3");
        initialize();
        } catch (Exception e) {

            String s = "\nException: " + e.getClass().getName() + "\n";

            for(int i = 0; i < e.getStackTrace().length; i++) {
                s += e.getStackTrace()[i] + "\n";
            }
            JOptionPane.showMessageDialog(null, "Error 1: " + s);

        }
    }

仅限&#34; getConnection()1&#34;显示。所以错误似乎发生在:

Class.forName("org.sqlite.JDBC");

错误是:

enter image description here

的pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.mycompany</groupId>
    <artifactId>ControleMateriais_Maven</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <build>
        <plugins>
            <plugin>
                <version>3.0.2</version>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <mainClass>com.mycompany.controlemateriais_maven.main.Main</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencies>
         <dependency>
        <groupId>org.xerial</groupId>
        <artifactId>sqlite-jdbc</artifactId>
        <version>3.8.11.1</version>
        </dependency>
    </dependencies>



    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
</project>

enter image description here

0 个答案:

没有答案