SQL错误:检查与MySQL服务器版本对应的手册以获得正确的语法

时间:2015-07-15 05:30:51

标签: java mysql sql jdbc

尝试将数据保存到database时,我收到错误并且 它不会将数据保存到数据库中。我做了一些研究 看看是否有人遇到与此类代码相匹配的错误 我的,没有找到任何可以帮助我的东西。我决定张贴我的 问题和我的代码希望有人可以帮助我。

主要错误:

  

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:您的SQL语法中有错误;查看与您的MySQL服务器版本对应的手册,以便在'WHERE playerName ='Nick''at第1行附近使用正确的语法

这是完整的Stacktrace:

[7/14/15 11:49 PM]: [REGISTERED]: Nick
[7/14/15 11:49 PM]: There's a problem when saving the data of  player Nick.
[7/14/15 11:49 PM]: 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 'WHERE `playerName`='Nick''at line 1
[7/14/15 11:49 PM]:     at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
[7/14/15 11:49 PM]:     at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
[7/14/15 11:49 PM]:     at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
[7/14/15 11:49 PM]:     at java.lang.reflect.Constructor.newInstance(Unknown Source)
[7/14/15 11:49 PM]:     at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
[7/14/15 11:49 PM]:     at com.mysql.jdbc.Util.getInstance(Util.java:381)
[7/14/15 11:49 PM]:     at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1030)
[7/14/15 11:49 PM]:     at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
[7/14/15 11:49 PM]:     at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3491)
[7/14/15 11:49 PM]:     at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3423)
[7/14/15 11:49 PM]:     at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1936)

[7/14/15 11:49 PM]:     at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2060)
[7/14/15 11:49 PM]:     at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2542)
[7/14/15 11:49 PM]:     at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1734)
[7/14/15 11:49 PM]:     at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2019)
[7/14/15 11:49 PM]:     at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1937)
[7/14/15 11:49 PM]:     at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1922)
[7/14/15 11:49 PM]:     at server.util.SQL.saveHighScore(SQL.java:145)
[7/14/15 11:49 PM]:     at server.model.mobile.players.Client.logout(Client.java:580)
[7/14/15 11:49 PM]:     at server.model.mobile.players.packets.Clicking.ClickingButtons.processPacket(ClickingButtons.java:2190)
[7/14/15 11:49 PM]:     at server.model.mobile.players.PacketHandler.processPacket(PacketHandler.java:118)
[7/14/15 11:49 PM]:     at server.model.mobile.players.Client.processQueuedPackets(Client.java:774)
[7/14/15 11:49 PM]:     at server.model.mobile.players.PlayerHandler.process(PlayerHandler.java:191)
[7/14/15 11:49 PM]:     at server.Server$1.execute(Server.java:103)
[7/14/15 11:49 PM]:     at server.task.Task2.tick(Task2.java:105)
[7/14/15 11:49 PM]:     at server.event.TaskScheduler.run(TaskScheduler.java:100)
[7/14/15 11:49 PM]:     at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
[7/14/15 11:49 PM]:     at java.util.concurrent.FutureTask.runAndReset(Unknown Source)
[7/14/15 11:49 PM]:     at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(Unknown Source)
[7/14/15 11:49 PM]:     at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(Unknown Source)
[7/14/15 11:49 PM]:     at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
[7/14/15 11:49 PM]:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
[7/14/15 11:49 PM]:     at java.lang.Thread.run(Unknown Source)
false
[7/14/15 11:49 PM]: Could not save for Nick
[7/14/15 11:49 PM]: LOGGED OUT: Nick

Heres是将其保存到表格中的代码:

public boolean saveHighScore(Client c) throws SQLException {
long totallvlexp = (long) (c.playerXP[0]) + (c.playerXP[1]) + (c.playerXP[2]) + (c.playerXP[3]) + (c.playerXP[4]) + (c.playerXP[5]) + (c.playerXP[6]) + (c.playerXP[7]) + (c.playerXP[8]) + (c.playerXP[9]) + (c.playerXP[10]) + (c.playerXP[11]) + (c.playerXP[12]) + (c.playerXP[13]) + (c.playerXP[14]) + (c.playerXP[15]) + (c.playerXP[16]) + (c.playerXP[17]) + (c.playerXP[18]) + (c.playerXP[19]) + (c.playerXP[20]); 
int totallevell = (int) (c.getLevelForXP(c.playerXP[0]) + c.getLevelForXP(c.playerXP[1]) + c.getLevelForXP(c.playerXP[2]) + c.getLevelForXP(c.playerXP[3]) + c.getLevelForXP(c.playerXP[4]) + c.getLevelForXP(c.playerXP[5]) + c.getLevelForXP(c.playerXP[6]) + c.getLevelForXP(c.playerXP[7]) + c.getLevelForXP(c.playerXP[8]) + c.getLevelForXP(c.playerXP[9]) + c.getLevelForXP(c.playerXP[10]) + c.getLevelForXP(c.playerXP[11]) + c.getLevelForXP(c.playerXP[12]) + c.getLevelForXP(c.playerXP[13]) + c.getLevelForXP(c.playerXP[14]) + c.getLevelForXP(c.playerXP[15]) + c.getLevelForXP(c.playerXP[16]) + c.getLevelForXP(c.playerXP[17]) + c.getLevelForXP(c.playerXP[18]) + c.getLevelForXP(c.playerXP[19]) + c.getLevelForXP(c.playerXP[20]));
boolean result = true;
String updateSkillsSql = "UPDATE skills"
    + " SET `Attacklvl`=?,"
    + " `Attackxp`=?,"
    + " `Defencelvl`=?,"
    + " `Defencexp`=?,"
    + " `Strengthlvl`=?,"
    + " `Strengthxp`=?,"
    + " `Hitpointslvl`=?,"
    + " `Hitpointsxp`=?,"
    + " `Rangelvl`=?,"
    + " `Rangexp`=?,"
    + " `Prayerlvl`=?,"
    + " `Prayerxp`=?,"
    + " `Magiclvl`=?,"
    + " `Magicxp`=?,"
    + " `Cookinglvl`=?,"
    + " `Cookingxp`=?,"
    + " `Woodcuttinglvl`=?,"
    + " `Woodcuttingxp`=?,"
    + " `Fletchinglvl`=?,"
    + " `Fletchingxp`=?,"
    + " `Fishinglvl`=?,"
    + " `Fishingxp`=?,"
    + " `Firemakinglvl`=?,"
    + " `Firemakingxp`=?,"
    + " `Craftinglvl`=?,"
    + " `Craftingxp`=?,"
    + " `Smithinglvl`=?,"
    + " `Smithingxp`=?,"
    + " `Mininglvl`=?,"
    + " `Miningxp`=?,"
    + " `Herblorelvl`=?,"
    + " `Herblorexp`=?,"
    + " `Agilitylvl`=?,"
    + " `Agilityxp`=?,"
    + " `Thievinglvl`=?,"
    + " `Thievingxp`=?,"
    + " `Slayerlvl`=?,"
    + " `Slayerxp`=?,"
    + " `Farminglvl`=?,"
    + " `Farmingxp`=?,"
    + " `Runecraftlvl`=?,"
    + " `Runecraftxp`=?"
    + " WHERE `playerName`=?";


String updateSkillsoverallSql = "UPDATE skillsoverall"
    + " SET `lvl`=?,"
    + " `xp`=?,"
    + " WHERE `playerName`=?";

String updatePlayerRightsSql = "UPDATE playerrights"
    + " SET `rank` = ?,"
    + " WHERE `playerName`=?";

try(Connection con = DriverManager.getConnection("jdbc:mysql://localhost/highscores","root","root")) {
con.setAutoCommit(false);
    try(PreparedStatement pstmt = con.prepareStatement(updateSkillsSql);PreparedStatement pstmt2 = con.prepareStatement(updateSkillsoverallSql);PreparedStatement pstmt3 = con.prepareStatement(updatePlayerRightsSql);) {
        setParameters(pstmt,
                    c.playerLevel[0], c.playerXP[0],
                    c.playerLevel[1], c.playerXP[1],
                    c.playerLevel[2], c.playerXP[2],
                    c.playerLevel[3], c.playerXP[3],
                    c.playerLevel[4], c.playerXP[4],
                    c.playerLevel[5], c.playerXP[5],
                    c.playerLevel[6], c.playerXP[6],
                    c.playerLevel[7], c.playerXP[7],
                    c.playerLevel[8], c.playerXP[8],
                    c.playerLevel[9], c.playerXP[9],
                    c.playerLevel[10], c.playerXP[10],
                    c.playerLevel[11], c.playerXP[11],
                    c.playerLevel[12], c.playerXP[12],
                    c.playerLevel[13], c.playerXP[13],
                    c.playerLevel[14], c.playerXP[14],
                    c.playerLevel[15], c.playerXP[15],
                    c.playerLevel[16], c.playerXP[16],
                    c.playerLevel[17], c.playerXP[17],
                    c.playerLevel[18], c.playerXP[18],
                    c.playerLevel[19], c.playerXP[19],
                    c.playerLevel[20], c.playerXP[20],
                    c.playerName);

        setParameters(pstmt2,
            totallevell, totallvlexp,
                    c.playerName);
        setParameters(pstmt3,
            c.getPermission().getId(), c.playerName);

        pstmt.executeUpdate();
        pstmt2.executeUpdate();
        pstmt3.executeUpdate();
    } catch (Exception e) {
        System.out.println(String.format("There's a problem when saving the data of  player %s.", c.playerName));
        e.printStackTrace(System.out);
        con.rollback();
        result = false;
    }
    if (result) {
        con.commit();
        con.setAutoCommit(true);
    }
} catch (Exception e) {
    System.out.println(String.format("There's a problem when saving the data of  player %s.", c.playerName));
    e.printStackTrace(System.out);
    result = false;
    }
    return result;
}

private void setParameters(PreparedStatement pstmt, Object ... args) {
    int i = 0;
    try {
        for (Object arg : args) {
            pstmt.setObject(++i, arg);
        }
    } catch(Exception e) {
        e.printStackTrace();
    }
}
}

以下是表格:

CREATE TABLE `skills` (
`playerName` varchar(15) NOT NULL default '',
`Attacklvl` double default NULL,
`Attackxp` double default NULL,
`Defencelvl` double default NULL,
`Defencexp` double default NULL,
`Strengthlvl` double default NULL,
`Strengthxp` double default NULL,
`Hitpointslvl` double default NULL,
`Hitpointsxp` double default NULL,
`Rangelvl` double default NULL,
`Rangexp` double default NULL,
`Prayerlvl` double default NULL,
`Prayerxp` double default NULL,
`Magiclvl` double default NULL,
`Magicxp` double default NULL,
`Cookinglvl` double default NULL,
`Cookingxp` double default NULL,
`Woodcuttinglvl` double default NULL,
`Woodcuttingxp` double default NULL,
`Fletchinglvl` double default NULL,
`Fletchingxp` double default NULL,
`Fishinglvl` double default NULL,
`Fishingxp` double default NULL,
`Firemakinglvl` double default NULL,
`Firemakingxp` double default NULL,
`Craftinglvl` double default NULL,
`Craftingxp` double default NULL,
`Smithinglvl` double default NULL,
`Smithingxp` double default NULL,
`Mininglvl` double default NULL,
`Miningxp` double default NULL,
`Herblorelvl` double default NULL,
`Herblorexp` double default NULL,
`Agilitylvl` double default NULL,
`Agilityxp` double default NULL,
`Thievinglvl` double default NULL,
`Thievingxp` double default NULL,
`Slayerlvl` double default NULL,
`Slayerxp` double default NULL,
`Farminglvl` double default NULL,
`Farmingxp` double default NULL,
`Runecraftlvl` double default NULL,
`Runecraftxp` double default NULL,
 index (playerName),

constraint fk_playername_rights
foreign key (playername) references players (playername)
on delete restrict
on update restrict 

) ENGINE=MyISAM;
ALTER TABLE skills ADD INDEX idx_playername (playername);

CREATE TABLE `playerrights` (
`playerName` varchar(15) NOT NULL default '',
`rank` int(2) default NULL,
index (playername),

constraint fk_playername_rights
foreign key (playername) references players (playername)
on delete restrict
on update restrict 

) ENGINE=MyISAM;
ALTER TABLE playerrights ADD INDEX idx_playername (playername);

CREATE TABLE `skillsoverall` (
`playerName` varchar(15) NOT NULL default '',
`lvl` int(11) default NULL,
`xp` BIGINT(11) default NULL,
index (playername),

constraint fk_playername_rights
foreign key (playername) references players (playername)
on delete restrict
on update restrict 

) ENGINE=MyISAM;
ALTER TABLE skillsoverall ADD INDEX idx_playerName (playerName);

1 个答案:

答案 0 :(得分:2)

不正确的查询是:

String updateSkillsoverallSql = "UPDATE skillsoverall"
+ " SET `lvl`=?,"
+ " `xp`=?,"
+ " WHERE `playerName`=?";

String updatePlayerRightsSql = "UPDATE playerrights"
+ " SET `rank` = ?,"
+ " WHERE `playerName`=?";

应该是:

String updateSkillsoverallSql = "UPDATE skillsoverall"
+ " SET `lvl`=?,"
+ " `xp`=?"
+ " WHERE `playerName`=?";

String updatePlayerRightsSql = "UPDATE playerrights"
+ " SET `rank` = ?"
+ " WHERE `playerName`=?";

在SET xp和SET等级之后的地方。发生在所有人身上。

P.S。您数据的简单方法可能是:

CREATE TABLE skills (
skill_name VARCHAR(200),
skill_level INT,
player_name VARCHAR(200)
);

然后你可以设置1技能(skill_name),当你掌握新技能时,你不需要改变你的表并且需要更少的代码来维护。