2011-08-23 12 views
7

Viene visualizzato questo errore quando sto creando un nuovo personaggio nel mio gioco, nel CreateCharHandler invia "saveToDb (false);" ma quando imame con un altro carattere che ho creato manualmente posso salvareToDb (vero); senza errori. per favore aiuto, perché sta succedendo questo?SQLException - Chiavi generate non richieste (MySQL)

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

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

linea 3514 è rs ResultSet = ps.getGeneratedKeys();

Grazie in anticipo!

+0

Ciao Charlie, dal momento che i link esterni tendono a decadere e sparire (in particolare pastebin) è meglio includere il codice e le immagini che vuoi mostrare direttamente nella tua domanda. Puoi farlo facendo clic su modifica proprio sopra la casella dei commenti. – James

risposta

28

tuo SQLException afferma chiaramente che:

è necessario specificare Statement.RETURN_GENERATED_KEYS al Statement.executeUpdate() o Connection.prepareStatement().

Ciò può essere ottenuto come segue (aggiunta di un valore aggiunto a Connection.prepareStatement() metodo):

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

Il Statement.RETURN_GENERATED_KEYS è chiave qui.

Spero che questo aiuti!

PS: Useful resource.


@Charlie berg, dal momento che si preferisce essere pigro, ho cambiato la linea 13 del codice per includere il 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); 

Inoltre, Statement classe è del pacchetto di java.sql (assicuratevi di importare correttamente). :-)

+0

Grazie per il tuo aiuto! Ma sfortunato non capisco -facepalm- potresti per favore mostrarmi cosa cambiare nei miei codici e promettere che imparo: D. Tyty –

+0

@Charlie berg, basta cambiare la riga 13 a questo (guarda come ho appena aggiunto 'Statement.RETURN_GENERATED_KEYS' al tuo' con.prepareStatement'. –

+0

TY COSÌ TANTO: D! Sei un vero gentiluomo haha ​​ –

Problemi correlati