gpt4 book ai didi

java - 使用java插入表时出现SQl错误

转载 作者:行者123 更新时间:2023-12-02 01:07:24 25 4
gpt4 key购买 nike

我正在尝试使用以下代码将新行插入到 sql 表 Customers 中:

                    String str = "register customer Mark Waid 1234567 389529548 Monthly LilacHaifa "
+ "1234567891234567 5/2025";
String[] tokens = str.trim().split("\\s++");
sql = "INSERT INTO Customers " +
"(ID, Name, Password, Connected, SubscriptionType, Status, Store Name, CreditCardNumber, Balance, ExpirationDate) "
+ "VALUES (?,?,?,?,?,?,?,?,?,?)";
preparedStmt = conn.prepareStatement(sql);
preparedStmt.setInt(1, Integer.parseInt(tokens[5]));
preparedStmt.setString(2, tokens[2] + " " + tokens[3]);
preparedStmt.setString(3, tokens[4]);
preparedStmt.setInt(4, 1);
preparedStmt.setString(5, tokens[6]);
preparedStmt.setInt(6, 1);
preparedStmt.setString(7, tokens[7]);
preparedStmt.setString(8, tokens[8]);
preparedStmt.setDouble(9, 0);
preparedStmt.setString(10, tokens[9]);
preparedStmt.executeUpdate();

但我收到以下错误

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 'Name, CreditCardNumber, Balance, ExpirationDate) VALUES (389529548,'Mark Waid','' at line 1
SQLException: 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 'Name, CreditCardNumber, Balance, ExpirationDate) VALUES (389529548,'Mark Waid','' at line 1
SQLState: 42000
VendorError: 1064
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:3283)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1332)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1604)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1519)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1504)
at ocsf.LilacServer.handleMessageFromClient(LilacServer.java:297)
at ocsf.AbstractServer.receiveMessageFromClient(AbstractServer.java:474)
at ocsf.ConnectionToClient.run(ConnectionToClient.java:219)

我的查询肯定有问题,但我无法准确识别它是什么。感谢您的帮助。

最佳答案

Store Name 列中有一个空格,因此应该正确引用它,具体取决于您使用的数据库。

例如:

MS SQL Server:[商店名称]
Oracle:“商店名称”
MySQL:`商店名称`
Postgres:“商店名称”

关于java - 使用java插入表时出现SQl错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59837211/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com