我在使用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'并且它应该可以正常工作。不幸的是,它不适用于我的客户。

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

最佳答案

什么是Settings.mysqlPref?如果这是数据库名称,则不能在反引号中包装db_name.table_name。您要么需要删除反引号,要么将它们放在db_name周围,并重新命名table_name。合理?

关于java - 带保留字的SQL语法错误(Java),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40364578/

10-11 20:14