java.sql.SQLException: Datenbank-Verbindung geschlossen

Ich habe ein problem mit dem SQLite-Treiber für Java (JDBC). Wenn ich will, so laden Sie eine Datenbank, es sagt, dass es geladen wurde, aber instatly es geschlossen wurde. Ich trennte meinen code in verschiedene Dateien:

SQLite.java

package net.jeddsan;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.SQLException;

public class SQLite {
    public static Connection createOrOpenDatabase(String database) {

        String url = "jdbc:sqlite:" + database;

        try (Connection conn = DriverManager.getConnection(url)) {
            if (conn != null) {
                System.out.println("A new database has been created.");
                conn.setAutoCommit(false);
                return conn;
            }else{
                return null;
            }

        } catch (SQLException e) {
            System.out.println(e.getMessage());
            return null;
        }
    }
}

MainController.java

@Override
    public void initialize(URL url, ResourceBundle rb) {
        c = SQLite.createOrOpenDatabase("test.db");

        try {
            sta = c.createStatement();
            sta.executeQuery("INSERT INTO token (token) VALUES ('123')");
            sta.close();
        } catch (SQLException ex) {
            Logger.getLogger(MainController.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            c.close();
        } catch (SQLException ex) {
            Logger.getLogger(MainController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

Log

SCHWERWIEGEND: null
java.sql.SQLException: database connection closed
    at org.sqlite.core.CoreConnection.checkOpen(CoreConnection.java:336)
    at org.sqlite.jdbc4.JDBC4Connection.createStatement(JDBC4Connection.java:38)
    at org.sqlite.jdbc3.JDBC3Connection.createStatement(JDBC3Connection.java:193)
    at net.jeddsan.MainController.initialize(MainController.java:103)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
    at net.jeddsan.koradesktop.start(koradesktop.java:22)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(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$148(WinApplication.java:191)
    at java.lang.Thread.run(Thread.java:745)
  • Die Zeile ist 103 in MainController.java?
  • 103: sta = c.createStatement(); Sie sehen es in den code.
  • Nicht bezogen auf dein problem, aber einige Hinweise: in Ihrem initialize Methode, weisen Sie nicht die Verbindung zu einem Instanz-Feld, sondern um eine lokale variable, und verwenden Sie einen try-mit-Ressourcen anstelle einer handschriftlichen finally-block mit einer close(). Mit einem Instanz-Feld für die Verbindung wird Ihnen nur Schmerzen langfristig.
Schreibe einen Kommentar