| 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
|
|
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
|
|
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
|
|
|
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
|
|
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)
|
 |
 |
|
|
subject: Connection to database in separated class
|
|
|