Guest User

Untitled

a guest
Jul 22nd, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.80 KB | None | 0 0
  1. public void saveNewToDB() {
  2. Connection con = DatabaseConnection.getConnection();
  3. PreparedStatement ps = null;
  4. ResultSet rs = null;
  5. try {
  6. // clients should not be able to log back before their old state is saved (see MapleClient#getLoginState) so we are safe to switch to a very low isolation level here
  7. con.setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);
  8. // connections are thread local now, no need to
  9. // synchronize anymore =)
  10. con.setAutoCommit(false);
  11. ps = con.prepareStatement("INSERT INTO characters " + "(`hair`, `face`, `accountid`, `name`, `defaultaccid`)" + //6
  12. "VALUES (?, ?, ?, ?, ?);"); //6
  13. ps.setInt(1, hair);
  14. ps.setInt(2, face);
  15. ps.setInt(3, accountid);
  16. ps.setString(4, name);
  17. ps.setInt(5, accountid);
  18. ps.executeUpdate();
  19. rs = ps.getGeneratedKeys();
  20. if (rs.next()) {
  21. this.id = rs.getInt(1);
  22. } else {
  23. rs.close();
  24. ps.close();
  25. throw new DatabaseException("Inserting char failed.");
  26. }
  27. rs.close();
  28. ps.close();
  29. // Pets not saved for new char
  30. // Skill Macros not saved for new Char
  31. // Deleting inventory is not needed for new char. so removed.
  32. ps = con.prepareStatement("INSERT INTO inventoryitems (characterid, itemid, inventorytype, position, quantity, owner, uniqueid, expiredate) VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
  33. PreparedStatement pse = con.prepareStatement("INSERT INTO inventoryequipment VALUES (DEFAULT, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
  34. for (MapleInventory iv : inventory) {
  35. ps.setInt(3, iv.getType().getType());
  36. for (IItem item : iv.list()) {
  37. ps.setInt(1, id);
  38. ps.setInt(2, item.getItemId());
  39. ps.setInt(4, item.getPosition());
  40. ps.setInt(5, item.getQuantity());
  41. ps.setString(6, item.getOwner());
  42. ps.setInt(7, item.getUniqueId());
  43. ps.setLong(8, item.getExpiration());
  44. ps.executeUpdate();
  45. rs = ps.getGeneratedKeys();
  46. int itemid;
  47. if (rs.next()) {
  48. itemid = rs.getInt(1);
  49. } else {
  50. rs.close();
  51. ps.close();
  52. throw new DatabaseException("Inserting char failed.");
  53. }
  54. if (iv.getType().equals(MapleInventoryType.EQUIP) || iv.getType().equals(MapleInventoryType.EQUIPPED)) {
  55. pse.setInt(1, itemid);
  56. IEquip equip = (IEquip) item;
  57. pse.setInt(2, equip.getUpgradeSlots());
  58. pse.setInt(3, equip.getLevel());
  59. pse.setInt(4, equip.getStr());
  60. pse.setInt(5, equip.getDex());
  61. pse.setInt(6, equip.getInt());
  62. pse.setInt(7, equip.getLuk());
  63. pse.setInt(8, equip.getHp());
  64. pse.setInt(9, equip.getMp());
  65. pse.setInt(10, equip.getWatk());
  66. pse.setInt(11, equip.getMatk());
  67. pse.setInt(12, equip.getWdef());
  68. pse.setInt(13, equip.getMdef());
  69. pse.setInt(14, equip.getAcc());
  70. pse.setInt(15, equip.getAvoid());
  71. pse.setInt(16, equip.getHands());
  72. pse.setInt(17, equip.getSpeed());
  73. pse.setInt(18, equip.getJump());
  74. pse.setBoolean(19, equip.isRing());
  75. pse.setInt(20, equip.getHammers());
  76. pse.executeUpdate();
  77. }
  78. }
  79. }
  80. ps.close();
  81. pse.close();
  82. //Deleting Keyboard is not needed for new characters
  83. ps = con.prepareStatement("INSERT INTO keymap (characterid, `key`, `type`, `action`) VALUES (?, ?, ?, ?)");
  84. ps.setInt(1, id);
  85. for (Entry<Integer, MapleKeyBinding> keybinding : keymap.entrySet()) {
  86. ps.setInt(2, keybinding.getKey());
  87. ps.setInt(3, keybinding.getValue().getType());
  88. ps.setInt(4, keybinding.getValue().getAction());
  89. ps.addBatch();
  90. }
  91. ps.executeBatch();
  92. ps.close();
  93. // no need for saved locations for new char
  94. // no buddies for new char
  95. //storage and account details doesnt have to be saved for new character
  96. con.commit();
  97. } catch (Exception e) {
  98. log.error(MapleClient.getLogMessage(this, "[charsave] Error saving new character data"), e);
  99. try {
  100. con.rollback();
  101. } catch (SQLException e1) {
  102. log.error(MapleClient.getLogMessage(this, "[charsave] Error Rolling Back"), e);
  103. }
  104. } finally {
  105. try {
  106. if (rs != null) {
  107. rs.close();
  108. }
  109. if (ps != null) {
  110. ps.close();
  111. }
  112. con.setAutoCommit(true);
  113. con.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
  114. } catch (SQLException e) {
  115. log.error(MapleClient.getLogMessage(this, "[charsave] Error saving new character going back to autocommit mode"), e);
  116. }
  117. }
  118. }
Add Comment
Please, Sign In to add comment