• Post Reply Bookmark Topic Watch Topic
  • New Topic

Connection to database in separated class  RSS feed

 
ishiro moh
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!