你SQLException
中明確指出:
你需要指定Statement.RETURN_GENERATED_KEYS
到 Statement.executeUpdate()
或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
在這裏是關鍵。
希望這會有所幫助!
PS:Useful resource。
@Charlie伯格,因爲你喜歡偷懶,我改線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
(確保你正確導入的)。 :-)
您好查理,因爲外部鏈接往往會衰減並消失(尤其是pastebin),所以最好在您的問題中直接顯示代碼和圖片。您可以通過單擊評論框上方的編輯來完成此操作。 – James