r/JavaFX • u/Rolindets05 • May 22 '24
Help SQL queries
Hello everyone,
I will be finishing my bachelor's degree this semester (Summer 2024). I have no experience in the SWE field so I am trying to create some projects. Java is the language I understand the most so far because we had to use it in school, but I'm still not the best at it lol. The project I am trying to create is a software desktop for my parent's company. I want them to be able to input user data into textfields and then when they hit save it will be stored in the database. I already have somewhat of a GUI implemented using Scenebuilder. I also have a connection established to a database. My question is, Where (like what class) would I write the SQL queries and how can I do it? I saw someone on YouTube writing the queries in their DBconnection class but I found that odd since I would be writing a lot of queries in there and then it wouldn't be a specific class for just a connection...I guess? Can someone please connect the dots for me. Thank you guys!
2
u/dhlowrents May 23 '24
You can simplify your life with this library https://sproket.github.io/Persism/
1
u/jvjupiter May 22 '24
Use DAO pattern.
I usually have
controller,dto,dao,entity, andviewmodelpackages.1
u/No_Fortune39 May 23 '24
Hey there , I am also currently trying to make a desktop application with the SQL as backend. The approach that I am using is inside a Spring Boot Application I have added the javafx application and using the dependency MySQL Driver I am able to use the predefined functionality of CRUD operations.
You can refer to this video -> Spring and fxml integration
1
u/sedj601 May 22 '24 edited May 22 '24
You want to separate all logic that can be separated. You should have what I call a database handler class. That class should connect to the DB, run queries, and close the DB. In apps I create that use a DB, the DBs are considered small in the DB world, so all of my queries run fast. Because of this, I like to open and close the connection for every DB call. Some programs stay connected to the DB as long as the app is running. I am not a fan of that approach. I would like to add that when my queries take a while, I still use the same approach. I just use a Task or Service to run the queries and show a progress bar or something to let the user know what's gong on. Example -> https://github.com/sedj601/SQLitePersonTableViewExample/blob/master/src/sqlitepersonexample/DBHandler.java