Search code examples
javamysqlnetbeansnetbeans-8

Error when trying to connect to MySQL server from netbeans IDE 8.2


When attempting to do this in my code:

Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/MazeJumperLeaderboardDB", "root", "password");

I always seem to get this error no matter what I try:

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)
    ... 61 more

I am using Java NetBeans IDE 8.2 and using MySQL Connector Java 8.0.18.

I have looked at other questions, notably this one: ClassCastException: java.math.BigInteger cannot be cast to java.lang.Long on connect to MySQL

But, doing this resulted in the same 'java.sql.SQLException: java.lang.ClassCastException: java.math.BigInteger cannot be cast to java.lang.Long' error.

Furthermore, I seem to be able to connect to my database driver just fine, but when attempting to connect to the MySQL server, it displays an error message with the same error yet again.

I'm really not sure what else to try at this point, so any help would be greatly appreciated!


Solution

  • I managed to (finally) solve my issue.

    The problem was with the pre-installed JDBC driver on NetBeans. The pre-installed JDBC driver was of version 5.1.23, which is outdated and did not match up with my installed JDBC driver from the MySQL website.

    So, what I did was:

    I deleted the pre-installed driver in the 'Libraries' folder on NetBeans (under your project, the libraries folder), then right clicked on my project, selected 'Properties', then 'Libraries', then I clicked 'Add JAR/Folder', then browsed through my system files and searched for the latest version of MySQL Connector that I had downloaded (Connector/J 8.0.18), and then I added the .zip file. Then I went over to the services tab, and re-established the connection to the database and drivers using the SAME MySQL Connector version, and then it worked and stopped giving me this error.