Java获取当前插入行的自动递增编号,以将其用于其他查询? [英] Java getting the auto-increment number of the current inserted row, to use it for another query?

查看:100
本文介绍了Java获取当前插入行的自动递增编号,以将其用于其他查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public static void updateHistory(Client winner) {
    try {
        if (winner != null) {
            getDatabase().newQuery("INSERT INTO tournaments (`players`, `winner`, `winnerKills`, `map`, `prize`) VALUES ('" + tournyPlayers.size() +"', '" + winner.playerName + "', '" + winner.killCount + "', 'Wilderness', '" + winner.gameScore + "')");
        }
        else {
            getDatabase().newQuery("INSERT INTO tournaments (`players`, `winner`, `winnerKills`, `map`, `prize`) VALUES ('" + tournyPlayers.size() +"', 'None', 'None', 'Wilderness', 'None')");            
        }

        ArrayList<Client> player = tournyPlayers;

        for (int i = 0; i < player.size(); i++) {
            if (player.get(i).killedByPlayer == "") {
                getDatabase().newQuery("INSERT INTO tournament_players (`name`, `kills`, `killedBy`, `score`, `points`) VALUES ('" + player.get(i).playerName + "', '" + player.get(i).killCount + "', 'N/A', '" + player.get(i).gameScore + "', '" + player.get(i).gamePoints + "')");
            }
            else {
                getDatabase().newQuery("INSERT INTO tournament_players (`name`, `kills`, `killedBy`, `score`, `points`) VALUES ('" + player.get(i).playerName + "', '" + player.get(i).killCount + "', '" + player.get(i).killedByPlayer + "', '" + player.get(i).gameScore + "', '" + player.get(i).gamePoints + "')");                
            }
        }

    } catch (SQLException e) {
        e.printStackTrace();
    }       
}

好吧,我想要做的是,插入一个新的锦标赛行,该行将显示有关锦标赛的信息,现在我想将玩家添加到锦标赛中(谁在玩游戏),所以现在我将添加每个玩家,并将其数据放到新的一行.

Well what I am trying to do is, insert a new tournament row, which will display the information about the tournament, now I want to add the players to the tournament (Who was playing it), so I now will add each player, into a new row, with his data.

但是现在我要确保玩家与我插入的当前锦标赛匹配.我无法在服务器端确定当前服务器的唯一编号,因为服务器可能会停机并丢失计数.

But now I want to make sure that the player matches the current tournament I inserted. I can't make a unique number of the current server, server-sided because the server may go down, and lose the count.

因此,我考虑过使用插入的锦标赛表的auto_increment ID,并将其与插入的玩家一起添加,因此他的ID将是锦标赛的自动增量ID.

therefore I've thought of using the inserted tournaments table's auto_increment ID, and add it along with the inserted player, so his id will be tournament's auto increment id.

有没有一种方法可以获取自动增量ID,而无需使用我插入到锦标赛中的完全相同的数据(因为可能有2个锦标赛的状态相同,谁知道).

Is there a way to fetch the auto increment ID without using the exact same data I inserted into the tournaments (cause there may be 2 tournaments with the same status, who knows).

有办法吗?

Is there a way to do so?

我当时正在考虑将带有日期和时间的MD5哈希添加到锦标赛行中,然后将其添加到玩家行中,这是一个好的解决方案吗?

推荐答案

看看Statement#getGeneratedKeys(),它返回一个ResultSet,您可以将其用作检索任何生成的主键

Take a look at Statement#getGeneratedKeys() which returns a ResultSet that you can use to retrieve any generated primary keys as

ResultSet rsKeys = statement.getGeneratedKeys();
if (rsKeys.next()) {
    tournament.setId(rsKeys.getLong(1));
}

请注意,在准备PreparedStatement as

Please, note that you need to give a hint to the JDBC driver that you would like to retrieve the generated keys while preparing a PreparedStatement as

connection.prepareStatement(strSQL, Statement.RETURN_GENERATED_KEYS);

,或者在将Statement执行为

statement.executeUpdate(strSQL, Statement.RETURN_GENERATED_KEYS);

参考:
Statement#getGeneratedKeys()

Reference:
Statement#getGeneratedKeys()

检索由于执行此Statement对象而创建的任何自动生成的键.如果此Statement对象没有生成任何键,则返回一个空的ResultSet对象.

Retrieves any auto-generated keys created as a result of executing this Statement object. If this Statement object did not generate any keys, an empty ResultSet object is returned.

这篇关于Java获取当前插入行的自动递增编号,以将其用于其他查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆