• 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

Single class file to handle connect/disconnect methods for a MySQL database

 
Greenhorn
Posts: 3
Mac Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I will first admit to being new to java coding. Recently, I decided to finally sit down and train myself in the language.

As part of my self-imposed assignment I am building a webapp using a MySQL back-end. A while back, I had built this on a PHP/nuSOAP/MySQL design and now I want to replicate in Java. The architecture for the original design had the following:

1) Relational Database (MySQL 5.1)
2) PHP global variables (to re-use without having to re-write a lot)
3) Central PHP functions file
4) SOA functions file with WSDL creation
5) PHP front end which interacted with the functions file

In Java, I have:

1) Same Database
2) config.properties file with things like (dbuser, dburl, dbpassword, dbdriver, etc.)
3) dbConnection class

Now, I can connect to the database using the following code:



This works as I am able to get the "Database connection established" message in the console. But what I want this class to do is to call two methods that I can access from an external java client (to be constructed):

- dbConnect()
- dbClose()

In this fashion, I can have an external client call 'dbConnect()' and connect to DB, perform SQL queries, call stored procedures, and all that good stuff... and then call 'dbClose()' when is all done! For me, in PHP, this was a very modular approach to handle the database connections from a different functions' file.

The source code above is divided on two sections "SECTION A and SECTION B". Section A is the connection code and SECTION B. The latter is what I need help on. I was thinking about enclosing my SECTION A under a 'public static dbConnect()' and SECTION B to be a 'public static dbClose()'. However, how would I close a connection on a separate method?

Any advice on how to create this 'connection manager'? Thanks in advance


 
Bartender
Posts: 1111
Eclipse IDE Oracle VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
take the code out of main, in fact delete the main method and create 2 methods I would call one getConnection and close
so your code would go



the advantage of this solution is you can alomst seemlessly add in connection pooling if you want to.
 
Xuxo Garcia
Greenhorn
Posts: 3
Mac Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You rock!!!...thanks!
 
reply
    Bookmark Topic Watch Topic
  • New Topic