17

I am trying to get the mysql command like mysql_insert_id(); which retrieve the last inserted row's auto_increment id. What can I do to get it in Java?

rs = st.executeQuery("select last_insert_id() from schedule"); lastid = rs.getString("last_insert_id()"); 

my lastid was declared as INT. I dono what to use in rs.get and also the parameter..

7 Answers 7

28

Using JDBC, you can use Connection.PreparedStatement(query, int) method.

PreparedStatement pstmt = conn.prepareStatement(Query, Statement.RETURN_GENERATED_KEYS); pstmt.executeUpdate(); ResultSet keys = pstmt.getGeneratedKeys(); keys.next(); key = keys.getInt(1); 
Sign up to request clarification or add additional context in comments.

6 Comments

I tried this method, but it always return 1. I somehow thought this is for checking did it insert any query into database?
@Maki92 i dont really understand your comment. this piece of code just returns the last auto-increment_id from your table . BTW, ASAIK this is the standard way of doing using JDBC.
I had seen people using it, but they faced same problem which same as mine. The code always return 1 from table.
@Maki92 i am most certainly sure that then its a problem in their code. i my self use it and never really faced such problems.. :)
I have the exact problem. That always returns 1 not matter what the real index is.
|
25

Try using an alias

rs = st.executeQuery("select last_insert_id() as last_id from schedule"); lastid = rs.getString("last_id"); 

2 Comments

Why do you add FROM statement? It's harmful because if your table contains 2 rows you'll get 2 rows as a result. "SELECT LAST_INSERT_ID();" is enough.
And instead of rs.getString("last_id") you can call rs.getString(1).
7

see this post for answer & explanation

Statement stmt = db.prepareStatement(query, Statement.RETURN_GENERATED_KEYS); numero = stmt.executeUpdate(); ResultSet rs = stmt.getGeneratedKeys(); if (rs.next()){ risultato=rs.getInt(1); } 

Comments

3

Why not

SELECT MAX(id) FROM schedule 

If id your column has a different name than id, you need to replace it accordingly in the above query.

You can use it like:

rs = st.executeQuery("SELECT MAX(id) AS id FROM schedule"); int lastid = rs.getInt("id"); 

6 Comments

I din't think of it.. haha.. Last time do PHP with using mysql_insert_id();, suddenly switched to java so I only focus with it. Thanks for suggestion. :)
int lastid = rs.getInt("id"); will throw an error since id doesn't exist. you should add an alias first on your select statement. rs = st.executeQuery("SELECT MAX(id) id FROM schedule");
@JW. Thank's for the hint. Updated my answer.
You should not rely on max(id), because another thread can insert a new row between your insert and select statements and you'll get wrong id.
Yep, same applies to mysql_inserted_id and last_inserted_id functions, which may also cause issues due to race conditions on other threads, specially considering that usually web applications share the same database user.
|
0

You can use following query to get last auto_incremented ID of last inserted row.

SELECT (max(auto_incr_id)) from table_name; 

Comments

0

Another option:

SELECT id FROM table_name ORDER BY id DESC LIMIT 1;

Comments

0

another way:

ResultSet rs = stmt.executeQuery("SELECT last_insert_id()"); while(rs.next()){ System.out.println("id = " + rs.getLong(1)); } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.