我從我的appiclation的其他部分調用此類,它工作正常。不過,我想關閉已打開的每個mysql連接。錯誤「ResultSet關閉後不允許操作」在Mysql連接關閉時
這是我的類連接到我的數據庫:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class BACENGQueryDatabases {
public String Query;
public BACENGQueryDatabases(String Query){
this.Query = Query;
}
public ResultSet LoadQuery() throws Exception{
// Accessing Driver From Jar File
Class.forName("com.mysql.jdbc.Driver");
//Get credentials from Property file
BACENGPropertiesFile user = new BACENGPropertiesFile("user.cred");
BACENGPropertiesFile passwd = new BACENGPropertiesFile("passwd.cred");
BACENGPropertiesFile database = new BACENGPropertiesFile("database.system");
BACENGPropertiesFile DBServer = new BACENGPropertiesFile("DBServer.system");
BACENGPropertiesFile DBPort = new BACENGPropertiesFile("DBPort.system");
Connection con = null;
PreparedStatement pst = null;
ResultSet rs = null;
//DB Connection
con = DriverManager.getConnection("jdbc:mysql://"+DBServer.properties()+":"+DBPort.properties()+"/"+database.properties()+"",""+user.properties()+"",""+passwd.properties()+"");
//DB Connection
String sql =Query;
pst = con.prepareStatement(sql);
//System.out.println(sql); //Just to test
rs=pst.executeQuery();
con.close();
return rs;
}
}
當我嘗試關閉連接trought問題正在發生的事情:
con.close();
然後,輸出是:
"Operation not allowed after ResultSet closed"
我非常懷疑你從關閉連接中得到這個錯誤,你在連接關閉後關閉連接(關閉語句,關閉結果集),然後嘗試使用結果集。 – Gimby
然後,將可能關閉連接keepig結果集打開? – Sallyerik
@Sallyerik不,這是不可能的。 –