Wednesday, June 26, 2013

Re: getGeneratedKey() throws Exception

Yes. using 1 will always work (and is the way I've always done it). Each driver handles the names differently.

Rob


On Wed, Jun 26, 2013 at 9:30 AM, Jonas Gabriel <soli.gabriel@googlemail.com> wrote:
It said "ResultSet doesn't have a column XXX"... I was under the impression that I had to use to column name of my row id, but it seems just using 1 is the better option?

Am Mittwoch, 26. Juni 2013 18:18:42 UTC+2 schrieb Rob:
how does it fail? What's the SQLException or error?


On Wed, Jun 26, 2013 at 9:13 AM, Jonas Gabriel <soli.g...@googlemail.com> wrote:
Hi!
I have a problem with getting the key value after inserting a new row into Google Cloud SQL.
I could have sworn it worked before, but now somehow it doesn't...

String statement = "INSERT INTO xxxx ( xx) VALUES (?)";
PreparedStatement stmt = c.prepareStatement(statement, Statement.RETURN_GENERATED_KEYS);
stmt.setString(1, xx);
int i = 2;
i = stmt.executeUpdate();
if (i == 1) {
ResultSet result = stmt.getGeneratedKeys();
String resultCode = "1";
while (result.next()) {
resultCode = result.getString("xxid");
}
}

I can see the insert in my Database, so that works. And if I delete the stmt.getGeneratedKeys(); Method, everything works fine. But when I add that line, the query fails. What am I doing wrong here? (Makes no difference if I user RETURN_GENERATED_KEYS or 1...)

--
You received this message because you are subscribed to the Google Groups "Google Cloud SQL discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-cloud-sql-discuss+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

--
You received this message because you are subscribed to the Google Groups "Google Cloud SQL discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-cloud-sql-discuss+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

--
You received this message because you are subscribed to the Google Groups "Google Cloud SQL discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-cloud-sql-discuss+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

No comments:

Post a Comment