Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void saveNewToDB() {
- Connection con = DatabaseConnection.getConnection();
- PreparedStatement ps = null;
- ResultSet rs = null;
- try {
- // 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
- con.setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);
- // connections are thread local now, no need to
- // synchronize anymore =)
- con.setAutoCommit(false);
- ps = con.prepareStatement("INSERT INTO characters " + "(`hair`, `face`, `accountid`, `name`, `defaultaccid`)" + //6
- "VALUES (?, ?, ?, ?, ?);"); //6
- ps.setInt(1, hair);
- ps.setInt(2, face);
- ps.setInt(3, accountid);
- ps.setString(4, name);
- ps.setInt(5, accountid);
- ps.executeUpdate();
- rs = ps.getGeneratedKeys();
- if (rs.next()) {
- this.id = rs.getInt(1);
- } else {
- rs.close();
- ps.close();
- throw new DatabaseException("Inserting char failed.");
- }
- rs.close();
- ps.close();
- // Pets not saved for new char
- // Skill Macros not saved for new Char
- // Deleting inventory is not needed for new char. so removed.
- ps = con.prepareStatement("INSERT INTO inventoryitems (characterid, itemid, inventorytype, position, quantity, owner, uniqueid, expiredate) VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
- PreparedStatement pse = con.prepareStatement("INSERT INTO inventoryequipment VALUES (DEFAULT, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
- for (MapleInventory iv : inventory) {
- ps.setInt(3, iv.getType().getType());
- for (IItem item : iv.list()) {
- ps.setInt(1, id);
- ps.setInt(2, item.getItemId());
- ps.setInt(4, item.getPosition());
- ps.setInt(5, item.getQuantity());
- ps.setString(6, item.getOwner());
- ps.setInt(7, item.getUniqueId());
- ps.setLong(8, item.getExpiration());
- ps.executeUpdate();
- rs = ps.getGeneratedKeys();
- int itemid;
- if (rs.next()) {
- itemid = rs.getInt(1);
- } else {
- rs.close();
- ps.close();
- throw new DatabaseException("Inserting char failed.");
- }
- if (iv.getType().equals(MapleInventoryType.EQUIP) || iv.getType().equals(MapleInventoryType.EQUIPPED)) {
- pse.setInt(1, itemid);
- IEquip equip = (IEquip) item;
- pse.setInt(2, equip.getUpgradeSlots());
- pse.setInt(3, equip.getLevel());
- pse.setInt(4, equip.getStr());
- pse.setInt(5, equip.getDex());
- pse.setInt(6, equip.getInt());
- pse.setInt(7, equip.getLuk());
- pse.setInt(8, equip.getHp());
- pse.setInt(9, equip.getMp());
- pse.setInt(10, equip.getWatk());
- pse.setInt(11, equip.getMatk());
- pse.setInt(12, equip.getWdef());
- pse.setInt(13, equip.getMdef());
- pse.setInt(14, equip.getAcc());
- pse.setInt(15, equip.getAvoid());
- pse.setInt(16, equip.getHands());
- pse.setInt(17, equip.getSpeed());
- pse.setInt(18, equip.getJump());
- pse.setBoolean(19, equip.isRing());
- pse.setInt(20, equip.getHammers());
- pse.executeUpdate();
- }
- }
- }
- ps.close();
- pse.close();
- //Deleting Keyboard is not needed for new characters
- ps = con.prepareStatement("INSERT INTO keymap (characterid, `key`, `type`, `action`) VALUES (?, ?, ?, ?)");
- ps.setInt(1, id);
- for (Entry<Integer, MapleKeyBinding> keybinding : keymap.entrySet()) {
- ps.setInt(2, keybinding.getKey());
- ps.setInt(3, keybinding.getValue().getType());
- ps.setInt(4, keybinding.getValue().getAction());
- ps.addBatch();
- }
- ps.executeBatch();
- ps.close();
- // no need for saved locations for new char
- // no buddies for new char
- //storage and account details doesnt have to be saved for new character
- con.commit();
- } catch (Exception e) {
- log.error(MapleClient.getLogMessage(this, "[charsave] Error saving new character data"), e);
- try {
- con.rollback();
- } catch (SQLException e1) {
- log.error(MapleClient.getLogMessage(this, "[charsave] Error Rolling Back"), e);
- }
- } finally {
- try {
- if (rs != null) {
- rs.close();
- }
- if (ps != null) {
- ps.close();
- }
- con.setAutoCommit(true);
- con.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
- } catch (SQLException e) {
- log.error(MapleClient.getLogMessage(this, "[charsave] Error saving new character going back to autocommit mode"), e);
- }
- }
- }
Add Comment
Please, Sign In to add comment