1

Iam using a Java Swing Application to Update a column in Mysql table. Here is My code(part)

String qunty = txtWithdraw.getText();
String partno = txtNo.getText();
int qty = Integer.parseInt(qunty);
con = DriverManager.getConnection(url + db, "username", "password");
        Statement st = con.createStatement();
String sell = "update Store_info_table set qnty_received = qnty_received - " + qty +      "where Part_number = '" + partno + "'";
                st.executeUpdate(sell);

I am getting the Exception that:

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 'Part_number = 'DF6534'' at line 1

I want to Update the qnty_received field so that it is equal to the Original value minus the value passed by the user i.e (int qty). What Error Am I making?

4

2 に答える 2

6

場所の前にスペースを追加します。

 " where Part_number = '" + partno + "'";

良い習慣として、PreparedStatement同じものを使用してパラメータを設定することをお勧めします。パラメータを動的に連結すると、dbエンジンが毎回新しいSQLステートメントを解析するように強制される場合があります。

SQLステートメントはプリコンパイルされ、PreparedStatementオブジェクトに格納されます。このオブジェクトを使用して、このステートメントを複数回効率的に実行できます。

参照:PreparedStatement

于 2012-08-21T14:22:49.583 に答える
3

不足しているスペース:

... + qty +      "where ...
                  ^--- here

これにより、クエリは次のようになります

... qnty_received - blahwhere
于 2012-08-21T14:22:51.563 に答える