Java中无效的列名,但可用于数据库

时间:2019-01-20 19:40:21

标签: java mysql sql sqlexception cachedrowset

给出以下(MYSQL-)查询:

SELECT view_match_metric.player_id, discord_id, view_match_metric.name, 
  view_match_metric.xp AS xpthis, player.xp AS xptotal, ranked, mode, mu, sigma, 
  IF(team = 'Alpha', goals_alpha > goals_beta, goals_beta > goals_alpha) as won 
FROM view_match_metric 
LEFT OUTER JOIN kl_idmap ON view_match_metric.player_id = kl_idmap.player_id 
LEFT OUTER JOIN kl_trueelo ON view_match_metric.player_id = kl_trueelo.player_id 
LEFT OUTER JOIN player ON view_match_metric.player_id = player.player_id 
WHERE match_id="169498" 

当我在数据库程序(管理员)上执行它时,它返回以下结果:

table

请注意,它确实包含xpthis和xptotal的int(10)个未签名的条目

但是在Java中,此查询抛出

java.sql.SQLException: Invalid column name
at com.sun.rowset.CachedRowSetImpl.getColIdxByName(Unknown Source)
at com.sun.rowset.CachedRowSetImpl.getInt(Unknown Source)
xpthis和xptotal字段的

。它确实可以在不获取这些字段的情况下工作。 由于xp字段在多个表中是模棱两可的,因此我确实使用别名来指定它们。

用于加载数据的代码:

while (result.next()) {
    match.mode = result.getString("mode");
    match.ranked = result.getBoolean("ranked");
    TrueEloPlayerData player = new TrueEloPlayerData();
    player.id = result.getLong("player_id");
    player.discordID = result.getLong("discord_id");
    player.name = result.getString("name");
    //exception thrown in next line:
    player.xp = result.getInt("xpthis");
    player.level = Utility.getLevel(result.getInt("xptotal"));
    //some more
    match.players.add(player);
}

对于并行处理,通常的ResultSet被复制到CachedRowSet中:

CachedRowSet res = RowSetProvider.newFactory().createCachedRowSet();
res.populate(resultSet);

我尝试使用getDouble,getString,甚至使用getObject来获取它们,试图直接访问不带别名的字段(getInt(tablename.xp)),但是它从未解决问题。

我完全不知道为什么它不能像在数据库中那样在这里工作。

1 个答案:

答案 0 :(得分:1)

还有另一种基于索引的同名方法。

int getInt(int columnIndex) throws SQLException

尝试查看是否可行。我从图像中看到的是4索引(从1开始编号列)

player.xp = result.getInt(4);