All pastes #45110 Raw Edit

Stuff

public text v1 · immutable
#45110 ·published 2006-03-10 11:18 UTC
rendered paste body
package net.sf.l2j.sql;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Logger;

import net.sf.l2j.gameserver.model.L2ItemInstance;
import net.sf.l2j.L2DatabaseFactory;

public final class SQLHandler
{
	private static final String SELECT_REMOVED_ITEMS_SQL = "SELECT object_id, count FROM items WHERE object_id = ?";
	private static final String DELETE_REMOVED_ITEMS_SQL = "DELETE FROM items WHERE object_id = ?";
	private static final String UPDATE_REMOVED_ITEMS_SQL = "UPDATE items SET count = ? WHERE object_id = ?";

    private static Logger _log = Logger.getLogger(SQLHandler.class.getName());

    public boolean removeItems (List<L2ItemInstance> removeItems)
	{	
		Connection con = null;
		
		try
		{		
			con = L2DatabaseFactory.getInstance().getConnection();

			for (Iterator<L2ItemInstance> it = removeItems.iterator(); it.hasNext(); )
			{
				L2ItemInstance item = it.next();

				con.setAutoCommit(false);
				PreparedStatement statement = con.prepareStatement(SELECT_REMOVED_ITEMS_SQL);
				statement.setInt(1, item.getObjectId());
				ResultSet res = statement.executeQuery();
				if (res.next())
				{
					if (res.getInt("count") == item.getCount())
					{
						PreparedStatement statement2 = con.prepareStatement(DELETE_REMOVED_ITEMS_SQL);
						statement2.setInt(1, item.getObjectId());
						statement2.execute();
					}
					else
					{
						PreparedStatement statement2 = con.prepareStatement(UPDATE_REMOVED_ITEMS_SQL);
						statement2.setInt(1, item.getCount());
						statement2.setInt(2, item.getObjectId());
						statement2.execute();
					}
				}
				else
				{
					PreparedStatement statement2 = con.prepareStatement(DELETE_REMOVED_ITEMS_SQL);
					statement2.setInt(1, item.getObjectId());
					statement2.execute();
				}
				con.commit();
			}
		}
		catch (Exception e1)
		{
			try
			{
				if (con != null)
					con.rollback();
			}
			catch (SQLException e2)
			{
				_log.warning("Unable to rollback transaction!");
			}
			_log.warning("Error removing Objects from DB. Transaction rollback.");
		}
		return true;
	}

}