i am using xammp database. and i want to get last ID from my database so that i can insert new stuff in it.
the code below doesnt get me the last it. its prints it is adding one next to last it. ex
7, 71, 711, 7111, 71111,
but i want ID to be 1, ... 7, 8, 9, 10, 11, 12 ...

try{
            Class.forName("com.mysql.jdbc.Driver");
            connect = DriverManager.getConnection(url, "root", ""); //connect with xammp database
            Statement sqlState = connect.createStatement();

            //-------------------------------------------------------------------------------
            //get last id
            String sqlString = "Select * from Score";
            ResultSet row = sqlState.executeQuery(sqlString);
            while (row.next()) {
                 lastID = row.getInt(1);
            }

            //insert in database
            sqlState.executeUpdate("insert into Score"+
                                     "(ID, User_Name, ScoreValue)"+
                                          "values('"+ lastID+1 +"', '"+userNameTemp +"', '"+ score +"')");


            sqlState.close();
            connect.close();
        }

only part that doesnt workis the lastid. rest works fine.

weird this bottom code works now. isnt they both done same thing.

//insert in database
 lastID++;
            sqlState.executeUpdate("insert into Score"+
                                     "(ID, User_Name, ScoreValue)"+
                                          "values('"+ lastID +"', '"+userNameTemp +"', '"+ score +"')");

what do you need that for anyway? a better way would be to have your id set as "auto increment". no need for the developer to "calculate" the next id, if the db can do all of that for you.

This question has already been answered. Start a new discussion instead.