我在为游戏制作新角色时收到此错误,它在CreateCharHandler中发送“saveToDb(false);”。但是当我与另一个角色在游戏中手动创建时,我可以保存ToDb(true);没有错误。请帮忙,为什么会这样?
http://i56.tinypic.com/oh1pn5.png
SaveToDb方法http://pastebin.com/9sT5XBxp
3514行是:
ResultSet rs = ps.getGeneratedKeys();
提前致谢!
最佳答案
您的SQLException
明确指出:
这可以通过以下方式实现(在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
是关键。希望这可以帮助!
PS: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-不需要生成的 key (MySQL),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7162989/