Results 1 to 10 of 10
Thread: Need some help here with java ^^
- 11-03-2010, 03:41 PM #1
Member
- Join Date
- Nov 2010
- Posts
- 6
- Rep Power
- 0
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:
now, as u can see, its inserting somethin into the "price" row in the SQL now what i need is: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; }
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:
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"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; }
i just rlly need thouse 2 problems fixed =\\ im really noob at java so i ask for ur help :confused:
- 11-03-2010, 04:05 PM #2
Member
- Join Date
- Nov 2010
- Posts
- 6
- Rep Power
- 0
any1? i have tryed numerous times by mayself but it all failed... so please help...
- 11-03-2010, 04:14 PM #3
Senior Member
- Join Date
- Feb 2009
- Posts
- 303
- Rep Power
- 5
Why not just add a if statement checking if the price is greater than 0 before you do the execute?
- 11-03-2010, 04:18 PM #4
Member
- Join Date
- Nov 2010
- Posts
- 6
- Rep Power
- 0
thats what i tryed to do, numerous times, failed. always had some errors...
what i did was:
but it errored, i get the general idea of how it should be done, im just not that good with java to do it..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());
- 11-03-2010, 05:05 PM #5
You may want to use an if () {} else {} statement.
e.g.
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.Java Code:if (item.getPrice() < 0) { stmt.setLong(5, '0'); } else { stmt.setLong(5, item.getPrice(); }
- 11-03-2010, 05:09 PM #6
Member
- Join Date
- Nov 2010
- Posts
- 6
- Rep Power
- 0
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?
- 11-03-2010, 05:10 PM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
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.
- 11-03-2010, 05:12 PM #8
Member
- Join Date
- Nov 2010
- Posts
- 6
- Rep Power
- 0
yes, but that just crash the whole thing and its ennoying.... i rather set it to 0 rather then redo the whole thing
- 11-03-2010, 05:12 PM #9
It might be useful if you post all the errors you get instead of letting us guess...
- 11-03-2010, 05:13 PM #10
Member
- Join Date
- Nov 2010
- Posts
- 6
- Rep Power
- 0


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks