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

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

Click here for code

I get this output:

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(
    at com.mysql.jdbc.SQLError.createSQLException(
    at com.mysql.jdbc.SQLError.createSQLException(
    at com.mysql.jdbc.SQLError.createSQLException(
    at com.mysql.jdbc.ConnectionImpl.buildCollationMapping(
    at com.mysql.jdbc.ConnectionImpl.initializePropsFromServer(
    at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(
    at com.mysql.jdbc.ConnectionImpl.createNewIO(
    at com.mysql.jdbc.ConnectionImpl.<init>(
    at com.mysql.jdbc.JDBC4Connection.<init>(
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(
    at java.lang.reflect.Constructor.newInstance(
    at com.mysql.jdbc.Util.handleNewInstance(
    at com.mysql.jdbc.ConnectionImpl.getInstance(
    at com.mysql.jdbc.NonRegisteringDriver.connect(
    at java.sql.DriverManager.getConnection(
    at java.sql.DriverManager.getConnection(
    at dbms_basic.Dbms_Basic.main(
Caused by: java.lang.ClassCastException: java.math.BigInteger cannot be cast to java.lang.Long
    at com.mysql.jdbc.ConnectionImpl.buildCollationMapping(
    ... 15 more
BUILD SUCCESSFUL (total time: 0 seconds)

How can I solve this?


Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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 for the latest version), for example upgrade to 5.1.47 or 8.0.12 if you are using an older version.

Method 2

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

Method 3

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


Method 4

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

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 ‘’ 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.

Method 5

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 :

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

Using java on netbeans.

Method 6

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)MySQLConnector J 8.0mysql-connector-java-8.0.23.jar”

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Notify of

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x