• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

return connection object from a function

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic