0

I know that this will not work, which sucks IMO, but I am wondering what is my best solution. After some searching I have found out about ORM and hibernate. After finding out about them I have found many people who say its more hassle than it is worth and promotes laziness. Please can anyone point me in a good direction?

//Setup the weapon classes
logger.info("Initializing the weapon classes.");
stat = conn.prepareStatement("SELECT *" + 
                            " FROM " + DB_NAME + ".weapons");
rs = stat.executeQuery();
while (rs.next()) {
    Weapon rs.getString("name") = new Weapon(rs.getint("weapon_id"), rs.getString("name"), rs.getString("description"), rs.getString("filename"), rs.getint("rarity"), rs.getint("sell_price"), rs.getint("quantity_max"), rs.getint("slot"), rs.getint("level_requirement"), rs.getint("strength_requirement"), rs.getint("dexterity_requirement"), rs.getint("intelligence_requirement"), rs.getint("enchanted_increase"), rs.getint("enchanted_cost"), rs.getint("hit_min"), rs.getint("hit_max"), rs.getint("speed"), rs.getint("elemental_type"), rs.getint("elemental_hit_min"), rs.getint("elemental_hit_max"));
}

I know this has been asked many times, but I am just finding so many contradictory opinions. :(

5
  • This code would work??? u nutz? Weapon rs.getString("name") =... doenst work. please understand java first. then understand the fundementals of hibernate then reflection. Commented Nov 7, 2012 at 5:56
  • I get an error(it won't work-I know that from learning Java)... "The left-hand side of an assignment must be a variable". I want to instantiate an instance of my weapon class, for every entry in the database, using the name field from the db for the instantiated name of the class. Commented Nov 7, 2012 at 5:57
  • @DarthVader I didn't read the Weapon rs.getString("name") code =\ Commented Nov 7, 2012 at 5:57
  • @JayAvon replace the Weapon rs.getString("name") per Weapon weapon and the code will compile and run. Still, it's not clear what you're trying to achieve. Do you need variables with the name of the weapon or similar? Commented Nov 7, 2012 at 6:00
  • @Luiggi Mendoza: yes I would like to have an instance of the weapon class for each entry in my database without hard-coding them all. They should also have some instance name that makes sense. Commented Nov 7, 2012 at 6:02

3 Answers 3

2

I would suggest using a Map to store your instance for weapons that you fetch from your database. The key would be the name and the value would be an instance of your Weapons class.

Something like:

while(rs.next()){
    Weapon weapon = new Weapon(rs.getint("weapon_id"), rs.getString("name"), rs.getString("description"), rs.getString("filename"), rs.getint("rarity"), rs.getint("sell_price"), rs.getint("quantity_max"), rs.getint("slot"), rs.getint("level_requirement"), rs.getint("strength_requirement"), rs.getint("dexterity_requirement"), rs.getint("intelligence_requirement"), rs.getint("enchanted_increase"), rs.getint("enchanted_cost"), rs.getint("hit_min"), rs.getint("hit_max"), rs.getint("speed"), rs.getint("elemental_type"), rs.getint("elemental_hit_min"), rs.getint("elemental_hit_max"));
    //Assuming you have an instance of Map<String, Weapon>
    map.add(rs.getString("name"), weapon);
}
Sign up to request clarification or add additional context in comments.

1 Comment

awesome, I love it. Thanks a bunch. Makes perfect sense and should work well.
1

Using an ORM depends on your project size. Personally, and by looking at your understanding of Java, I would not recommend that you jump into hibernate and such for now. I would suggest that you play directly with SQL first so you have a better understanding of persistence.

As for the code you posted, here is what you could do :

//Setup the weapon classes
logger.info("Initializing the weapon classes.");
stat = conn.prepareStatement("SELECT *" + 
                            " FROM " + DB_NAME + ".weapons");

HashMap<String, Weapon> weapons = new HashMap<String, Weapon>();

rs = stat.executeQuery();
while (rs.next()) {
    Weapon weapon = new Weapon()
        .setId(rs.getint("weapon_id"))
        .setName(rs.getString("name"))
        .setDescription(rs.getString("description"))
        .setFilename(rs.getString("filename"))
        .setRarity(rs.getint("rarity"))
        .setSellPrice(rs.getint("sell_price"))
        .setQuantityMax(rs.getint("quantity_max"))
        .setSlot(rs.getint("slot"))
        .setLvlRequirement(rs.getint("level_requirement"))
        .setStrRequirement(rs.getint("strength_requirement"))
        .setDexRequirement(rs.getint("dexterity_requirement"))
        .setIntRequirement(rs.getint("intelligence_requirement"))
        .setEnchantedInc(rs.getint("enchanted_increase"))
        .setEnchantedCost(rs.getint("enchanted_cost"))
        .setHitMin(rs.getint("hit_min"))
        .setHitMax(rs.getint("hit_max"))
        .setSpeed(rs.getint("speed"))
        .setElementalType(rs.getint("elemental_type"))
        .setElementalHitMin(rs.getint("elemental_hit_min"))
        .setElementalHitMax(rs.getint("elemental_hit_max"));

    weapons.put(rs.getString("name"), weapon);
}

Avoid having a constructor (or even a method!) with so many arguments. Unless you are exporting your class to a third party or exposing it to some user plugins and don't want anyone to change the internal values, don't worry about setters! Then each set* methods return this so they can be chained. For example :

public Weapon setHitMax(int max) {
    this.hitMax = max;
    return this;
}

And you can, then access your weapons using the Map. Like, for example :

weapons.get("sword").getHitMax();

7 Comments

I want to thank both you and Sujay for the answers. I wish I could accept both, but have chosen yours so the next fellow who Google's this post sees the complete code up top. Any important differences between choosing HashMap and Map I should be aware of?
@JayAvon: Map is an interface, HashMap is an implementation of this interface
Map is an interface, whereas HashMap is a concrete class
Thank you and thank you... is now a bad time to mention I have been coding this MMORPG for the past year :P. I want to become a better coder so I'm not starting small.
|
0

What exactly do you mean by "dynamic class creation"? There's two things I can understand from your code:

a) you're a reaallly bad Java programmer :) or

b) you're trying to have your variable named differently for each row in the database, (which doesn't make any sense)

2 Comments

say I have 3 weapons in the db(sword/axe/dagger); I want three instances of the weapon class in my java code. one instance named sword, one named axe, and the last dagger.
Oh, I see. I don't know the big picture, but this seems like a bad idea. You should build a HashMap and, for each row, do a yourMap.put(yourWeaponName, weaponObject). Then, when you need the axe, all you do is yourMap.get("axe") and there's your axe...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.