我试图执行应该使用数据库中的字段创建新对象的方法,每次运行此代码时,我都会得到SQLException:ResultSet已关闭。
public DatabasedClient getDatabaseClient(int clientDatabaseid){
if(DatabaseClientUtil.isInDatabase(clientDatabaseid)){
return DatabaseClientUtil.getDBClient(clientDatabaseid);
}else{
try{
System.out.println("Trying to find user in db");
ResultSet rs = fbot.getStorage().query("select * from database_name where clientDBId = " + clientDatabaseid);
System.out.println("deb " + rs.getString("nick"));
while (rs.next()) {
DatabasedClient databasedClient = new DatabasedClient(clientDatabaseid);
databasedClient.setUid(rs.getString("uid"));
databasedClient.setNick(rs.getString("nick"));
databasedClient.setLastConnect(rs.getLong("lastConnected"));
databasedClient.setLastDisconnect(rs.getLong("lastDisconnect"));
databasedClient.setTimeSpent(rs.getLong("timeSpent"));
databasedClient.setLongestConnection(rs.getLong("longestConnection"));
return databasedClient;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
return null;
}
}
我使用hikari,这里是AbstractStorage类的方法
@Override
public void execute(String query) throws SQLException {
try (Connection connection = getConnection()){
connection.prepareStatement(query).executeUpdate();
}
}
@Override
public ResultSet query(String query) throws SQLException {
try (Connection connection = getConnection()) {
return connection.prepareStatement(query).executeQuery();
}
}
我希望有人能帮助我。
答案 0 :(得分:2)
我认为您看到的确切错误是由以下代码行引起的:
public class ElectricalDepartmentController {
public void initialize() throws IOException {
StageAddController stageAddController = new StageAddController();
stageAddController.setElectricalDepartmentController(this);
workersTableColumn.setCellValueFactory(new PropertyValueFactory<>("nameSurname"));
rowSelected();
}
@FXML
public void populateTable() {
for(Worker worker : workerDao.getWorkersNameSurname("Electrical")) workersList.addAll(worker);
workersTable.setItems(workersList);
}
@FXML
public void refreshButtonClicked() {
workersList.removeAll(workersList);
populateTable();
}
}
在将光标前进到第一条记录之前,您正尝试访问结果集。此外,您的方法System.out.println("deb " + rs.getString("nick"));
返回一个对象,该对象在概念上映射到查询中的单个预期记录。因此,在结果集上迭代一次似乎是有意义的。考虑到所有这些因素,我们可以尝试以下方法:
getDatabaseClient