JDBC SqlLite和Maven

时间:2018-11-28 17:42:55

标签: eclipse maven sqlite jdbc path

我的项目有问题。 我与sqllite数据库建立了jdbc连接。但是我不能使用相对路径,我总是会出错。 那是我的设置。在db-layer项目中。在GradingSystemDao类中。

private static final String DB_LINK = "jdbc:sqlite:C:\\Users\\Leumas\\eclipse-workspace\\SA\\db-layer\\db-layer\\d\\test.db";

public GradingSystemDao() {
    // constructor
}

/**
 * Creates the link to the db.
 * 
 * @return The connection of the db.
 */
private Connection getConnection() {
    System.out.println("DBLINK: " + DB_LINK);
    Connection c = null;
    try {
        Class.forName("org.sqlite.JDBC");
        c = DriverManager.getConnection(DB_LINK);
    } catch (SQLException | ClassNotFoundException e) {
        System.out.println("Connection do not work.");

        e.printStackTrace();
    }
    return c;
}

当我使用相对路径时,我得到数据库已关闭错误。此网址中存在相同的错误: https://github.com/xerial/sqlite-jdbc/issues/354

但是我无法解决它,我的路径不是很长。

我认为这是我的项目结构,由maven制作而成。

project structure

我在pom.xml中的设置在逻辑上。

    <dependency>
        <groupId>de.sf.grasys</groupId>
        <artifactId>db-layer</artifactId>
        <version>3.0.0</version>
    </dependency>

我在用户界面的pom.xml中的设置。

    <dependency>
        <groupId>de.sf.grasys</groupId>
        <artifactId>logic</artifactId>
        <version>3.0.0</version>
    </dependency>

当我还建立一个可执行jar时,它不起作用。  我希望你能帮助我。

修改

这只是一个小项目,因此异常处理不是那么重要。

DBLINK: jdbc:sqlite:..\d\test.db
Connection do not work.
java.sql.SQLException: path to '..\d\test.db': 'C:\Users\Leumas\eclipse-workspace\SA\userinterface\..\d' does not exist
at org.sqlite.core.CoreConnection.open(CoreConnection.java:192)
at org.sqlite.core.CoreConnection.<init>(CoreConnection.java:76)
at org.sqlite.jdbc3.JDBC3Connection.<init>(JDBC3Connection.java:25)
at org.sqlite.jdbc4.JDBC4Connection.<init>(JDBC4Connection.java:24)
at org.sqlite.SQLiteConnection.<init>(SQLiteConnection.java:45)
at org.sqlite.JDBC.createConnection(JDBC.java:114)
at org.sqlite.JDBC.connect(JDBC.java:88)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:270)
at  de.sf.grasys.persistence.GradingSystemDao.getConnection(GradingSystemDao.java:49)
at de.sf.grasys.persistence.GradingSystemDao.getAllSchoolClass(GradingSystemDao.java:269)
at de.sf.grasys.service.GradingSystem.loadDataFromDatabase(GradingSystem.java:33)
at de.sf.grasys.ui.Main.start(Main.java:47)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
at java.lang.Thread.run(Thread.java:748)

2 个答案:

答案 0 :(得分:0)

在运行应用程序时,您对“当前工作文件夹”的期望是错误的。您使用的绝对路径是:

C:\Users\Leumas\eclipse-workspace\SA\db-layer\db-layer\d\test.db

基于错误,相对路径解析为

C:\Users\Leumas\eclipse-workspace\SA\d\test.db

问题是文件夹C:\Users\Leumas\eclipse-workspace\SA\d不存在。

确保您的相对路径指向现有文件夹。

答案 1 :(得分:0)

我只在本地找到解决方案。

private static final String DB_LINK = "jdbc:sqlite:..\\db-layer\\db-layer\\d\\test.db";

修改 好的,它可以在eclipse中工作,但是如果我创建一个可执行jar,它就行不通了。

 C:\Users\Leumas\Desktop>java -jar Benotungssystem.jar
 Connection do not work.
java.sql.SQLException: path to '..\db-layer\db-layer\d\test.db':        'C:\Users\Leumas\Desktop\..\db-layer' does not exist
    at org.sqlite.core.CoreConnection.open(CoreConnection.java:192)
    at org.sqlite.core.CoreConnection.<init>(CoreConnection.java:76)
    at org.sqlite.jdbc3.JDBC3Connection.<init>(JDBC3Connection.java:25)
    at org.sqlite.jdbc4.JDBC4Connection.<init>(JDBC4Connection.java:24)
    at org.sqlite.SQLiteConnection.<init>(SQLiteConnection.java:45)
    at org.sqlite.JDBC.createConnection(JDBC.java:114)
    at org.sqlite.JDBC.connect(JDBC.java:88)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at de.sf.grasys.persistence.GradingSystemDao.getConnection(GradingSystemDao.java:48)
    at de.sf.grasys.persistence.GradingSystemDao.getAllSchoolClass(GradingSystemDao.java:268)
    at de.sf.grasys.logic.GradingSystem.loadDataFromDatabase(GradingSystem.java:34)
    at de.sf.grasys.ui.Main.start(Main.java:47)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
    at java.lang.Thread.run(Unknown Source)

我希望路径在项目中,以便它可在其他PC上使用。