// create/split item stack
 private ItemStack getSplitItemStack(
     int itemRowId, int itemId, short itemDamage, int qty, String enchStr) {
   ItemStack stack = new ItemStack(itemId, qty, itemDamage);
   int maxSize = stack.getMaxStackSize();
   // split stack
   if (qty > maxSize) {
     Connection conn = WebAuctionPlus.dataQueries.getConnection();
     PreparedStatement st = null;
     while (qty > maxSize) {
       try {
         if (WebAuctionPlus.isDebug())
           WebAuctionPlus.log.info(
               "WA Query: getSplitItemStack  qty:"
                   + Integer.toString(qty)
                   + "  max:"
                   + Integer.toString(maxSize));
         st =
             conn.prepareStatement(
                 "INSERT INTO `"
                     + WebAuctionPlus.dataQueries.dbPrefix()
                     + "Items` ( "
                     + "`playerName`, `itemId`, `itemDamage`, `qty`, `enchantments` )VALUES( ?, ?, ?, ?, ? )");
         st.setString(1, playerName);
         st.setInt(2, itemId);
         st.setShort(3, itemDamage);
         st.setInt(4, maxSize);
         st.setString(5, enchStr);
         st.executeUpdate();
       } catch (SQLException e) {
         WebAuctionPlus.log.warning(
             WebAuctionPlus.logPrefix + "Unable to insert new item to inventory!");
         e.printStackTrace();
         return null;
       } finally {
         WebAuctionPlus.dataQueries.closeResources(st, null);
       }
       qty -= maxSize;
     }
     stack.setAmount(qty);
     WebAuctionPlus.dataQueries.closeResources(conn);
   }
   // add enchantments
   if (enchStr != null && !enchStr.isEmpty())
     DataQueries.decodeEnchantments(Bukkit.getPlayer(playerName), stack, enchStr);
   return stack;
 }
  private void doUpdate() {
    if (WebAuctionPlus.isDebug())
      WebAuctionPlus.log.info(WebAuctionPlus.logPrefix + "Updating stats..");
    Connection conn = WebAuctionPlus.dataQueries.getConnection();

    // total buy nows
    {
      PreparedStatement st = null;
      ResultSet rs = null;
      this.totalBuyNowCount = 0;
      try {
        if (WebAuctionPlus.isDebug()) WebAuctionPlus.log.info("WA Query: Stats::count buy nows");
        st =
            conn.prepareStatement(
                "SELECT COUNT(*) FROM `"
                    + WebAuctionPlus.dataQueries.dbPrefix()
                    + "Auctions` WHERE `allowBids` = 0");
        rs = st.executeQuery();
        if (rs.next()) this.totalBuyNowCount = rs.getInt(1);
      } catch (SQLException e) {
        WebAuctionPlus.log.warning(WebAuctionPlus.logPrefix + "Unable to get total buy now count");
        e.printStackTrace();
      } finally {
        DataQueries.closeResources(st, rs);
      }
    }

    // total auctions
    {
      PreparedStatement st = null;
      ResultSet rs = null;
      this.totalAuctionCount = 0;
      try {
        if (WebAuctionPlus.isDebug()) WebAuctionPlus.log.info("WA Query: Stats::count auctions");
        st =
            conn.prepareStatement(
                "SELECT COUNT(*) FROM `"
                    + WebAuctionPlus.dataQueries.dbPrefix()
                    + "Auctions` WHERE `allowBids` != 0");
        rs = st.executeQuery();
        if (rs.next()) this.totalAuctionCount = rs.getInt(1);
      } catch (SQLException e) {
        WebAuctionPlus.log.warning(WebAuctionPlus.logPrefix + "Unable to get total auction count");
        e.printStackTrace();
      } finally {
        DataQueries.closeResources(st, rs);
      }
    }

    // get max auction id
    {
      PreparedStatement st = null;
      ResultSet rs = null;
      this.maxAuctionId = -1;
      try {
        if (WebAuctionPlus.isDebug()) WebAuctionPlus.log.info("WA Query: Stats::getMaxAuctionID");
        st =
            conn.prepareStatement(
                "SELECT MAX(`id`) AS `id` FROM `"
                    + WebAuctionPlus.dataQueries.dbPrefix()
                    + "Auctions`");
        rs = st.executeQuery();
        if (rs.next()) this.maxAuctionId = rs.getInt("id");
      } catch (SQLException e) {
        WebAuctionPlus.log.warning(WebAuctionPlus.logPrefix + "Unable to query for max Auction ID");
        e.printStackTrace();
      } finally {
        DataQueries.closeResources(st, rs);
      }
    }

    // get new auctions count
    {
      PreparedStatement st = null;
      ResultSet rs = null;
      this.newAuctionsCount = 0;
      try {
        final boolean isFirst = (this.newAuctionsCount_lastId < 1);
        if (WebAuctionPlus.isDebug())
          WebAuctionPlus.log.info(
              "WA Query: Stats::getNewAuctionsCount" + (isFirst ? " -first-" : ""));
        if (isFirst) {
          // first query
          st =
              conn.prepareStatement(
                  "SELECT MAX(`id`) AS `id` FROM `"
                      + WebAuctionPlus.dataQueries.dbPrefix()
                      + "Auctions`");
          rs = st.executeQuery();
          if (rs.next()) {
            this.newAuctionsCount = 0;
            this.newAuctionsCount_lastId = rs.getInt("id");
          }
        } else {
          // refresher query
          st =
              conn.prepareStatement(
                  "SELECT COUNT(*) AS `count`, MAX(`id`) AS `id` FROM `"
                      + WebAuctionPlus.dataQueries.dbPrefix()
                      + "Auctions` WHERE `id` > ?");
          st.setInt(1, this.newAuctionsCount_lastId);
          rs = st.executeQuery();
          if (rs.next()) {
            this.newAuctionsCount = rs.getInt("count");
            if (this.newAuctionsCount > 0) this.newAuctionsCount_lastId = rs.getInt("id");
          }
        }
      } catch (SQLException e) {
        WebAuctionPlus.log.warning(
            WebAuctionPlus.logPrefix + "Unable to query for new auctions count");
        e.printStackTrace();
      } finally {
        DataQueries.closeResources(st, rs);
      }
    }

    // get ended auctions count
    {
      PreparedStatement st = null;
      ResultSet rs = null;
      this.endAuctionsCount = 0;
      try {
        final boolean isFirst = (this.endAuctionsCount_lastId < 1);
        if (WebAuctionPlus.isDebug())
          WebAuctionPlus.log.info(
              "WA Query: Stats::getNewSalesCount" + (isFirst ? " -first-" : ""));
        if (isFirst) {
          // first query
          st =
              conn.prepareStatement(
                  "SELECT MAX(`id`) AS `id` FROM `"
                      + WebAuctionPlus.dataQueries.dbPrefix()
                      + "LogSales`");
          rs = st.executeQuery();
          if (rs.next()) {
            this.endAuctionsCount = 0;
            this.endAuctionsCount_lastId = rs.getInt("id");
          }
        } else {
          // refresher query
          st =
              conn.prepareStatement(
                  "SELECT COUNT(*) AS `count`, MAX(`id`) AS `id` FROM `"
                      + WebAuctionPlus.dataQueries.dbPrefix()
                      + "LogSales` WHERE `id` > ?");
          st.setInt(1, this.endAuctionsCount_lastId);
          rs = st.executeQuery();
          if (rs.next()) {
            this.endAuctionsCount = rs.getInt("count");
            if (this.endAuctionsCount > 0) this.endAuctionsCount_lastId = rs.getInt("id");
          }
        }
      } catch (SQLException e) {
        WebAuctionPlus.log.warning(
            WebAuctionPlus.logPrefix + "Unable to query for new sales count");
        e.printStackTrace();
      } finally {
        DataQueries.closeResources(st, rs);
      }
    }

    WebAuctionPlus.dataQueries.closeResources(conn);
  }
  // save inventory to db
  protected void saveInventory() {
    Connection conn = WebAuctionPlus.dataQueries.getConnection();
    PreparedStatement st = null;
    int countInserted = 0;
    int countUpdated = 0;
    int countDeleted = 0;
    for (int i = 0; i < chest.getSize(); i++) {
      //			if(!slotChanged.contains(i)) continue;
      ItemStack Item = chest.getItem(i);

      // empty slot
      if (Item == null || Item.getTypeId() == 0) {

        // delete item
        if (tableRowIds.containsKey(i)) {
          try {
            if (WebAuctionPlus.isDebug())
              WebAuctionPlus.log.info(
                  "WA Query: saveInventory::delete slot " + Integer.toString(i));
            st =
                conn.prepareStatement(
                    "DELETE FROM `"
                        + WebAuctionPlus.dataQueries.dbPrefix()
                        + "Items` WHERE `id` = ? LIMIT 1");
            st.setInt(1, tableRowIds.get(i));
            st.executeUpdate();
          } catch (SQLException e) {
            WebAuctionPlus.log.warning(
                WebAuctionPlus.logPrefix + "Unable to delete item from inventory!");
            e.printStackTrace();
          } finally {
            WebAuctionPlus.dataQueries.closeResources(st, null);
          }
          countDeleted++;
          continue;

          // no item
        } else {
          continue;
        }

        // item in slot
      } else {

        // update existing item
        if (tableRowIds.containsKey(i)) {
          try {
            if (WebAuctionPlus.isDebug())
              WebAuctionPlus.log.info(
                  "WA Query: saveInventory::update slot " + Integer.toString(i));
            st =
                conn.prepareStatement(
                    "UPDATE `"
                        + WebAuctionPlus.dataQueries.dbPrefix()
                        + "Items` SET "
                        + "`itemId` = ?, `itemDamage` = ?, `qty` = ?, `enchantments` = ? WHERE `id` = ? LIMIT 1");
            st.setInt(1, Item.getTypeId());
            st.setShort(2, Item.getDurability());
            st.setInt(3, Item.getAmount());
            st.setString(4, DataQueries.encodeEnchantments(Bukkit.getPlayer(playerName), Item));
            st.setInt(5, tableRowIds.get(i));
            st.executeUpdate();
          } catch (SQLException e) {
            WebAuctionPlus.log.warning(
                WebAuctionPlus.logPrefix + "Unable to update item to inventory!");
            e.printStackTrace();
          } finally {
            WebAuctionPlus.dataQueries.closeResources(st, null);
          }
          countUpdated++;
          continue;

          // insert new item
        } else {
          try {
            if (WebAuctionPlus.isDebug())
              WebAuctionPlus.log.info(
                  "WA Query: saveInventory::insert slot " + Integer.toString(i));
            st =
                conn.prepareStatement(
                    "INSERT INTO `"
                        + WebAuctionPlus.dataQueries.dbPrefix()
                        + "Items` ( "
                        + "`playerName`, `itemId`, `itemDamage`, `qty`, `enchantments` )VALUES( ?, ?, ?, ?, ? )");
            st.setString(1, playerName);
            st.setInt(2, Item.getTypeId());
            st.setShort(3, Item.getDurability());
            st.setInt(4, Item.getAmount());
            st.setString(5, DataQueries.encodeEnchantments(Bukkit.getPlayer(playerName), Item));
            st.executeUpdate();
          } catch (SQLException e) {
            WebAuctionPlus.log.warning(
                WebAuctionPlus.logPrefix + "Unable to insert new item to inventory!");
            e.printStackTrace();
          } finally {
            WebAuctionPlus.dataQueries.closeResources(st, null);
          }
          countInserted++;
          continue;
        }
      }
    }
    WebAuctionPlus.dataQueries.closeResources(conn);
    //		slotChanged.clear();
    chest.clear();
    tableRowIds.clear();
    WebAuctionPlus.log.info(
        WebAuctionPlus.logPrefix
            + "Updated player inventory for: "
            + playerName
            + " ["
            + " Inserted:"
            + Integer.toString(countInserted)
            + " Updated:"
            + Integer.toString(countUpdated)
            + " Deleted:"
            + Integer.toString(countDeleted)
            + " ]");
  }