java.sql.SQLException:数据库连接已关闭 [英] java.sql.SQLException: database connection closed
本文介绍了java.sql.SQLException:数据库连接已关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的Java SQLite驱动程序(JDBC)出现问题.当我要加载数据库时,它说已加载,但始终关闭.我将代码分隔在不同的文件中:
I have a problem with the SQLite driver for Java (JDBC). When I want to load a database, it says that it was loaded, but instatly it was closed. I seperated my code in different files:
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);
}
}
登录
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)
推荐答案
当离开范围时,此try-with-resources
构造将在Connection上调用方法close
:
This try-with-resources
construct will invoke method close
on Connection when you leave its scope:
try (Connection conn = DriverManager.getConnection(url)) {
return conn;
}
因此,您可以有效地返回关闭的连接.
So, you effectively return closed connection.
将其更改为常规的try
构造以返回实时连接:
Change it to regular try
construct to return live connection:
try {
Connection conn = DriverManager.getConnection(url);
return conn;
} catch (SQLException e) {
return null;
}
这篇关于java.sql.SQLException:数据库连接已关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文