aspose file tools*
The moose likes JDBC and the fly likes return connection object from a function Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "return connection object from a function" Watch "return connection object from a function" New topic
Author

return connection object from a function

Jason Wylde
Ranch Hand

Joined: May 25, 2009
Posts: 30
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
internet detective
Marshal

Joined: May 26, 2003
Posts: 30537
    
150

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

Joined: May 25, 2009
Posts: 30
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
internet detective
Marshal

Joined: May 26, 2003
Posts: 30537
    
150

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

Joined: May 25, 2009
Posts: 30
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
internet detective
Marshal

Joined: May 26, 2003
Posts: 30537
    
150

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: return connection object from a function