0
import java.sql.*;

class MysqlCon {

    public static void main(String args[]) {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/banco", "matheus", "123456");
            Statement stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery("select * from usuario");
            while (rs.next()) {
                System.out.println(rs.getInt(1) + "  " + rs.getString(2) + "  " + rs.getString(3));
            }
            con.close();
        } catch (ClassNotFoundException | SQLException e) {
            System.out.println(e);
        }
    }
}

This code is throwing an error:

java.sql.SQLException: java.lang.ClassCastException: java.math.BigInteger cannot be cast to java.lang.Long

I also have an table on Workbench with columns:

id (int), name (varchar), age (varchar).

Any help?

Using 'e.printStackTrace()':

run:
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 MysqlCon.main(MysqlCon.java:10)
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
CONSTRUÍDO COM SUCESSO (tempo total: 1 segundo)
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 1
    Please correct you tags. This is not javascript. – Philip Jan 05 '19 at 18:52
  • When asking about an exception, always paste the exact and complete stack trace of the exception. Replace `System.out.println(e);` by `e.printStackTrace()`, and post the output. Also, don't use `select *`. Select the column you actually want to select, in the order you want. – JB Nizet Jan 05 '19 at 21:32
  • The stack trace shows the call to `DriverManager.getConnection()` is throwing the exception. Possible duplicate of [ClassCastException: java.math.BigInteger cannot be cast to java.lang.Long on connect to MySQL](https://stackoverflow.com/q/46131295/2985643). You probably need to update your driver. If that is not the case then update your question to specify: [1] The **version of MySQL**. [2] The **exact name of the driver** you are using. – skomisa Jan 09 '19 at 17:30

1 Answers1

-1

JDBC drivers often return BigInteger when the column type is an integer type.

Try this instead:

rs.getObject(1)

And let java handle the conversion to string.

If you really want the value as a long:

long id = ((BigInteger)rs.getObject(1)).longValue();
Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • Hi, the problem still persists for both suggestions. :( –  Jan 06 '19 at 13:16
  • Which line of your code is throwing the exception? If there are multiple calls you are making, separate them the into the different lines to isolate the problem. Use a debugger to trace the execution into the driver. – Bohemian Jan 06 '19 at 21:10
  • Hi Bohemian, I've replaced the int on the table for a string and as crazy as it sound the bug still remains. Idk how to use debugger, I mean, how do I trace line by line on Netbeans? –  Jan 07 '19 at 11:38
  • Watch [this](https://www.youtube.com/watch?v=joWldbcp1So) and [this](https://www.youtube.com/watch?v=2Z9B8wYhKWw) and search the web for "netbeans debug". – Bohemian Jan 07 '19 at 15:57
  • 1
    The stack trace shows that the exception is being thrown by the call to `DriverManager.getConnection()`, before the `ResultSet` is even declared. – skomisa Jan 09 '19 at 17:32