gpt4 book ai didi

java - SQLException - 未请求生成的键 (MySQL)

转载 作者:IT老高 更新时间:2023-10-29 00:13:51 26 4
gpt4 key购买 nike

我在为我的游戏制作新角色时收到此错误,在 CreateCharHandler 中它发送“saveToDb(false);”但是当我与另一个我手动创建的字符进行游戏时,我可以 saveToDb(true);没有错误。请帮忙,为什么会这样?

http://i56.tinypic.com/oh1pn5.png

SaveToDb 方法 http://pastebin.com/9sT5XBxp

第 3514 行是:

ResultSet rs = ps.getGeneratedKeys();

提前致谢!

最佳答案

您的 SQLException 明确指出:

You need to specify Statement.RETURN_GENERATED_KEYS to the Statement.executeUpdate() or Connection.prepareStatement().

这可以通过以下方式实现(在 Connection.prepareStatement() 方法上添加一个附加值):

String SQL = ""; //whatever my String is
PreparedStatement ps = connection.prepareStatement(SQL, Statement.RETURN_GENERATED_KEYS);
ps.setString(1, "value");
//Other necessary ps.setXXX() methods

//now update
ps.executeUpdate();

ResultSet rs = ps.getGeneratedKeys();

Statement.RETURN_GENERATED_KEYS 是这里的关键。

希望这对您有所帮助!

附言:Useful resource .


@Charlie berg,因为你喜欢偷懒,我更改了你代码的第 13 行以包含 Statement.RETURN_GENERATED_KEYS:

ps = con.prepareStatement("INSERT INTO characters (level, fame, str, dex, luk, `int`, exp, hp, mp, maxhp, maxmp, sp, ap, gm, skincolor, gender, job, hair, face, map, meso, hpMpUsed, spawnpoint, party, buddyCapacity, messengerid, messengerposition, mountlevel, mounttiredness, mountexp, equipslots, useslots, setupslots, etcslots, monsterbookcover, watchedcygnusintro, vanquisherStage, dojopoints, lastDojoStage, finishedDojoTutorial, vanquisherKills, matchcardwins, matchcardlosses, matchcardties, omokwins, omoklosses, omokties, givenRiceCakes, partyquestitems, jailtime, accountid, name, world) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", Statement.RETURN_GENERATED_KEYS);

此外,Statement 类属于 java.sql 包(确保导入正确)。 :-)

关于java - SQLException - 未请求生成的键 (MySQL),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7162989/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com