import testsuite.BaseTestCase; import java.sql.*; public class bug28505_3 extends BaseTestCase { public bug28505_3(String name) { super(name); // TODO Auto-generated constructor stub dbUrl = "jdbc:mysql://127.0.0.1:3351/test?user=root"; } public static void main(String[] args) { junit.textui.TestRunner.run(bug28505_3.class); } public void testBug3753() throws Exception { try { System.out.println("java.vm.version : " + System.getProperty("java.vm.version")); System.out.println("java.vm.vendor : " + System.getProperty("java.vm.vendor")); System.out.println("java.runtime.version : " + System.getProperty("java.runtime.version")); System.out.println("os.name : " + System.getProperty("os.name")); System.out.println("os.version : " + System.getProperty("os.version ")); System.out.println("sun.management.compiler : " + System.getProperty("sun.management.compiler")); this.stmt.executeUpdate("DROP TABLE IF EXISTS genkeytest"); this.stmt.executeUpdate("CREATE TABLE genkeytest (" + "id int(10) PRIMARY KEY AUTO_INCREMENT NOT NULL, " + "name char(20) NOT NULL, " + "UNIQUE INDEX name (name))"); for (int i = 0; i < 2; i++) { System.out.println("Running insert #"+i); int changed = this.stmt.executeUpdate("INSERT INTO genkeytest (id, name) " + "VALUES (NULL, 'test') ON DUPLICATE KEY UPDATE id = last_insert_id(id)"); // the following value changed in MySQL v5.0.36, see http://bugs.mysql.com/bug.php?id=19978 System.out.println(" executeUpdate() returned: "+changed); // see what this.stmt.getGeneratedKeys() returns ResultSet keysResultSet = this.stmt.getGeneratedKeys(); if (keysResultSet.next()) { // THIS IS THE EXPECTED BEHAVIOR * * * * System.out.println(" this.stmt.getGeneratedKeys.getInt(1) returned: "+keysResultSet.getInt(1)); } else { // THIS IS THE INCORRECT BEHAVIOP * * * * System.out.println(" No generated keys returned by this.stmt.getGeneratedKeys()"); } keysResultSet.close(); // see what LAST_INSERT_ID() returned ResultSet lastInsertIdResults = this.stmt.executeQuery("SELECT LAST_INSERT_ID()"); if (lastInsertIdResults.next()) { // This is what actually happens in MySQL 5.0.41, and is correct. System.out.println(" SELECT LAST_INSERT_ID() returned: "+lastInsertIdResults.getInt(1)); } else { System.out.println(" SELECT LAST_INSERT_ID() didn't return any results"); } lastInsertIdResults.close(); System.out.println(); } } finally { closeMemberJDBCResources(); } } }