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. :(

KisnardOnline
  • 653
  • 4
  • 16
  • 42
  • This code would work??? u nutz? `Weapon rs.getString("name") =...` doenst work. please understand java first. then understand the fundementals of hibernate then reflection. – DarthVader Nov 07 '12 at 05: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. – KisnardOnline Nov 07 '12 at 05:57
  • @DarthVader I didn't read the `Weapon rs.getString("name")` code =\ – Luiggi Mendoza Nov 07 '12 at 05: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? – Luiggi Mendoza Nov 07 '12 at 06: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. – KisnardOnline Nov 07 '12 at 06:02

3 Answers3

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);
}
Sujay
  • 6,753
  • 2
  • 30
  • 49
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();
Yanick Rochon
  • 51,409
  • 25
  • 133
  • 214
  • 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? – KisnardOnline Nov 07 '12 at 06:12
  • 1
    @JayAvon: Map is an interface, HashMap is an implementation of this interface – Sujay Nov 07 '12 at 06:13
  • Map is an interface, whereas HashMap is a concrete class – Yanick Rochon Nov 07 '12 at 06:13
  • You should read [What does it mean to “program to an interface”?](http://stackoverflow.com/q/383947/1065197) – Luiggi Mendoza Nov 07 '12 at 06:16
  • 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. – KisnardOnline Nov 07 '12 at 06:19
  • keep at it! it's by coding and asking questions that one improves – Yanick Rochon Nov 07 '12 at 06:21
  • that's the idea. Thanks so much guys, much appreciated. Shameless plug in case you are intrigued... www.kisnardonline.com – KisnardOnline Nov 07 '12 at 06:22
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)

Daniel Platon
  • 583
  • 7
  • 16
  • 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. – KisnardOnline Nov 07 '12 at 06:04
  • 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... – Daniel Platon Nov 07 '12 at 06:17