bạn SQLException
nêu rõ rằng:
Bạn cần phải xác định Statement.RETURN_GENERATED_KEYS
đến Statement.executeUpdate()
hoặc Connection.prepareStatement()
.
này có thể đạt được như sau (thêm một giá trị gia tăng trên Connection.prepareStatement()
phương pháp):
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();
Các Statement.RETURN_GENERATED_KEYS
là chìa khóa ở đây.
Hy vọng điều này sẽ hữu ích!
PS: Useful resource.
@Charlie berg, vì bạn thích lười biếng, tôi đã thay đổi dòng 13 của mã của bạn để bao gồm các 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);
Ngoài ra, Statement
lớp của gói java.sql
(chắc chắn rằng bạn nhập một cách chính xác). :-)
Xin chào Charlie, vì các liên kết bên ngoài có xu hướng phân hủy và biến mất (đặc biệt là pastebin), tốt hơn nên bao gồm mã và hình ảnh bạn muốn hiển thị trực tiếp trong câu hỏi của bạn. Bạn có thể làm như vậy bằng cách nhấp vào chỉnh sửa ngay phía trên hộp nhận xét. – James