所以我想向表( PlayersHand )中添加 playerID (在另一个名为 Player 的表中)想要将CardsID,CardsName和typeOfDeck添加到该表( PlayersHand )。
我尝试使用RETURN_GENERATED_KEYS获取PlayersID,因为它是表 Player
中的自动增量列我需要的其他信息可以通过遍历玩家列表来获取他们的手牌和cardID,名称,牌组类型。
public void saveHand(List<Player> players) throws SQLException{
try (java.sql.Connection conn = DriverManager.getConnection(Connectie.JDBC_URL);
PreparedStatement query = conn.prepareStatement(INSERT_KAARTEN_IN_HAND)) {
/* private static final String INSERT_KAARTEN_IN_HAND = "INSERT INTO ID222177_g74.KaartenSpeler (idPlayer, idCard, cardName, deck)"
+ "VALUES (?,?,?, ?)";*/
int count = 0;
int teller = 0;
for (Player s : players) {
do {
query.setInt(1, getPlayerID());
query.setInt(2, s.getHand().get(teller).getKaartID());
query.setString(3, s.getHand().get(teller).getNaam());
query.setString(4, s.getHand().get(teller).getSoort()); query.addBatch();
teller++;
count++;
if (count % 100 == 0 || count == players.size()) {
query.executeBatch();
}
} while (teller <= s.getHand().size());
}
}
}
在另一个查询中,我使用它来获取玩家ID
try (ResultSet generatedKeys = query.getGeneratedKeys()) {
if (generatedKeys.next()) {
setPlayerID(generatedKeys.getInt(1));
}
基本上,一个玩家只能容纳5张或更少的卡,我想将这些卡保存在数据库中。所以对于每个玩家,我想保存他所有的牌。
现在我得到1个玩家,如果他手上有4个,则喜欢3张牌。但随后它停止了,对其他玩家无效。
所以应该像
PLayer 1 card1 cardName decktype
PLayer 1 card2 cardName decktype
PLayer 1 card3 cardName decktype
PLayer 1 card4 cardName decktype
PLayer 2 card1 cardName decktype
PLayer 2 card2 cardName decktype
PLayer 2 card3 cardName decktype
PLayer 2 card4 cardName decktype
PLayer 3 card1 cardName decktype
PLayer 3 card2 cardName decktype
PLayer 3 card3 cardName decktype
PLayer 3 card4 cardName decktype