2013-02-27 48 views
1
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; 
} 

我创建了添加的所有数据的主要方法,我看不到任何错误的insert声明。错误SQL语法中使用Java

我收到此错误:

[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 
+0

我不认为这是问题,我有许多其他公共类,每个人都在ID字段中有NULL – user2113404 2013-02-27 02:53:28

+0

你是否直接在SQL Server上测试SQL代码?另外,你可以指定你正在使用的SQL引擎吗? SQL Server,MySQL,Postgre等 - 语言中每个语言之间都有一些细微之处。 – Jesse 2013-02-27 03:08:39

回答

1

LEFTreserved word in MySql(如LEFT OUTER JOIN...)。您需要使用引号将其用作标识符。

更好的是,考虑将字段重命名为不使用保留字作为标识符。

1

LEFT是在MySQL的保留字;你要逃避它:

INSERT INTO gameplayers (
    id, botid, gameid, name, ip, spoofed, reserved, loadingtime, `left`, ... 
                 ----------^----^