0

我有一个表单和 onClick 侦听器我想更新我的数据库中的一些数据异常说我有语法错误但是当我在 mysql 控制台执行查询时它在这里工作是我的代码检查所有变量

String temp =  itemList.getModel().getElementAt(itemList.getSelectedIndex()).toString();
PreparedStatement pt = supplies.con.prepareStatement("update prods set pquant = pquant + ? where pname = ?");
pt.setInt(1, Integer.parseInt(empsalary.getText()));
pt.setString(2, temp);
supplies.pst.executeQuery(temp); 

错误

com.mysql.jdbc.exceptions.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 'DVD Verdatim' at line 1
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:936)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2985)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1631)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1723)
at com.mysql.jdbc.Connection.execSQL(Connection.java:3277)
at com.mysql.jdbc.Connection.execSQL(Connection.java:3206)
at com.mysql.jdbc.Statement.executeQuery(Statement.java:1232)
at buy$1.actionPerformed(buy.java:62)
4

2 回答 2

0

正如编译器所提到的,您的 SQL 查询中确实存在语法错误。尝试:

String temp =  itemList.getModel().getElementAt(itemList.getSelectedIndex()).toString();
PreparedStatement pt = supplies.con.prepareStatement("update prods set pquant = @0 + 21" + "'variableForPquant'" "where pname = @1" + "'variableForPname'");
pt.setInt(1, Integer.parseInt(empsalary.getText()));
pt.setString(2, temp);
supplies.pst.executeQuery(temp); 

在 SQL 查询中使用变量时需要使用引号。我相信要么

"'variableName'"或者'"variableName'"

于 2014-11-10T16:39:04.443 回答
0

我发现我的错误我声明了一个变量 pt 并且我从为空 pst 的语句执行查询我更改为 pt.executeUpdate(); 现在可以了

于 2014-11-10T17:07:13.687 回答