尽管成功批量插入,ReturnGeneratedKeys为空

时间:2018-05-12 07:20:46

标签: java mysql mariadb

所以我最近开始了一个新项目,并且已经将数据插入到MariaDB表中。我以前能够获得生成的密钥,所以这对我来说有点不可思议。正在插入行,正如刷新我的表后所看到的那样,但是没有返回生成的键。这是我目前的代码。

    try (Connection con = DatabaseConnection.getConnection();
            PreparedStatement ps = con.prepareStatement("INSERT INTO items (chrid, accountid, type, pos, itemid) VALUES (?,?,?,?,?)", Statement.RETURN_GENERATED_KEYS)) {
            con.setAutoCommit(true);
            ArrayList<Pair<Integer, Equip>> equips = new ArrayList<>();
            for (Pair<Integer, Integer> item : items) {
                equips.add(itemCreationSystem.createEquip(item.right));
                ps.setInt(1, dbId.dbId);
                ps.setInt(2, client.accountId);
                ps.setInt(3, 0);
                ps.setInt(4, item.left);
                ps.setInt(5, item.right);
                ps.addBatch();
            }
            ps.executeBatch();
            try (ResultSet rs = ps.getGeneratedKeys()) {
                try (PreparedStatement ps2 = con.prepareStatement(
                        "INSERT INTO equips (itemKey, slots, successfulUpgrades, str, dex, intel, luk, hp, mp, " +
                                "wAtk, mAtk, wDef, mDef, equipPos)")) {
                    while (rs.next()) {
                        Pair<Integer, Equip> pair = equips.remove(0);
                        Equip equip = pair.right;
                            ps2.setInt(1, rs.getInt(1));
                            ps2.setShort(2, equip.upgradeSlots);
                            ps2.setShort(3, equip.successfulUpgrades);
                            ps2.setShort(4, equip.getProperty("STR"));
                            ps2.setShort(5, equip.getProperty("DEX"));
                            ps2.setShort(6, equip.getProperty("INT"));
                            ps2.setShort(7, equip.getProperty("INT"));
                            ps2.setShort(8, equip.getProperty("HP"));
                            ps2.setShort(9, equip.getProperty("MMP"));
                            ps2.setShort(10, equip.getProperty("PAD"));
                            ps2.setShort(11, equip.getProperty("MAD"));
                            ps2.setShort(12, equip.getProperty("PDD"));
                            ps2.setShort(13, equip.getProperty("MDD"));
                            ps2.setInt(14, pair.left);
                            ps2.addBatch();
                    }
                    ps2.executeBatch();
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

我一直在玩autoCommit,但没有得到任何结果。我怀疑也许是这样,但除非我在那里或在查询之后将其提交为true,否则不会插入任何行。

0 个答案:

没有答案