我想在用户的MySQL表中创建排名,按其得分排序。我已经找到了一个很好的查询,但是这个查询只与phpmyadmin一起使用,而不是在java ...
我得到了以下MySQL-Query,它在phpmyadmin中运行良好,可以创建排名:
SET @r=0; UPDATE `hg_stats` SET `stats_rank`= @r:= (@r+1) ORDER BY `stats_score` DESC;
在Java中:
try {
preparedStatement = connection.prepareStatement("SET @r=0; UPDATE `hg_stats` SET `stats_rank`= @r:= (@r+1) ORDER BY `stats_score` DESC;");
preparedStatement.executeQuery();
} catch (SQLException e) {
e.printStackTrace();
}
我收到以下错误,但查询无效:
[00:09:50] [Server thread/WARN]: 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 'UPDATE `hg_stats` SET `stats_rank`= @r:= (@r+1) ORDER BY `stats_score` DESC' at line 1
[00:09:50] [Server thread/WARN]: at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
[00:09:50] [Server thread/WARN]: at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
[00:09:50] [Server thread/WARN]: at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
[00:09:50] [Server thread/WARN]: at java.lang.reflect.Constructor.newInstance(Unknown Source)
[00:09:50] [Server thread/WARN]: at com.mysql.jdbc.Util.handleNewInstance(Util.java:407)
[00:09:50] [Server thread/WARN]: at com.mysql.jdbc.Util.getInstance(Util.java:382)
[00:09:50] [Server thread/WARN]: at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1052)
[00:09:50] [Server thread/WARN]: at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3593)
[00:09:50] [Server thread/WARN]: at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3525)
[00:09:50] [Server thread/WARN]: at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1986)
[00:09:50] [Server thread/WARN]: at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2140)
[00:09:50] [Server thread/WARN]: at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2626)
[00:09:50] [Server thread/WARN]: at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2111)
[00:09:50] [Server thread/WARN]: at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:2273)
[00:09:50] [Server thread/WARN]: at com.jolbox.bonecp.PreparedStatementHandle.executeQuery(PreparedStatementHandle.java:174)
[00:09:50] [Server thread/WARN]: at de.hg_pvp.game.stats.StatsManager.executeQuery(StatsManager.java:340)
[00:09:50] [Server thread/WARN]: at de.hg_pvp.game.stats.StatsManager.win(StatsManager.java:292)
[00:09:50] [Server thread/WARN]: at de.hg_pvp.game.HungerGamesPlugin.onDisable(HungerGamesPlugin.java:149)
[00:09:50] [Server thread/WARN]: at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:252)
[00:09:50] [Server thread/WARN]: at org.bukkit.plugin.java.JavaPluginLoader.disablePlugin(JavaPluginLoader.java:348)
[00:09:50] [Server thread/WARN]: at org.bukkit.plugin.SimplePluginManager.disablePlugin(SimplePluginManager.java:424)
[00:09:50] [Server thread/WARN]: at org.bukkit.plugin.SimplePluginManager.disablePlugins(SimplePluginManager.java:417)
[00:09:50] [Server thread/WARN]: at org.bukkit.craftbukkit.v1_7_R2.CraftServer.disablePlugins(CraftServer.java:398)
[00:09:50] [Server thread/WARN]: at net.minecraft.server.v1_7_R2.MinecraftServer.stop(MinecraftServer.java:384)
[00:09:50] [Server thread/WARN]: at net.minecraft.server.v1_7_R2.MinecraftServer.run(MinecraftServer.java:507)
[00:09:50] [Server thread/WARN]: at net.minecraft.server.v1_7_R2.ThreadServerApplication.run(SourceFile:618)
答案 0 :(得分:2)
您调用查询的事实上是一系列查询,以分号分隔。
但是你不能通过JDBC一次运行多个查询。
你需要这样的东西:
PreparedStatement ps1 = connection.prepareStatement
("SET @r=0");
ps1.ExecuteQuery();
PreparedStatement ps2 = connection.prepareStatement
("UPDATE `hg_stats` SET `stats_rank`= @r:= (@r+1) ORDER BY `stats_score` DESC");
ps2.ExecuteQuery();