SQLException - generated keys are not requested (MySQL)

I get this error when I create a new character in my game, in CreateCharHandler it sends "saveToDb (false);" but when I play with another character that I created manually, I can save saveToDb (true); without mistakes. Please help, why is this happening?

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

SaveToDb Method http://pastebin.com/9sT5XBxp

line 3514 is:

ResultSet rs = ps.getGeneratedKeys(); 

Thanks in advance!

+12
source share
1 answer

Your SQLException clearly states that:

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

This can be achieved as follows (adding an additional value to the Connection.prepareStatement() method):

 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(); 

Here is Statement.RETURN_GENERATED_KEYS .

Hope this helps!

PS: Useful resource .


@Charlie berg, since you prefer being lazy, I changed line 13 of your code to include 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); 

In addition, the Statement class has a java.sql package (make sure you import correctly). :-)

+33
source

All Articles