簡體   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