• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Creating classes

 
Jose Vives
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi. I have a generic question and I don't understand how create what I want.
I writing a basic program they can access to one database an do some SQL request, etc. But at the moment I have one big class with all SQL for all the tables from the database. And I want slipt this class in different classes for make the code more easy to read and implement stuff. I think the best solution is create one subclass for each table on the database and on for this class put all the methods related on this class. But later I don't understand how I can call this methods from the class. Because for access to the database I create an object DataBase on the main application and if I want call a method just I do a database.methodIWant().
At the moment I have:

DataBase (this class have all the methods for the databases) extends DataBaseUtil
DataBaseUtil (this class contain all methods and information for connect to the database)

and I crate the database object wit "DataBase db = new DataBase();

And I want something like:


DataBase (this class have all the methods for the databases) extends DataBaseUtil
DataBaseUtil (this class contain all methods and information for connect to the database)
and classes for example:
DogSQL (all the methods for the table dog on the database)
CatSQL (all the methods for the table cat on the database)

and I create the database object wit "DataBase db = new DataBase(); and call methods like db.methodFromDogSQL();

I don't understand how do this. Im starting and my knowledge in java is a bit basic, so I want learn how do this type of stuff. Thanks
 
Campbell Ritchie
Sheriff
Posts: 51349
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I would suggest that if you are going to use a database you start by running all the queries on its command line before you try putting a Java® frontend on it. I am not sure you ought to be thinking about names of classes at this stage. I shall refer you to the Java® Tutorials; that section should provide you with lots of useful information.

I shall also duplicate this discussion in our databases forum, in the hope of getting it more attention.
 
Jose Vives
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At the moment I have all the querys working in one file for two tables, but now I want add another two tables to the database, and I need write all de methods for get information and do all the operations I want. But all in the same class is a bit messy. So I want to create a new class for each table and put all the methods related with that class. But I already have all the sql code working
 
Knute Snortum
Bartender
Pie
Posts: 2890
62
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Definitely put each table into it's own class, or better yet, two classes: one that is the "model" (that is, the data), and one that holds all the access to that data, sometimes call the DAO (I think for Data Access Object).

An Internet search for "java jdbc dao" will get you a lot of information, such as this. (Don't get too caught up in doing things exactly like the example; get the general idea.)

It sounds like you have a DB connection method -- make it into a class and use the Singleton design pattern so there's just one instance of it. Then your DAOs can call the DB connection class, access the data with SQL, and use the data model class to hold and possible pass back the data.

Have fun!

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic