Java 如何修复此MySQL语法错误
这是我的源代码。Java 如何修复此MySQL语法错误,java,mysql,jdbc,Java,Mysql,Jdbc,这是我的源代码。 当我按下这个按钮时,我有一个语法错误异常 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 'order(lot_date, inst_id,
当我按下这个按钮时,我有一个语法错误异常
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 'order(lot_date, inst_id, qty, total, ptotal) values('2014-11-14', '4', '5', '100' at line 1
这是我的点菜台
在这次移动中,我没有向lot_id和draw字段添加值。请帮帮我。
![enter image description here][2]
order
是SQL中的保留字,因此如果要将其用作表名,需要使用前向引号对其进行保护:
boolean b = JDBC.putData("insert into `order` (lot_date, inst_id, qty, total, ptotal) values ('"+lot_date+"', '"+lot_id+"', '"+qty+"', '"+cost+"', '"+profit+"' )");
这与错误无关,但当您已经在使用PreparedStatement
时,请使用占位符(?
)来设置值。
这样可以防止攻击
更新
原因很简单:
- 内联绑定值时,可以忽略由错误字符串连接引起的语法错误
- 内联绑定值时,可以忽略错误字符串连接中的SQL注入漏洞
- 在内联更“复杂”的数据类型(如时间戳、二进制数据等)时,可以避免边缘情况
- 您可以将打开的PreparedStatements保留一段时间,使用新的绑定值重用它们,而不是立即关闭它们(例如,在Postgres中很有用)
- 您可以在更复杂的数据库中使用自适应光标共享(Oracle speak)。这有助于防止为每一组新的绑定值硬解析SQL语句
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 'order(lot_date, inst_id, qty, total, ptotal) values('2014-11-14', '4', '5', '100' at line 1
boolean b = JDBC.putData("insert into `order` (lot_date, inst_id, qty, total, ptotal) values ('"+lot_date+"', '"+lot_id+"', '"+qty+"', '"+cost+"', '"+profit+"' )");