2012-11-07 22 views
0

我知道這不起作用,這吸引IMO,但我想知道什麼是我最好的解決方案。經過一番搜索,我發現了關於ORM和休眠。在瞭解他們之後,我發現很多人說它比它更值得花費,並且提倡懶惰。請任何人都指向一個好的方向嗎?在java解決方案中創建動態類

//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")); 
} 

我知道這已被問了很多次,但我只是發現了這麼多矛盾的意見。 ?:(

+0

此代碼將工作?你好嗎? '武器rs.getString(「名稱」)= ...''做的工作。請先了解Java。然後理解hibernate的思想,然後反思。 – DarthVader

+0

我得到一個錯誤(這不會起作用 - 我知道從學習Java開始)......「作業的左側必須是一個變量」。我想爲數據庫中的每個條目實例化我的武器類的實例,使用db中的名稱字段作爲類的實例化名稱。 – KisnardOnline

+0

@DarthVader我沒有閱讀'Weapon rs.getString(「name」)'code = \ –

回答

1

使用ORM取決於你的項目規模。就個人而言,通過查看你對Java的理解,我會而不是建議你現在跳入休眠等。我建議你先直接使用SQL,以便更好地理解持久性。

至於你發佈的代碼,這裏是你能做什麼:具有構造

//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); 
} 

避免(或者甚至是方法!)有這麼多的爭論。除非您將您的課程導出到第三方或將其暴露給某些用戶插件,並且不希望任何人更改內部值,否則不要擔心設置者!然後每個set*方法返回this,以便它們可以鏈接。例如:

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

你可以,然後使用Map訪問你的武器。例如:

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

我想感謝你和Sujay的答案。我希望我可以接受這兩種方式,但是選擇了你的方式,這樣Google的這個職位的下一位成員就可以看到完整的代碼。選擇HashMap和Map I之間的任何重要區別應該意識到? – KisnardOnline

+1

@JayAvon:Map是一個接口,HashMap是這個接口的實現 – Sujay

+0

Map是一個接口,而HashMap是一個具體的類 –

0

究竟你的「動態類的創建」的意思有兩件事情我可以從你的代碼的理解:

一)你是一個壞reaallly Java程序員:) 或

b)您想擁有你的變量數據庫中的每一行,(其中沒有任何意義)

+0

說我在db(劍/斧/匕首)有3件武器;我想在我的java代碼中使用武器類的三個實例。一個叫劍,一個叫斧頭,最後一把匕首。 – KisnardOnline

+0

哦,我明白了。我不知道大局,但這似乎是一個壞主意。你應該建立一個HashMap,併爲每一行做一個yourMap.put(yourWeaponName,weaponObject)。然後,當你需要斧頭時,你所做的就是你的Map.get(「斧頭」),那裏有你的斧頭...... –

2

我會建議使用Map來存儲您的實例,你從獲取武器的命名不同你的數據庫。關鍵將是名稱和價值將是你的武器類的一個實例。

喜歡的東西:

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); 
} 
+0

真棒,我喜歡它。謝謝一堆。具有完美的意義,應該工作得很好。 – KisnardOnline

+0

@JayAvon:很高興幫助:) – Sujay