ClassCastException: java.math.BigInteger cannot be cast to java.lang.Long on connect to MySQL
Asked Answered
S

7

27

When connecting to MySQL, I get an error (see below).

Click here for code

I get this output:

run:
Now connecting to databse...

java.sql.SQLException: java.lang.ClassCastException: java.math.BigInteger cannot be cast to java.lang.Long
java.sql.SQLException: java.lang.ClassCastException: java.math.BigInteger cannot be cast to java.lang.Long
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1074)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:988)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:974)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:919)
    at com.mysql.jdbc.ConnectionImpl.buildCollationMapping(ConnectionImpl.java:1062)
    at com.mysql.jdbc.ConnectionImpl.initializePropsFromServer(ConnectionImpl.java:3556)
    at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2513)
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2283)
    at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:822)
    at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
    at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:404)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:317)
    at java.sql.DriverManager.getConnection(DriverManager.java:664)
    at java.sql.DriverManager.getConnection(DriverManager.java:247)
    at dbms_basic.Dbms_Basic.main(Dbms_Basic.java:28)
Caused by: java.lang.ClassCastException: java.math.BigInteger cannot be cast to java.lang.Long
    at com.mysql.jdbc.ConnectionImpl.buildCollationMapping(ConnectionImpl.java:1007)
    ... 15 more
BUILD SUCCESSFUL (total time: 0 seconds)

How can I solve this?

Skewer answered 9/9, 2017 at 13:37 Comment(2)
Moreover your link to code shows some other error and you have posted something different.Wobble
that is not actual error.Skewer
I
50

Your error clearly says casting is not possible, because a java.math.BigInteger class instance is not an instance of java.lang.Long class.

Now the question arises who is doing casting at what level, when we ask the JDBC driver to make a connection, it is doing lot of work behind the scene before it actually give us back the proper working object of connection.

The problem seems with your version of MySQL in combination with your version of mysql-connector.jar. Try a newer version of MySQL Connector/J (see https://dev.mysql.com/downloads/connector/j/ for the latest version), for example upgrade to 5.1.47 or 8.0.12 if you are using an older version.

Inesinescapable answered 9/9, 2017 at 16:28 Comment(4)
Bug#13958793 was already logged and resolved around this on mysql connector.Inesinescapable
changed connector's jar version from 5.1.21 to 5.1.45 - worked!Elder
It's saved me lot of timeLager
This answer solved my issue. Thanks! I would like also to add that to easily know the versions of each of the dependencies this Maven command helped a lot. mvn dependency:tree > tree.txtCb
T
6

Another way, because changing the version from mysql does not worked for me, for help another people:

Long.parseLong(String.valueOf(item[0]);
Thaw answered 11/3, 2021 at 18:35 Comment(0)
A
5

This issue is not there with 5.1.45 as mentioned in the above comments. Available to download at,

https://repo1.maven.org/maven2/mysql/mysql-connector-java/5.1.45/

Adagietto answered 13/4, 2019 at 21:16 Comment(1)
Tested on 5.1.47 with no error, however a warning about SSL showed up, I had to include useSSL=false at the url connection to get a rid of it.Koeninger
M
2

There is a miss-match between your MySQL version, which might be the latest 8.0.19, but the MySQL driver file is older version may be 5.1.23, which is generally available with the NetBeans IDE. To overcome this, download the mysql-connector-java-5.1.48.jar from this link in your PC download connector/j 5.1.48 zip file (4.6MB)

Now right-click on project name in the netbeans IDE, go to services, in that choose 'Libraries', in it choose 'Add library', then don't opt for available libraries( the drop-down menu will list a JDBCDriver file which has 'mysql-connector-java-5.1.23.jar' file inside it, which is an older version, this is causing the miss-match). Therefore, instead click on 'Create Library', now give it any name of your choice, then click the create button, a browse window will pop-up, go to the directory where you have downloaded the 'mysql-connector-java-5.1.48.zip' folder, open it and select the java jar file 'mysql-connector-java-5.1.48.jar' and click 'ok'. The library folder of your project tree will now show 'JDBCDriver-mysql-connector-java-5.1.48.jar' added in the list of libraries(JDK and Tomcat) . Now try connecting to your database again by clicking on the 'run' button, go to the JSP link, and you see that this time you are connected.

Marlo answered 20/3, 2020 at 19:18 Comment(0)
F
1

For me updating the connector wasn't enough, I also had to complete my DriverManager.getConnection() url parameter with all the arguments, even if the error message was not mentionning this issue.

In my case this parameters were needed : "jdbc:mysql://127.0.0.1:3306/database?zeroDateTimeBehavior=convertToNull&serverTimezone=UTC"

Connector : mysql-connector-java-8.0.17.jar mysql version : 8.0.17

Using java on netbeans.

Freezedry answered 20/9, 2019 at 15:7 Comment(0)
C
0

This is a common issue when you use outdated/unsupported mysql connector driver. If youre using x86 version of Netbeans, your driver will usually be found in the Program(x86) folder an not the Program folder. something like this "C:\Program Files (x86)\MySQL\Connector J 8.0\mysql-connector-java-8.0.23.jar"

Cogent answered 4/3, 2021 at 8:9 Comment(0)
J
0

try to convert the BigInteger value into a long value like this:

**((BigInteger)(valueFromDB)).longValue()**

so first we cast the value returned from db to BigInteger value, and than BigInteger object offers a method longValue() that converts the value to a long type. for me it worked fine!

Jeana answered 27/6 at 12:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.