At the moment I'm also at an JavaFX application with an database connection. The way I chose is the following: Create a SQL-Controller
-Class. This class should contain everything handling your SQL-Data(For example: a connect
-method to open a connection - a close
-method is also not wrong). Use this class in all your controller classes to get the data you need or save the data you have.
Here an little example
The SQLController class could look like that:
public class SqlController {
//Put you connection string with pw, user, ... here
private static final String YOUR_CONNECTION_STRING = "";
public boolean openConnection() {
boolean result;
try {
// Open your connection
result = true;
} catch (Exception e) {
result = false;
}
return result;
}
public boolean closeConnection() {
boolean result;
try {
// Close your connection
result = true;
} catch (Exception e) {
result = false;
}
return result;
}
public YourData getSomeData(){
//get The Data you want.
return YourData;
}
}
You could use the controller in any method of your UI-Controller.
public void handelSomeUiThing()
{
SqlController sc = new SqlController();
sc.openConnection();
YourData = sc.getSomeData();
sc.closeConnection();
}
Hope that helps!
PS: Everyone has his own programming-style. You have to see what fits for your application and what is the most comfortable way for you.