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.

Felix Gerber
  • 1,615
  • 3
  • 30
  • 40
Konz Mama
  • 975
  • 2
  • 8
  • 26
  • 1
    for "intensive" database algorithms its **much** better to use ORM, like JPA, for instance : https://docs.oracle.com/javaee/7/tutorial/partpersist.htm. Everything else will result in a horrible, un-maintainable mess which nobody wants to deal with. – specializt Dec 02 '15 at 09: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 http://stackoverflow.com/questions/25651641/javafx-mysql-connection-example-please (which I probably shouldn't have answered either, for the same reasons, however...) – James_D Dec 02 '15 at 13:02
  • @specializt what do you mean by "intensive" ? does that mean like what we do in most ERP system for corporate ? – Konz Mama Dec 02 '15 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. – James_D Dec 02 '15 at 16:38
  • thank you so much @James_D ill take a look at it and learn it... – Konz Mama Dec 03 '15 at 02:14
  • ... basically, what he said. – specializt Dec 03 '15 at 06:28

2 Answers2

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.

Felix Gerber
  • 1,615
  • 3
  • 30
  • 40
0

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

TT.
  • 15,774
  • 6
  • 47
  • 88
Daniel D.
  • 23
  • 9