Reputation: 32124
I used to execute this query with no problems what so ever, but lately i've been having exceptions.
(on a mysql server)
48442 [NioProcessor-1] ERROR c.x.xpofacebook.mysql.MysqlDb - SQLException: Before start of result set
48443 [NioProcessor-1] ERROR c.x.xpofacebook.mysql.MysqlDb - SQLState: S1000
48443 [NioProcessor-1] ERROR c.x.xpofacebook.mysql.MysqlDb - VendorError: 0
48451 [NioProcessor-1] ERROR c.x.xpofacebook.mysql.MysqlDb - stack trace: java.sql.SQLException: Before start of result set
The code:
String createPlayerRow = "insert into highscore_challenge_player "
+ "(fb_user_id,played,best_score,highscore_challenge_id) values ((select fb_user.id from fb_user where uid=?),?,?,?)";
stmt.close();
stmt = conn.prepareStatement(createPlayerRow,Statement.RETURN_GENERATED_KEYS);
stmt.setLong(1, uid);
stmt.setInt(2, 0);
stmt.setInt(3, 0);
stmt.setInt(4, highscoreChallengeId);
stmt.executeUpdate();
rs = stmt.getGeneratedKeys();
rowId = rs.getInt(1); <--- ERROR IN THIS LINE
any ideas ? !?
thanks
Upvotes: 0
Views: 1495
Reputation: 7554
You need to get to the first result before you can start looking at items in it.
More explicitly: you need to call rs.next()
at least once before you can ever access results. More generally, you need to call next
n times to look at the n-th row, and with sql results, the count starts at 1, not 0.
Upvotes: 1