To obtain the value immediately after an INSERT, use a SELECT query with the LASTINSERTID() function. this is an oversimplification of what actually happens, but basically constructs the SQL statement while *ignoring the ID*, because. Obtaining the value of column that uses AUTOINCREMENT after an INSERT statement can be achieved in a number of different ways. String insertStatement = mainItem.asInsertStatement(true) // true, ignore IDs Also, if I leave the value of the Primary Key blank (which is the correct method) but fill in the other values for the row and click Add Row and Return to the table row display page, I get 2 blank rows, with the value of the Primary Key field +5 of where it ought to be. MainItem mainItem = values.getMainItem() In the final layer of the application (in another class where the values holder was passed as an argument), the data from the "holder" class is read and inserted into the database: // First insert the main item, belonging to the parent table
MainItemValuesHolder values = new MainItemValuesHolder() These get initialised and have getters and setters, omitted here for brevity's sake These inputs are stored in Java objects so that each row of the table corresponds to one of these objects - here are some examples: MainItem In a part of this application, the user can create a new item via a form or console input to enter details - some of these details are in the form of "sub-items" within the new item. Maybe there's a better solution to the problem for this particular application? Details below: I am already aware of the getGeneratedKeys() method in JDBC to do this following an insert, but my application requires the ID before insertion. TL DR: I want to be able to get the AUTO_INCREMENT id of a table before an INSERT statement. It uses parent/child tables linked with foreign keys. I'm developing a MySQL database project using JDBC.