使用Java的SQL语法出错
我创建了一个主方法来添加所有数据,在使用Java的SQL语法出错,java,sql,Java,Sql,我创建了一个主方法来添加所有数据,在insert语句中看不到任何错误 我得到这个错误: public boolean setgame(int botid, int gameid, String name, String ip, int spoofed, int reserved, int loadingtime, int left, String leftreason, int team, int colour, String spoofedrealm) { try {
insert
语句中看不到任何错误
我得到这个错误:
public boolean setgame(int botid, int gameid, String name, String ip, int spoofed, int reserved, int loadingtime, int left, String leftreason, int team, int colour, String spoofedrealm) {
try {
Connection connection = connection();
PreparedStatement statement = connection.prepareStatement("INSERT INTO gameplayers (id, botid, gameid, name, ip, spoofed, reserved, loadingtime, left, leftreason, team, colour, spoofedrealm) VALUES (NULL, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
statement.setInt(1, botid);
statement.setInt(2, gameid);
statement.setString(3, name);
statement.setString(4, ip);
statement.setInt(5, spoofed);
statement.setInt(6, reserved);
statement.setInt(7, loadingtime);
statement.setInt(8, left);
statement.setString(9, leftreason);
statement.setInt(10, team);
statement.setInt(11, colour);
statement.setString(12, spoofedrealm);
statement.execute();
connectionReady(connection);
return true;
} catch (SQLException e) {
if (Main.DEBUG) {
}
Main.println("[SQLThread] Unable to add bot ban to MySQL database: " + e.getLocalizedMessage());
}
return false;
}
LEFT
是MySQL中的保留字;你必须逃避它:
[SQLThread] Fail: 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 'left, leftreason, team, colour, spoofedrealm) VALUES (NULL, 2, 146, 'Teste', '120.32' at line 1
左
是(如左外连接…
)。您需要在其周围使用引号将其用作标识符
更好的是,考虑将字段重命名为不使用保留字作为标识符。
我不认为这是问题。我有许多其他公共类,每个人在ID字段中都有NULL。您直接在SQL Server上测试了SQL代码吗?另外,您可以指定您正在使用的SQL引擎吗?SQL Server、MySQL、Postgre等等——这门语言中的每一种都有某些微妙之处。INSERT INTO gameplayers (
id, botid, gameid, name, ip, spoofed, reserved, loadingtime, `left`, ...
----------^----^