Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cast smallint unsigned in Java

I'm using jOOQ to get id which in MySQL is smallint unsigned primary key auto_increment

public List<Integer> getID() {
   Factory sql = new Factory(Database.getInstance().connect(), SQLDialect.MYSQL);
   return (List<Integer>) sql.select().from("users").fetch().getValues("id_users");
}

And go error

org.jooq.tools.unsigned.UShort cannot be cast to java.lang.Integer

Here they wrote that smallint unsigned should be cast to int.

Edit Method should be

public List<UShort> getID() {
    Factory sql = new Factory(Database.getInstance().connect(), SQLDialect.MYSQL);
    return (List<UShort>) sql.select().from("users").fetch().getValues("id_users");
}

And in loop result should be cast to int.

like image 668
kskaradzinski Avatar asked Nov 23 '12 11:11

kskaradzinski


2 Answers

You cannot cast UShort into Integer as it does not inherit that class. I guess you should use UShort.intValue() to retrieve the Integer.

like image 152
Crozin Avatar answered Sep 29 '22 06:09

Crozin


The java lang cannot cast that directly. You need am intermediate step. Something like UShortval.intValue()
Iterate the result of the query, and build up a new List where you add the result of ushortval.intValue()

like image 42
AlexWien Avatar answered Sep 29 '22 07:09

AlexWien