mysql - Java Spring JDBC last_insert_id() -


i have web service , database(mysql), made table called batch must generate each time add value, unique key.

create table batch (     id int auto_increment primary key,     description varchar(250) not null ); 

this code database table , here wrote far:

static final string addbatchidsql = "insert batch" + "(description)" + "values (?)"; static final string getbatchidsql = "select last_insert_id()";      @override         public int getbatchid() {             dateformat dateformat = new simpledateformat("yyyy-mm-dd hh:mm:ss");             string batchidcreation = dateformat.format(new date());             jdbctemplate.update(addbatchidsql, batchidcreation);             sqlrowset rowset = jdbctemplate.queryforrowset(getbatchidsql);             while (rowset.next()) {                 system.out.println(rowset.findcolumn("id"));             }             return 0;         } 

the problem each time try id throws exception states "invalid column name", have checked naming properties in application in general , not find issue, wrong here ://

you can try aliasing column returned application's call last_insert_id():

static final string getbatchidsql = "select last_insert_id() id"; 

full code:

static final string addbatchidsql = "insert batch" + "(description)" + "values (?)"; static final string getbatchidsql = "select last_insert_id() id";  @override public int getbatchid() {     dateformat dateformat = new simpledateformat("yyyy-mm-dd hh:mm:ss");     string batchidcreation = dateformat.format(new date());     jdbctemplate.update(addbatchidsql, batchidcreation);     sqlrowset rowset = jdbctemplate.queryforrowset(getbatchidsql);     if (rowset.next()) {         system.out.println(rowset.findcolumn("id"));     }     return 0; } 

Comments