• 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

Using same database connection

 
Ranch Hand
Posts: 69
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
I have a main Jframe and then some jdialog depends of the jframe.
In one jdialog I set up a database connection that after I would like to use in lifecycle of application.
How is the best way to make that? With object connection public? Could you suggest me how could I do that?
Thanks and sorry for my English
 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess it kinda depends on what kind of information you require the user to enter in the dialog. One way to do it is have the dialog return a DataSource, which you can then pass from your frame to the persistence layer.
 
Saloon Keeper
Posts: 27763
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It sounds like what you probably need is a singleton object that you can obtain the Connection from (have it create the Connection the first time it is called).

For multi-user applications (Swing apps usually aren't), there are some Database Connection Pool libraries such as Apache dbcp that can be used to manage shared pools with multiple connections in them. Apps like Apache Tomcat use* this instead of inventing their own).

===
* Actually, up to about Tomcat 7, they used Apache dbcp, I think later versions switched to a different provider. Tomcat's connection pooler was always plug-replaceable though.
 
david luis
Ranch Hand
Posts: 69
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:I guess it kinda depends on what kind of information you require the user to enter in the dialog. One way to do it is have the dialog return a DataSource, which you can then pass from your frame to the persistence layer.





These is my organizacion:

JFRAME (main) -> multiple buttons that open JDIALOGs to make querys in database and one of them open Jdialog to configure database (for instance JDialogDatabase)
JDIALOG JDialogDatabase with JTextField with name database, user, port, pass...., and Jbutton to text connectivity of that configuration
CLASS CONNECTION -> Is called from JDIALOG JDialogDatabase to connect to database and return to JDIALOG the output of the connection to see the connection is established.

How could you organize better that?

Thanks!


 
david luis
Ranch Hand
Posts: 69
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:It sounds like what you probably need is a singleton object that you can obtain the Connection from (have it create the Connection the first time it is called).

For multi-user applications (Swing apps usually aren't), there are some Database Connection Pool libraries such as Apache dbcp that can be used to manage shared pools with multiple connections in them. Apps like Apache Tomcat use* this instead of inventing their own).

===
* Actually, up to about Tomcat 7, they used Apache dbcp, I think later versions switched to a different provider. Tomcat's connection pooler was always plug-replaceable though.




I don't want multi-user configuration. These is my organizacion:

JFRAME (main) -> multiple buttons that open JDIALOGs to make querys in database and one of them open Jdialog to configure database (for instance JDialogDatabase)
JDIALOG JDialogDatabase with JTextField with name database, user, port, pass...., and Jbutton to text connectivity of that configuration
CLASS CONNECTION -> Is called from JDIALOG JDialogDatabase to connect to database and return to JDIALOG the output of the connection to see the connection is established.

How could you organize better that?

Thanks!
reply
    Bookmark Topic Watch Topic
  • New Topic