File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Connection to database in separated class Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Connection to database in separated class " Watch "Connection to database in separated class " New topic
Author

Connection to database in separated class

ishiro moh
Greenhorn

Joined: May 27, 2012
Posts: 6
Hello,
establishing connection in every new class i create is now painful if i want to change some informations in database connection.
What should i do to have a separated class for database connection instead of always copy-past this:

Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 5844
    
    5

ishiro moh wrote:
What should i do to have a separated class for database connection instead of always copy-past this:


Define a class. Whatever information you're currently copy/pasting, put it instead into that class. Then replace all those copy pastes with uses of that class.

I really don't understand the question. If you're writing Java code and using JDBC and aware of the idea that repeated code should be put into its own class, I gotta figure you already know everything you need to know.

You know how to define a class, right?
You know how to provide that class with state, either in a constructor or in methods, right?
You know how to use a class to perform services for you and, if appropriate, return results from actions, right?

So what are you having trouble with?
ishiro moh
Greenhorn

Joined: May 27, 2012
Posts: 6
The problem is that it doesnt work

here is the class i created:



when i call it from another class id doent work



what i'm doing wrong?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 16687
    
  19

ishiro moh wrote:The problem is that it doesnt work

here is the class i created:



when i call it from another class id doent work



what i'm doing wrong?



You need to actually call the method -- all your code does is instantiate a dbconnect object.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32654
    
    4
You have made the mistake of creating a method with the same name as the class. If you stick to the conventions about Capital Letters, and remember you mustn’t give a constructor a return type, that will no longer happen.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 5844
    
    5

ishiro moh wrote:The problem is that it doesnt work


Although others have already pointed you in the right direction, please note that ItDoesntWorkIsUseless.(⇐click) You need to TellTheDetails.(⇐click)
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Connection to database in separated class
 
Similar Threads
BeanInstantiationException
Ensim Linux & Tomcat issues, can't MySQL - anyone?
"Function sequence error!" what is it mean
static connection
java.sql.SQLException: Server configuration denies access to data source