Best practices for connecting to a database in Java FX

I am developing an MVC JAVAFX program with an intensive database connection. I do not know where to place the database connection data. Should I define information about the details of the connection in the form of a model and process the connection as a controller, or should I combine them into a single model file.

Ultimately, I need this connection as a session of the entire program before the user logs out.

Is there a simple example that I could study on this. I know using hibernate is the best way, but I'm still learning Java FX, and I need a little about it.

Thanks.

+4
source share
2 answers

JavaFX . , , : SQL-Controller -Class. , SQL- (: a connect -, - close). , , .

SQLController :

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;
   }
}

.

public void handelSomeUiThing()
{
    SqlController sc = new SqlController();
    sc.openConnection();
    YourData = sc.getSomeData();
    sc.closeConnection();
}

, !

PS: . , .

+1

MVC, Spring application.properties...

0

Source: https://habr.com/ru/post/1618253/


All Articles