Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

return connection object from a function

 
Jason Wylde
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have a function that I call with the DB info to login to the database and it returns the connection object. Can anyone see a reason to not do it this way or let me know of a better way to do it ? I just thought it would clean up my code a bit rather than having all this database code in the middle of my UI code.


Thanks In Advance
Jason
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34095
337
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jason Wylde wrote: Can anyone see a reason to not do it this way or let me know of a better way to do it ? I just thought it would clean up my code a bit rather than having all this database code in the middle of my UI code.

You are part of the way there. It would be better to have a data access class/package that does all the database work. Passing a connection to the UI code still results in an awful lot of database code in the UI class.
 
Jason Wylde
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So if I separate my data from my UI , what should the data layer return to the UI ? Just the data and then I do with it as I want or something else ? Any suggestions would be great !
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34095
337
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jason Wylde wrote:So if I separate my data from my UI , what should the data layer return to the UI ? Just the data and then I do with it as I want or something else ? Any suggestions would be great !

Yes data. It's typically a Java object. If you don't have a meaningful object (just a collection of data), there's a pattern called "data transfer object" where you create an object just to hold the random data.
 
Jason Wylde
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does it make sense to store the connection info in a static class so anytime I need to connect I can grab the info or are there better ways of doing this ? I don't really need to use connection pooling because it's a very small app.


Thanks In Advance
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34095
337
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jason Wylde wrote:Does it make sense to store the connection info in a static class so anytime I need to connect I can grab the info or are there better ways of doing this ?

No. That invites the connection to go stale. Either get a new one each time or use a connection pool with only one connection in it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic