我的查询中的语法有什么问题?

时间:2015-02-03 06:41:05

标签: java mysql

query = "update transaksi set cloth = '"+txtCloth.getText()+"', quantity = '"+txtQty.getText()+"', price = '"+txtPrice.getText()+"', total = '"+txtTotal.getText()+"', pay = '"+txtPay.getText()+"', change = '"+txtChange.getText()+"' where id = "+txtId.getText();

它说

  

com.mysql.jdbc.exceptions.MySQLSyntaxErrorException:您有错误   在你的SQL语法中;查看与MySQL对应的手册   服务器版本,以便在第1行'change = '64' where id = 9'附近使用正确的语法

2 个答案:

答案 0 :(得分:0)

使用PreparedStatement将帮助您避免造成这种混乱

试试这个:

String query = "update transaksi set cloth = ?, quantity = ?, price = ?, total = ?, pay = ?, change = ? where id = ?";

PreparedStatement preparedStatement =
        connection.prepareStatement(query);

preparedStatement.setString(1, txtCloth.getText());
preparedStatement.setString(2, txtQty.getText());
preparedStatement.setString (3, txtPrice.getText());
..................................................//and so on
ResultSet result = preparedStatement.executeQuery();

答案 1 :(得分:0)

我只是更改表中的列名,一切都连接到该列。它的工作原理。不知道为什么名称“改变”不起作用