So currently i'm working on a project with allot of connections to a MySQL database. This is done with JDBC (mysql-connector-java-5.1.28-bin). Since some of the query's being sent to the database will take a while i want to place some indicator showing the user that the program haven't frozen. To do this i use a JavaFX ProgressIndicator in indeterminate mode. To start with this indicator isn't visible so to show it i simply use setVisible(true) but it isn't working.
Code explaining
The following code shows what happens when a user press the a button. So first i set the indicator to visible and secondly i call the method authenticate in the if statement. In my eye's this look clear and should work without a problem like the others methods i have that doesn't use the connection to the database.
What happens instead is that ProgressIndicator gets the assigned value but then for some reason it doesn't become visible. Instead the connection to MySQL is done and after about 2 seconds both the data and my ProgressIndicator shows up at the exact same time.
@FXML
public void login(ActionEvent event) {
working_ProgressIndicator.setVisible(true);
if(SystemManager.db().authenticate(username_TextField.getText(), password_Field.getText()) == true) {
Stage currentStage = (Stage) username_TextField.getScene().getWindow();
SystemManager.setAuthenticatedUser(username_TextField.getText());
SystemManager.toMainScene(currentStage);
} else {
show(info_Label, "Login failed", "ERROR");
System.out.println("Login failed");
}
working_ProgressIndicator.setVisible(false);
}
Code for the connecton to MySQL follows:
private void connect(){
try{
dbConnection = DriverManager.getConnection(dbUrl+db, dbUser, dbPassword);
}catch(Exception e){
System.out.println("Could not connect");
}
}
private void disconnect(){
try{
dbConnection.close();
}catch(Exception ex){
System.out.println("Could not disconnect");
}
}
private void requestToHashMap(ArrayList<HashMap> returnedRows, String query){
connect();
try{
Statement st = dbConnection.createStatement();
ResultSet rs = st.executeQuery(query);
ResultSetMetaData rsmd = rs.getMetaData();
int columnCount = rsmd.getColumnCount();
while(rs.next()){
HashMap<String, String> tmpMap = new HashMap<>();
for (int i = 1; i < columnCount + 1; i++) {
tmpMap.put(rsmd.getColumnName(i), rs.getString(rsmd.getColumnName(i))); keys to column name, Also fetch the result and place in Value.
}
returnedRows.add(tmpMap);
tmpMap = null;
}
}catch(Exception ex){
ex.printStackTrace();
}
disconnect();
}
What i have tried
I tried slowly increasing the opacity of the ProgressIndicator with a loop. Further more i also added another if statement so that the connection only is executed if the opacity is 1. This resulted in the connection never executing at all, it is being skipped for some reason. The only logic reason for this to happen as i see it is that the connection to the database for some reason is run before the loop has finished running the opacity changes?
Can someone explain to me why this is happening and how i can avoid it?