How can I make my Java constructor class connect with my dB and mysql ( glass fish server ) . There is a necessity to place these connection commands inside the constructor of a class .If possible ,how ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How can I make my Java constructor class connect with my dB and mysql ( glass fish server ) . There is a necessity to place these connection commands inside the constructor of a class .If possible ,how ?

23rd Sep 2016, 4:42 PM
Nantha kumar .R
Nantha kumar .R - avatar
3 Answers
+ 2
It is bad practices to set up DB connections within the constructor. I suggest you look at using a singleton pattern on google for this. Other than that you set up a connection using JDBC or JPA which one are you trying to achieve both are implemented differently. I suggest looking up persistence in java to mysql server. I will post and example of what you are trying to do.
23rd Sep 2016, 6:14 PM
Catlin
Catlin - avatar
+ 2
Here is a good example for you to look at http://www.javatpoint.com/singleton-design-pattern-in-java follow the link and look through there code.
23rd Sep 2016, 6:17 PM
Catlin
Catlin - avatar
0
thanks dude @Catlin
24th Sep 2016, 11:26 AM
Nantha kumar .R
Nantha kumar .R - avatar