所以我最近开始了一个新项目,一直在向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();
}
我一直在玩自动提交,但没有得到任何结果。我怀疑可能就是这样,但除非我在那里或查询之后将其提交为true,否则不会插入任何行。
暂无答案!
目前还没有任何答案,快来回答吧!