0

I'm trying to update the column amount if the buyid (primary key) is a specific value.

UPDATE portfolio set amount=40 WHERE buyid=3

I work with JDBC and MySql, everytime I try to execute the statement i get the following exception:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'buyid=3' at line 1

table structure of portfolio:

buyid int
username varchar
stockname varchar
priceperstock float
amount int

Javasourcecode:

public void sellStock(int buyid, int amount, float currentprice, String user) {
...
 try {
 stmt = this.conn.createStatement();
 System.out.println(fetchedamount);
 System.out.println("UPDATE portfolio
 SET amount=" + fetchedamount
 + " WHERE buyid=" + buyid);
 stmt.execute("UPDATE stockman.portfolio
 SET amount=" + fetchedamount
 + "WHERE buyid=" + buyid+"");
 // update capital
 newmoney = amount * currentprice + oldmoney;
 } catch (SQLException ee) {
 ee.printStackTrace();
 }
vivekpansara
8971 gold badge6 silver badges14 bronze badges
asked Jun 24, 2015 at 10:04
10
  • 5
    how about datatype of buyid, also if you can share table structure Commented Jun 24, 2015 at 10:06
  • Are you able to run this query directly in mysql client? Is the data type of buyid numberic? Commented Jun 24, 2015 at 10:07
  • 3
    You might want to check the statement for hidden characters . . . spaces that are not really spaces, for instance (sometimes just re-typing the expression can fix some problems). Commented Jun 24, 2015 at 10:09
  • The query looks OK. It seems the buyid is not a number. Commented Jun 24, 2015 at 10:09
  • if i run it from myphpadmin it works. Commented Jun 24, 2015 at 10:10

3 Answers 3

7

At stmt.execute() Your generated query is like
"UPDATE stockman.portfolio set amount=23WHERE buyid=54 "

Here 23Where is one whole string so you have to give space between these two.

Give space between " and Where like the following:

" WHERE buyid=" 

And remove the last +""

GingerHead
8,23815 gold badges63 silver badges97 bronze badges
answered Jun 24, 2015 at 10:14
Sign up to request clarification or add additional context in comments.

Comments

0

Just add space before Where it will solve the problem.

 stmt.execute("UPDATE stockman.portfolio
 SET amount=" + fetchedamount
 + " WHERE buyid=" + buyid+"");
answered Jun 24, 2015 at 12:02

Comments

0

Repleace the stm.execute line with: stmt.execute("UPDATE stockman.portfolio SET amount=" + fetchedamount + " WHERE buyid=" + buyid+" ");

Or: stmt.execute("UPDATE stockman.portfolio SET amount='" + fetchedamount + "' WHERE buyid='" + buyid+"' ");

xrcwrn
5,33517 gold badges72 silver badges130 bronze badges
answered Jun 24, 2015 at 12:34

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.