Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 220
Prashant Priyadarshi Avatar asked Sep 09 '17 13:09

Prashant Priyadarshi


2 Answers

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.

like image 54
Rizwan Avatar answered Sep 18 '22 17:09

Rizwan


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/

like image 39
Tom Taylor Avatar answered Sep 18 '22 17:09

Tom Taylor