简体   繁体   English

尝试从Java应用程序插入此dateTime有什么问题?

[英]What's wrong with trying to insert this dateTime from my Java application?

For this Java code: 对于此Java代码:

stmt.addBatch(
                "INSERT INTO Bills (BillDateTime, Table, Item, NoAttended, Service, Payment, Total) " +
                "VALUES('" + billDateTime +  "', " + Integer.parseInt(createTableNumberOutput.toString()) +  ", '" + null + "', '" 
                + Integer.parseInt(createGuestNumberOutput.toString()) + "', " + "5" +  ", '" + 
                createPaymentTypeOutput.toString() + "', '" +  "')");

I get the following error: 我收到以下错误:

java.sql.BatchUpdateException: 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 'Table, Item, NoAttended, Service, Payment, Total) VALUES('2012-03-26 11:15:8', 1' at line 1

The issue is not apparent to me, as MySql requires the format 'YYYY-MM-DD HH:MM:SS' for dateTime, which I have, right? 这个问题对我来说并不明显,因为MySql需要dateTime格式为“ YYYY-MM-DD HH:MM:SS”,对吗?

Table is reserved keyword in mysql . Table是mysql中的保留关键字 use backticks( ` ) around it. 在其周围使用反引号( ` )。

Like below: 如下所示:

stmt.addBatch("INSERT INTO Bills (BillDateTime,`Table`, Item,NoAttended,Service,Payment,Total..........")

Even when you fix the reserved keyword table issue, it still won't work. 即使您修复了保留关键字table问题,也仍然无法使用。 You are trying the insert date.toString() in the date field. 您正在尝试在日期字段中插入date.toString() (when concatenated, toString() gets called all non-string objects) (串联时, toString()被调用为所有非字符串对象)

Instead, you should use a PreparedStatement and call stm.setDate(..) . 相反,您应该使用PreparedStatement并调用stm.setDate(..) Get more familiar with the prepared statement, as it is most often the better way to go. 更加熟悉准备好的语句,因为通常这是更好的方法。 (it also has addBatch() , which works in a slightly different way - you set all parameters, then add, then set again. (它还具有addBatch() ,其工作方式略有不同-设置所有参数,然后添加,然后再次设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM