0

I am using Netbeans 6.9.1, and glassfish 3.1, the DB is MySql.

There is a table in the database called HotelNames, i need to write a SQL and pass the Hotel name to get its hotel ID. I get an exception which i am unable to solve.

    @Override
public int GetHotelID(String hotellName) {
   Query query  = em.createNativeQuery("select ID from HotelNames where hotName ='"+ hotellName+"'");
    String hotelID =  (String) query.getSingleResult();

    return Integer.parseInt(hotelID );
}

The exception i get points to the SQL i wrote in the above code

Caused by: java.lang.ClassCastException: java.math.BigInteger cannot be cast to java.lang.String

MySQL create table looks like;

CREATE TABLE HotelNames (`ID` BIGINT NOT NULL AUTO_INCREMENT, `hotName` VARCHAR(255), PRIMARY KEY (`ID`));

I think its because of the BIGINT in the SQL and int in the code that's causing this, but i am unable to solve this.

1
  • The error is not in the return, its the line above. But, why do you try to parse a BigInt to String and then to Int? Commented Aug 1, 2011 at 6:52

4 Answers 4

4

You may want to do something like this

Number hotelID =  (Number) query.getSingleResult();
return hotelID.intValue();
Sign up to request clarification or add additional context in comments.

Comments

0

Your query returns an BigInteger. So you can get an int like that:

        return ((BigInteger)  query.getSingleResult()).intValue();

Comments

0

java.math.BigInteger cannot be cast to java.lang.String - You are trying to convert the ID to string, which is BigInteger.. See String hotelID = (String) query.getSingleResult();

Comments

0
BigInteger bg = new BigInteger();
bg = query.getSingleResult();
String hotelId = bg.toString();

I hope this explains more.....you can also do it in one line if you wish to...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.