• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Using a connection class?

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am creating a Java connection to a MySQL database and I have several different classes which will be utilizing that connection for various purposes. This will involve different MySQL user accounts where the user logs in to the database and performs the necessary actions.

So, I guess my question is:
Is there a way to create a Connection class for a persistent connection to the database, so long as a specified user is logged into the system, that can be reused in other classes for authentication? If so, how can I use

If I am trying to go about this all wrong then let me know that as well.

Any help would be very....Helpful!

Thanks,
Bryan
 
Rancher
Posts: 43028
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A connection is tied to a user. While you can keep open a connection and pass it around to different classes, it will always be for that specific user. For a different user, you have to open a new connection.
 
Bryan Scarbrough
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is there a way to place the following in its own class:



I am trying to prevent placing this into every file class that I am using for queries.

Thanks for your reply.
Hope this is a little more clear.
 
Ulf Dittmer
Rancher
Posts: 43028
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can make it a static method in some utility class. That way you can access it from everywhere. The Class.forName().getInstance() only needs to be done once - you don't need to repeat it for every connection.
 
Bryan Scarbrough
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Once I set it up as a method to a utility class, then how can I reuse that information? Is there a way I can return the result set and then utilize that for creating statements, etc? If so, how?

I have been working on this for a couple of days now and more hours than I care to admit and I am completely lost at this point!
 
Ulf Dittmer
Rancher
Posts: 43028
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It would go something like this: In Helperclass.java you would have:

public static Connection getConnection (String url, String user, String password) {
try {
return DriverManager.getConnection( url, user, password );
} catch( Exception x ) {
System.err.println( x );
return null;
}

and then in your application:

static {
try {
Class.forName( driver ).newInstance("com.mysql.jdbc.Driver");
} catch (Exception ex) {
System.err.println(ex.getMessage());
}
}

and later on

Connection con = Helperclass.getConnection(url, user, password);
// do something with con; of course, it could be null!

If the URL is always the same (i.e., you're using the same DB all the time), you could have the helper class take care of that as well.
[ August 09, 2005: Message edited by: Ulf Dittmer ]
 
Slime does not pay. Always keep your tiny ad dry.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic