Here is my sample code lines
String query = "INSERT INTO tb_product_group values(?,?,?,?,?) ON DUPLICATE KEY UPDATE prg_name = values(prg_name)";
stmtGroups = con.prepareStatement(query,Statement.RETURN_GENERATED_KEYS);
stmtGroups.setInt(index++, java.sql.Types.NULL);
stmtGroups.setString(index++, "");
stmtGroups.setString(index++, str[1]);
stmtGroups.setString(index++, str[2]);
stmtGroups.setInt(index++, oprDto.getId());
System.out.println(stmtGroups);
ResultSet rs = stmtGroups.getGeneratedKeys();
if (rs.next()){
groupID = rs.getLong(1);
}
first time when I insert a data like (null,"1","1","1",1); I get a groupID with newly generated ID. but on the second time I try to add same data which fires UPDATE statement because of unique key constraint. and that time I get groupID as a null.
I try REPLACE INTO tb_product_group values(?,?,?,?,?) but also get null as a generatedKeys.