मैं जेडीबीसी/माईएसQL 5.1 के साथ खेल रहा हूं। मैंने तालिका में कुछ डेटा डालने के लिए insert
क्वेरी बनाई और नव निर्मित पंक्ति से जेनरेट की गई कुंजी को वापस करना चाहते हैं। हालांकि, जब मैं "आईडी" द्वारा कॉलम को संदर्भित करता हूं जो मेरा पीके और ऑटो-वृद्धि कॉलम है।क्यों GeneratedKeys() कॉलम नाम के रूप में "GENERATED_KEY" लौटाता है?
PreparedStatement ps = St0rm.getInstance().getDatabase("main")
.prepare("INSERT INTO quests (name,minlevel,start_npc,end_npc) VALUES(?,?,?,?)", true); // creates a prepared statement with flag RETURN_GENERATED_KEYS
// ...
int affected = ps.executeUpdate();
ResultSet keys = ps.getGeneratedKeys();
if (affected > 0 && keys.next()) {
St0rm.getInstance().getLogger().warning(String.format("ID Column Name: %s", keys.getMetaData().getColumnName(1))); // says the column name is: GENERATED_KEY
q = new Quest(keys.getInt(1)); // column index from the generated key, no error thrown.
q = new Quest(keys.getInt("id")); // actual column name, line throws a SQLException
// ...
}
तो, मेरे सवाल: क्यों स्तंभ नाम के रूप में ResultSet.getGeneratedKeys
उपयोग GENERATED_KEY
?