Getting last auto increment value in database
mysql
Solution
You should use `LAST_INSERT_ID()` after you insert something.
For LAST_INSERT_ID(), the most recently generated ID is maintained in the server on a per-connection basis. It is not changed by another client. It is not even changed if you update another AUTO_INCREMENT column with a nonmagic value (that is, a value that is not NULL and not 0).
Source
You may also try
SELECT max(id) FROM tableName
But it will not suppose deleted rows.
Problem
I am trying to retrieve the auto increment value of last inserted data in mySQL. Here is my code: ``` public int getAutoIncrementProductID() { ResultSet rs = null; DBController db = new DBController(); db.getConnection(); int autoIncKeyFromFunc = -1; rs = db.readRequest("SELECT LAST_INSERT_ID()"); try { if (rs.next()) { autoIncKeyFromFunc = rs.getInt(1); System.out.println("AUTO ID IS " + autoIncKeyFromFunc); rs.close(); } } catch (Exception e) { e.printStackTrace(); } db.terminate(); return autoIncKeyFromFunc; } ``` However, these codes keep returning me 0 value although the auto increment column in database is keep increasing. It just wont get the auto increment value of last inserted data. Anybody could help?