保留字的SQL语法错误(Java)

时间:2016-11-01 16:43:49

标签: java mysql

我的sql语法错误有问题,我的查询和代码如下所示:

@Override
public void insert() {
    try {
        PreparedStatement stmt = Main.getSQL().getConnection().prepareStatement(
                "INSERT INTO `" + Settings.mysqlPref + "guilds` (name, tag, owner, allies, points, kills, deaths, validity, " +
                        "attacked, home, members, pvp, born, lives, bossHealth) " +
                        "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
        stmt.setString(1, name);
        stmt.setString(2, tag);
        stmt.setString(3, ownerUniqueId.toString());
        stmt.setString(4, Serializer.serializeList(allies));
        stmt.setInt(5, getPoints());
        stmt.setInt(6, kills);
        stmt.setInt(7, deaths);
        stmt.setLong(8, validity);
        stmt.setLong(9, protection);
        stmt.setString(10, Serializer.serializeLocation(home));
        stmt.setString(11, Serializer.serializeList(members));
        stmt.setBoolean(12, pvp);
        stmt.setLong(13, born);
        stmt.setInt(14, lives);
        stmt.setInt(15, bossHealth);
        stmt.executeUpdate();
        stmt.close();
    } catch (SQLException e) {
        e.printStackTrace();
    }
}

这是一个堆栈跟踪:

com.mysql.jdbc.exceptions.jdbc4.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', 'tag', 'owner', 'allies', 'points', 'kills', 'deaths', 'validity', 'atta' at line 1
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:407)
    at com.mysql.jdbc.Util.getInstance(Util.java:382)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1052)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3593)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3525)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1986)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2140)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2626)
    at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2111)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2407)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2325)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2310)
    at me.khalit.prostorm.tools.data.Guild.insert(Guild.java:126)
    at me.khalit.prostorm.tools.util.thread.Work.execute(Work.java:71)
    at org.bukkit.craftbukkit.v1_7_R4.scheduler.CraftTask.run(CraftTask.java:71)
    at org.bukkit.craftbukkit.v1_7_R4.scheduler.CraftAsyncTask.run(CraftAsyncTask.java:53)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)

问题是 - 对我来说它运作正常,但对于我的客户(就像你在上面看到的那样)它没有。我不知道是什么问题,因为对我来说一切正常。在SQLite或MySQL上,没有区别。

我看到它可能是MySQL中保留字的问题,但我做了'guilds'并且它应该正常工作。不幸的是,它不适合我的客户。

我只是放弃了,也许你们有一些解决方案?我不知道你需要什么代码片段,所以告诉我是否需要它。

1 个答案:

答案 0 :(得分:0)

什么是Settings.mysqlPref?如果这是数据库名称,则不能将db_name.table_name包装在反引号中。您需要删除反引号或将它们放在db_name和aroudn table_name之外。有意义吗?