我正在对玩家进行排名,我需要将结果输入Java。
我的代码是
public static int getRank(UUID uuid) throws SQLException {
checkConnection();
PreparedStatement s = con.prepareStatement(
"SET @rownum := 0; SELECT rank FROM ( SELECT @rownum := @rownum + 1 AS rank, wins, uuid FROM `SKYWARS` ORDER BY wins DESC ) as result WHERE uuid=?");
s.setString(1, uuid.toString());
ResultSet r = s.executeQuery();
r.next();
return r.getInt(1);
}
但是s.execute()返回false,这意味着没有结果集。如何获得结果集?在MySQL工作台中,它返回值rank并在网格中获胜。
解决方案:
我在连接语句中添加了?allowMultiQueries=true
。这使我的代码按原样工作。
答案 0 :(得分:0)
@barmar建议一个好的解决方案:
public static int getRank(UUID uuid) throws SQLException {
checkConnection();
PreparedStatement s1 = con.prepareStatement(
"SET @rownum := 0;");
s1.execute();
PreparedStatement s = con.prepareStatement(
" SELECT rank FROM ( SELECT @rownum := @rownum + 1 AS rank, wins, uuid FROM `SKYWARS` ORDER BY wins DESC ) as rank WHERE uuid=?");
s.setString(1, uuid.toString());
if (!s.execute()) {
System.out.println(123);
} else {
ResultSet r = s.executeQuery();
r.next();
return r.getInt(1);
}
return 0;
}
SET语句可以拆分。