Speak now
Please Wait Image Converting Into Text...
Embark on a journey of knowledge! Take the quiz and earn valuable credits.
Challenge yourself and boost your learning! Start the quiz now to earn credits.
Unlock your potential! Begin the quiz, answer questions, and accumulate credits along the way.
General Tech Bugs & Fixes 2 years ago
Posted on 30 Aug 2022, this text provides information on Bugs & Fixes related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.
Turn Your Knowledge into Earnings.
I have two columns in my table are which is BigInt data type (NODEID and ULNODEID) and I want to keep it that way. I am using MYSQL workbench 8.0 for these table.
I want to get the value of my nodeid using the function below:
public long get_urlnodeid(long nodeID) { try { String sql = "select NODEID from urllink where ULNODEID="+nodeID; if (em == null) { throw new Exception("could not found URL object."); } return (long) em.createNativeQuery(sql).getSingleResult(); } catch (Exception e) { msg = CoreUtil.wrapMsg(CoreUtil.FUNC_ERROR, this.getClass().getName(), "get", e.getMessage()); } return 0; }
It throws an exception saying Big Integer cannot be cast to java.lang.Long
Big Integer cannot be cast to java.lang.Long
Is there a way I can retrieve the value while keeping it in long?
long
Just look at the Java doc for BigInteger:
public long longValue()
Converts this BigInteger to a long. This conversion is analogous to a narrowing primitive conversion from long to int as defined in section 5.1.3 of The Java™ Language Specification: if this BigInteger is too big to fit in a long, only the low-order 64 bits are returned. Note that this conversion can lose information about the overall magnitude of the BigInteger value as well as return a result with the opposite sign.
So you'd want something like this:
return ((BigInteger)em.createNativeQuery(sql).getSingleResult()).longValue();
I would recommend adding some type checking.
--
Another option, if you have full control of your application, and you expect values that go beyond the range of long, is to have your method return BigInteger instead of long:
BigInteger
public BigInteger get_urlnodeid(long nodeID) {
And:
return (BigInteger) em.createNativeQuery(sql).getSingleResult();
Of course then the rest of your application that calls this method has to work with BigInteger as well.
Please be aware that using BigInteger instead of long is much less performant, so only use this if performance is not an issue or if you are absolutely sure that values will be so big that this is absolutely necessary.
No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.
General Tech 9 Answers
General Tech 7 Answers
General Tech 3 Answers
General Tech 2 Answers
Ready to take your education and career to the next level? Register today and join our growing community of learners and professionals.