Results 1 to 10 of 10
  1. #1
    antoha is offline Member
    Join Date
    Nov 2010
    Posts
    6
    Rep Power
    0

    Default Need some help here with java ^^

    ok i got 2 small problems i need help with, its for a personal project, a homework if u will ^^
    i got this script:
    Java Code:
    private boolean insertBrokerItem(final BrokerItem item)
    	{
    		boolean result = DB
    			.insertUpdate(
    				"INSERT INTO `broker` (`itemPointer`, `itemId`, `itemCount`,`seller`, `price`, `brokerRace`, `expireTime`, `sellerId`, `isSold`, `isSettled`) VALUES (?,?,?,?,?,?,?,?,?,?)",
    				new IUStH(){
    					@Override
    					public void handleInsertUpdate(PreparedStatement stmt) throws SQLException
    					{
    						stmt.setInt(1, item.getItemUniqueId());
    						stmt.setInt(2, item.getItemId());
    						stmt.setLong(3, item.getItemCount());
    						stmt.setString(4, item.getSeller());
    						stmt.setLong(5, item.getPrice());
    						stmt.setString(6, String.valueOf(item.getItemBrokerRace()));
    						stmt.setTimestamp(7, item.getExpireTime());
    						stmt.setInt(8, item.getSellerId());
    						stmt.setBoolean(9, item.isSold());
    						stmt.setBoolean(10, item.isSettled());
    
    						stmt.execute();
    					}
    				});
    
    		return result;
    	}
    now, as u can see, its inserting somethin into the "price" row in the SQL now what i need is:
    checking the value thats being insurted to be positive or negative, if its positive, let it go on as planned, if its negative - stop the operation, for an example: a number like 5000 will get threw while -5000 wont.

    another thing, i need practicly the same, just with a different code:
    Java Code:
    ublic List<BrokerItem> loadBroker()
    	{
    		final List<BrokerItem> brokerItems = new ArrayList<BrokerItem>();
    
    		final List<Item> items = getBrokerItems();
    
    		DB.select("SELECT * FROM broker", new ReadStH(){
    
    			@Override
    			public void handleRead(ResultSet rset) throws SQLException
    			{
    				while(rset.next())
    				{
    					int itemPointer = rset.getInt("itemPointer");
    					int itemId = rset.getInt("itemId");
    					long itemCount = rset.getLong("itemCount");
    					String seller = rset.getString("seller");
    					int sellerId = rset.getInt("sellerId");
    					long price = rset.getLong("price");
    					BrokerRace itemBrokerRace = BrokerRace.valueOf(rset.getString("brokerRace"));
    					Timestamp expireTime = rset.getTimestamp("expireTime");
    					Timestamp settleTime = rset.getTimestamp("settleTime");
    					int sold = rset.getInt("isSold");
    					int settled = rset.getInt("isSettled");
    
    					boolean isSold = sold == 1;
    					boolean isSettled = settled == 1;
    
    					Item item = null;
    					if(!isSold)
    						for(Item brItem : items)
    						{
    							if(itemPointer == brItem.getObjectId())
    							{
    								item = brItem;
    								break;
    							}
    						}	
    
    					brokerItems.add(new BrokerItem(item, itemId, itemPointer, itemCount, price, seller, sellerId,
    						itemBrokerRace, isSold, isSettled, expireTime, settleTime));
    				}
    			}
    		});
    
    		return brokerItems;
    	}
    again, i want a check to be run on the recived price, if its positive, leave it as it is, if its negative - change the price to "0"
    i just rlly need thouse 2 problems fixed =\\ im really noob at java so i ask for ur help :confused:

  2. #2
    antoha is offline Member
    Join Date
    Nov 2010
    Posts
    6
    Rep Power
    0

    Default

    any1? i have tryed numerous times by mayself but it all failed... so please help...

  3. #3
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    305
    Rep Power
    6

    Default

    Why not just add a if statement checking if the price is greater than 0 before you do the execute?

  4. #4
    antoha is offline Member
    Join Date
    Nov 2010
    Posts
    6
    Rep Power
    0

    Default

    thats what i tryed to do, numerous times, failed. always had some errors...
    what i did was:
    Java Code:
    					stmt.setInt(1, item.getItemUniqueId());
    						stmt.setInt(2, item.getItemId());
    						stmt.setLong(3, item.getItemCount());
    						stmt.setString(4, item.getSeller());
    if(item.getPrice() < 0)
    {
    stmt.setLong(5, '0');
    }
    						stmt.setLong(5, item.getPrice());
    						stmt.setString(6, String.valueOf(item.getItemBrokerRace()));
    						stmt.setTimestamp(7, item.getExpireTime());
    						stmt.setInt(8, item.getSellerId());
    						stmt.setBoolean(9, item.isSold());
    						stmt.setBoolean(10, item.isSettled());
    but it errored, i get the general idea of how it should be done, im just not that good with java to do it..

  5. #5
    berkeleybross's Avatar
    berkeleybross is offline Senior Member
    Join Date
    Feb 2010
    Location
    England
    Posts
    187
    Rep Power
    5

    Default

    You may want to use an if () {} else {} statement.
    e.g.
    Java Code:
    if (item.getPrice() < 0) {
        stmt.setLong(5, '0');
    } else {
       stmt.setLong(5, item.getPrice();
    }
    The problem with what you are currently doing, is setting price to be zero if negative, and then immediatly overriding that by setting it to price regardless of if its negative.

  6. #6
    antoha is offline Member
    Join Date
    Nov 2010
    Posts
    6
    Rep Power
    0

    Default

    this is a part of a server emulator, and once i launch it after changing the code to w/e i change it to, it errors with some unrelated line (117) when the line i was changing in (169)
    any ideas about that?

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default

    According to your OP:
    "if its positive, let it go on as planned, if its negative - stop the operation, for an example: a number like 5000 will get threw while -5000 wont."

    That to me implies that you never insert a row with a negative price...not just convert the negative to zero.

  8. #8
    antoha is offline Member
    Join Date
    Nov 2010
    Posts
    6
    Rep Power
    0

    Default

    yes, but that just crash the whole thing and its ennoying.... i rather set it to 0 rather then redo the whole thing

  9. #9
    berkeleybross's Avatar
    berkeleybross is offline Senior Member
    Join Date
    Feb 2010
    Location
    England
    Posts
    187
    Rep Power
    5

    Default

    It might be useful if you post all the errors you get instead of letting us guess...

  10. #10
    antoha is offline Member
    Join Date
    Nov 2010
    Posts
    6
    Rep Power
    0

    Default

    somehow, using ur code berkeleybross everythin loaded fine, im testing atm if it worked or not.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •