2013-04-06 356 views
0

我想更新一個表,但它不工作,並給這個SQL錯誤。DB2錯誤SQLCODE = -103,SQLSTATE = 42604

//Updating Buy Table 
Integer stkbid = Integer.parseInt(request.getParameter("stockBid")); 
System.out.println("stock buy id : " + stkbid); 
//get buy details 
PreparedStatement stmtbuy = conn.prepareStatement(
     "SELECT \"StockSymbol\", \"Unit\", \"Price\", \"ClearingFee\", \"StampDuty\", \"BrokerFee\"" + 
     "FROM SPM.\"StockBuy\" WHERE \"StockBuyId\" = '"+ stkbid + "'"); 

System.out.println("Got stock buy details"); 

ResultSet rs=stmtbuy.executeQuery(); 
rs.next(); 
//String stkcode = rs.getString("StockSymbol"); 
Integer stkunit = Integer.parseInt(rs.getString("Unit")); 
stkunit -= stock.getStockUnit(); 
Double stkprice = Double.parseDouble(rs.getString("Price")); 
Double stkclear = Double.parseDouble(rs.getString("ClearingFee")); 
Double stksd = Double.parseDouble(rs.getString("StampDuty")); 
Double stkbfee = Double.parseDouble(rs.getString("BrokerFee")); 
Double stkval = stkunit * stkprice; 
Double stknv = stkval + stkval * (stkclear + stksd + stkbfee); 

System.out.println(stknv); 

PreparedStatement stmtbuy1 = conn.prepareStatement(
     "UPDATE SPM.\"StockBuy\" SET \"Unit\" = " + stkunit + ", \"Value\" = " + stkval + ", \"NetValue\" = " + stknv + 
     "WHERE \"StockBuyId\" = "+ stkbid); 
+0

第一個SELECT語句是工作,也是我試圖單引號內,但它不工作 – 2013-04-06 14:12:23

+0

更新語句封閉stkbid的相同的格式是有一些原因,你沒有使用參數化查詢? – RedFilter 2013-04-06 14:13:28

+0

是的,我可以使用它,但這應該工作得不? – 2013-04-06 14:14:41

回答

2

您在WHERE條款之前缺少一個空格,這會使您的stknv搞砸。

" WHERE \"StockBuyId\" = "+ stkbid); 

我認爲這是任何海報的義務,提醒你應該使用參數化查詢。所以我會做同樣的事情。

請使用參數化查詢!

+0

感謝您指出的錯誤,是的使用參數化查詢解決了我的問題.. – 2013-04-07 15:15:47

1

即作品查詢已在最後報價:

" WHERE \"StockBuyId\" = '"+ stkbid + "'"); 

失敗並不

"WHERE \"StockBuyId\" = "+ stkbid); 

這可能是與它的人。

相關問題