Reputation: 975
I am designing a MVC JAVAFX program that has intensive database connection. I dont know where to put the database connection details. Whether I should define the connection details info in a Model form and process the connection as a controller or should I put them together in one single model file.
In the long run I need that connection as a session throughout the program until the user logoff.
Is there any simple example that I could study for this matter. I know using hibernate is the best way but im still learning Java FX and I need someguidance about this.
Thanks.
Upvotes: 4
Views: 2782
Reputation: 23
If you're using MVC, download the Spring Boot dependency and put it in your application.properties
...
Upvotes: 0
Reputation: 1651
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.
Upvotes: 1