java - The "route_id" not found -
i trying id
of inserted raw in routes
table. getting error column 'route_id' not found
route , direction being inserted in table before why getting error?
code:
databasemetadata dbm = con.getmetadata(); resultset routestables = dbm.gettables(null, null, "routes", null); int route_id; if (routestables.next()) { preparedstatement preproutesinsert = con.preparestatement( "insert routes(direction, route)" + "values( ?, ?)", statement.return_generated_keys); preproutesinsert.setstring(1, direction); preproutesinsert.setint(2, route); preproutesinsert.executeupdate(); try (resultset generatedkeys = preproutesinsert .getgeneratedkeys()) { if (generatedkeys.next()) { int id = generatedkeys.getint("route_id"); system.out.println("the id is: " + id); } }
routes table structure:
stt.execute("create table if not exists routes (" + "route_id int(11) not null auto_increment primary key," + "direction varchar(30) not null, " + "route int(11) not null )");
screenshot of geratedkeys variable:
edit: when query getting route_id
of raws in routes table:
preparedstatement preproutesinsert2 = con.preparestatement( "select route_id routes", statement.return_generated_keys); resultset rs = preproutesinsert2.executequery(); while(rs.next()){ int route_id2 = rs.getint("route_id"); system.out.println(route_id2); }
use
int id = generatedkeys.getint(1);
instead.
the comment posted in response this mysql bug says that:
you shouldn't retrieve these columns name. index, since there can ever 1 column mysql , auto_increments returns value(s) can exposed statement.getgeneratedkeys().
if want to, can use
int id = generatedkeys.getint("generated_key");
instead, , work, wouldn't recommend it. a similar question asks why name generated_key
used, , answer quotes same bug linked above.
Comments
Post a Comment