4

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.

6
  • 1
    for "intensive" database algorithms its much better to use ORM, like JPA, for instance : docs.oracle.com/javaee/7/tutorial/partpersist.htm. Everything else will result in a horrible, un-maintainable mess which nobody wants to deal with. Commented Dec 2, 2015 at 9:50
  • 1
    This question is too broad: "There are either too many possible answers, or good answers would be too long for this format." You could really write an entire book on this topic. Maybe look at stackoverflow.com/questions/25651641/… (which I probably shouldn't have answered either, for the same reasons, however...) Commented Dec 2, 2015 at 13:02
  • @specializt what do you mean by "intensive" ? does that mean like what we do in most ERP system for corporate ? Commented Dec 2, 2015 at 16:19
  • 1
    @KonzMama You used the word "intensive" first... What do you mean by it? However, it is good practice to 1. encapsulate all your data access code in a class ("Data Access Object"), and 2. program to interfaces. So you define an interface with your data access methods, and then an implementation class. Your first implementation might be JDBC, but --if-- when that becomes unmaintainable, you can define a new implementation using JPA with minimal impact on the rest of your application. Commented Dec 2, 2015 at 16:38
  • thank you so much @James_D ill take a look at it and learn it... Commented Dec 3, 2015 at 2:14

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

so where do you store your connection details, like username, password and connection string ? U store it as a model >??
I would do a constant, see my edit in the SQLController
SqlController ? is this a new term?
0

If you're using MVC, download the Spring Boot dependency and put it in your application.properties...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.