6 Replies - 491 Views - Last Post: 20 May 2010 - 10:35 AM Rate Topic: -----

#1 Guest_Tronsho*


Reputation:

Working with DataBase

Posted 20 May 2010 - 07:36 AM

Hi all!
I am working on a simple RPG game, using MS Access database.
I have set the connection and everything is fine.
It is just a matter of designing my classes.
I have two ways I am not sure what to choose...
Let's take for example the Armor class.

first way:
When I want to create a new Armor, I'll pass the constructor an id.
by that id I want to retrieve all data from the armor table for this armor Object.
now, I have a database class.
should I have inside that class methods like:
public ResultSet getWeaponById(int id){
..
public ResultSet getArmorById(int id){
..

the methods only get the data from the database using the query, and return a ResultSet Object, where i will have a method setData(ResultSet rs) to set the data of that Armor.

second way:
Should I create the Armor, or any other Object inside the database class already, and return the Object itself, like:
public Armor getArmorById(int id){
//get armor from database using the query and id
//iterate the ResultSet to create a new Armor Object
//return the armor object.
}


Personally I prefer the second way. but just wanted to hear what you think.
Thanks :)

Is This A Good Question/Topic? 0

Replies To: Working with DataBase

#2 Paul-  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 61
  • View blog
  • Posts: 260
  • Joined: 11-December 09

Re: Working with DataBase

Posted 20 May 2010 - 07:55 AM

The second option is definitely better. You should separate the database access code from the program logic into different classes. That allows for easy switching to a different type of database if needed. And speaking of that, why did you choose MS Access? It seems like HSQLDB or Java DB/Derby are logical choices when working in Java.
Was This Post Helpful? 0
  • +
  • -

#3 Guest_Tronsho*


Reputation:

Re: Working with DataBase

Posted 20 May 2010 - 09:15 AM

Thanks Paul-!
to tell the truth I didn't even here about the DBs you mentioned.
I'll check them thanks!

Quote

You should separate the database access code from the program logic into different classes. That allows for easy switching to a different type of database if needed

could you please explain further what you mean? thanks!
Was This Post Helpful? 0

#4 Paul-  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 61
  • View blog
  • Posts: 260
  • Joined: 11-December 09

Re: Working with DataBase

Posted 20 May 2010 - 09:45 AM

View PostTronsho, on 20 May 2010 - 08:15 AM, said:

could you please explain further what you mean? thanks!


Suppose you have a class called Weapon, and another class, a container for weapons, called WeaponList. You probably want to be able to add and delete weapons from the container, find out the total number of weapons in the container, or retrieve individual weapons by ID. WeaponList could be implemented with a database, but it could also be based on a plain array, or some other data structure. The implementation details should be hidden to the outside world. This allows updating the WeaponList class with a new version that uses a completely different storage approach, without affecting the rest of your program.

If however, any of your methods return a data type dependent on SQL, or MS Access, you will have to make changes in many different parts of your program if you decide on a different storage model.
Was This Post Helpful? 2
  • +
  • -

#5 Guest_Tronsho*


Reputation:

Re: Working with DataBase

Posted 20 May 2010 - 10:15 AM

I see! Nakes a lot of sense!
so the WeponList should get a DataBase Object, to use the Connection Object I conected with?
cause if i don't pass the Conneection Object, how will I execute my queries?
Was This Post Helpful? 0

#6 Paul-  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 61
  • View blog
  • Posts: 260
  • Joined: 11-December 09

Re: Working with DataBase

Posted 20 May 2010 - 10:32 AM

View PostTronsho, on 20 May 2010 - 09:15 AM, said:

so the WeponList should get a DataBase Object, to use the Connection Object I conected with?
cause if i don't pass the Conneection Object, how will I execute my queries?


Sounds good to me.
Was This Post Helpful? 0
  • +
  • -

#7 Guest_Tronsho*


Reputation:

Re: Working with DataBase

Posted 20 May 2010 - 10:35 AM

Thanks a lot Paul-!
you are great!!
You have been a huge help! :)
Was This Post Helpful? 0

Page 1 of 1