Get a scalar in java

0

How can I read a scalar from java on sql server? This is my query to the database the insert works perfectly but I do not know how to read the second instruction

insert into Category values('Recursos Humanos','HR')
SELECT @@IDENTITY as insertado

I try to do the following in java

res=stmt.executeQuery("SELECT @@IDENTITY AS insertado");
int id=res.getInt("insertado");
System.out.println("ID insertado: "+id);

But mark exception because the column does not find any other idea of how to read this data? thanks

    
asked by Missael Armenta 10.06.2016 в 23:49
source

1 answer

1

Try this

...
ResultSet res=stmt.executeQuery("SELECT @@IDENTITY AS insertado");
if (res.next() ) {  
  int id = res.getInt(1);  
}
System.out.println("ID insertado: "+ Integer.toString(id));
    
answered by 11.06.2016 / 01:23
source