繁体   English   中英

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

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

对于此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() + "', '" +  "')");

我收到以下错误:

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

这个问题对我来说并不明显,因为MySql需要dateTime格式为“ YYYY-MM-DD HH:MM:SS”,对吗?

Table是mysql中的保留关键字 在其周围使用反引号( ` )。

如下所示:

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

即使您修复了保留关键字table问题,也仍然无法使用。 您正在尝试在日期字段中插入date.toString() (串联时, toString()被调用为所有非字符串对象)

相反,您应该使用PreparedStatement并调用stm.setDate(..) 更加熟悉准备好的语句,因为通常这是更好的方法。 (它还具有addBatch() ,其工作方式略有不同-设置所有参数,然后添加,然后再次设置。

暂无
暂无

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

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