public void putBid(int bidQuantity, int bidPrice, int marketId, boolean opposite, User u)
	{
		opposite = !opposite;
		int userId = u.getId();
		Connection conn = null;
		PreparedStatement ps = null;
		PreparedStatement ps2 = null;
		ResultSet rs = null;
		ResultSet rsK = null;

		try
		{
			conn = this.factory.getConnection();

			String req = "SELECT sl.stock_id as stockid, owner_id, quantity, (100-price_sell) as price"
					+ " FROM markets m, stocks st, sells sl"
					+ " WHERE m.market_id = ? AND opposite = ? AND st.stock_id = sl.stock_id AND m.market_id = st.market_id AND (100-price_sell) <= ? order by price asc;";

			ps = DAOUtil.getPreparedStatement(conn, req, marketId, opposite, bidPrice);
			rs = ps.executeQuery();

			String req2 = null;

			while (rs.next() && bidQuantity > 0)
			{
				int stockId = rs.getInt("stockid");
				int availableQuantity = rs.getInt("quantity");
				int ownerId = rs.getInt("owner_id");
				int sellPrice = rs.getInt("price");

				if (availableQuantity <= bidQuantity)
				{
					int exchangeQuantity = availableQuantity;
					int totalExchange = exchangeQuantity * sellPrice;

					bidQuantity -= exchangeQuantity;

					req2 = "INSERT INTO stocks (quantity, opposite, owner_id, market_id) VALUES (?, ?, ?, ?);";
					ps2 = DAOUtil.getPreparedStatement(conn, req2, exchangeQuantity, new Boolean(! opposite), userId, marketId);
					ps2.executeUpdate();

					req2 = "DELETE FROM sells WHERE stock_id = ?;";
					ps2 = DAOUtil.getPreparedStatement(conn, req2, stockId);
					ps2.executeUpdate();

					req2 = "UPDATE users SET money = money + ? WHERE user_id = ?;";
					ps2 = DAOUtil.getPreparedStatement(conn, req2, totalExchange, ownerId);
					ps2.executeUpdate();

					req2 = "UPDATE users SET money = money - ? WHERE user_id = ?;";
					ps2 = DAOUtil.getPreparedStatement(conn, req2, totalExchange, userId);
					ps2.executeUpdate();

					if (u.getId() != ownerId)
						u.setMoney(u.getMoney() - totalExchange);

					req2 = "INSERT INTO logs VALUES (default, TIMESTAMP 'now', ?, ?, ?);";
					ps2 = DAOUtil.getPreparedStatement(conn, req2, sellPrice, exchangeQuantity, marketId);
					ps2.executeUpdate();
				}
				else
				{
					int exchangeQuantity = bidQuantity;
					int totalExchange = exchangeQuantity * sellPrice;

					availableQuantity -= exchangeQuantity;
					bidQuantity = 0;

					req2 = "INSERT INTO stocks (quantity, opposite, owner_id, market_id) VALUES (?, ?, ?, ?);";
					ps2 = DAOUtil.getPreparedStatement(conn, req2, exchangeQuantity, new Boolean (! opposite), userId, marketId);
					ps2.executeUpdate();

					req2 = "UPDATE stocks SET quantity = ? WHERE stock_id = ?;";
					ps2 = DAOUtil.getPreparedStatement(conn, req2, availableQuantity, stockId);
					ps2.executeUpdate();

					req2 = "UPDATE users SET money = money + ? WHERE user_id = ?;";
					ps2 = DAOUtil.getPreparedStatement(conn, req2, totalExchange, ownerId);
					ps2.executeUpdate();

					req2 = "UPDATE users SET money = money - ? WHERE user_id = ?;";
					ps2 = DAOUtil.getPreparedStatement(conn, req2, totalExchange, userId);
					ps2.executeUpdate();

					if (u.getId() != ownerId)
						u.setMoney(u.getMoney() - totalExchange);

					req2 = "INSERT INTO logs VALUES (default, TIMESTAMP 'now', ?, ?, ?);";
					ps2 = DAOUtil.getPreparedStatement(conn, req2, sellPrice, exchangeQuantity, marketId);
					ps2.executeUpdate();
				}
			}
			if (bidQuantity > 0)
			{
				req2 = "INSERT INTO stocks VALUES (default, ?, ?, ?, ?);";
				ps2 = conn.prepareStatement(req2, PreparedStatement.RETURN_GENERATED_KEYS);
				ps2.setObject(1, bidQuantity);
				ps2.setObject(2, new Boolean(! opposite));
				ps2.setObject(3, userId);
				ps2.setObject(4, marketId);
				ps2.executeUpdate();
				
				long key = -1L;
				rsK = ps2.getGeneratedKeys();
				
				if (rsK != null && rsK.next())
					key = rsK.getLong(1);

				req2 = "INSERT INTO sells VALUES (default, TIMESTAMP 'now', ?, ?);";
				ps2 = DAOUtil.getPreparedStatement(conn, req2, bidPrice, key);
				ps2.executeUpdate();
			}
		}
		catch (SQLException e)
		{
			throw new DAOException(e);
		}
		finally
		{
			DAOUtil.close(rsK);
			DAOUtil.close(ps2);
			DAOUtil.close(rs, ps, conn);
		}
	}