I have a server with connection to a database in postgreSQL and I want to return the ID of a new row to the client once it is inserted. I've tried this:
int id = stmt.execute("insert into [table] values(default,0) returning id;");
the SQL statement works if I'm working directly on the database but not here, I get this error: org.postgresql.util.PSQLException: A result was returned when none was expected.
I'm thinking its because execute() and executeUpdate() aren't supposed to return values so I added this:
stmt.execute("insert into [table] values(default,0) returning id;");
ResultSet id = stmt.executeQuery("select last_insert_id();");
Apparently that statement is for mySQL so it doesn't work, I've read that CURRVAL and NEXTVAL do this in postgreSQL but I haven't been able to do it and I've also read it's not always acurate.
Is there something I'm missing or another way to do it?