如何将这个biginteger解为“java.lang.long”问题?

kiz8lqtg  于 2021-06-15  发布在  Mysql
关注(0)|答案(1)|浏览(369)

这个问题在这里已经有答案了

classcastexception:在连接到mysql时,java.math.biginteger不能转换为java.lang.long(4个答案)
去年关门了。

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);
        }
    }
}

此代码引发错误:
java.sql.sqlexception:java.lang.classcastexception:java.math.biginteger不能转换为java.lang.long
我在工作台上还有一个带有列的表:

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

有什么帮助吗?
使用“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)
ufj5ltwl

ufj5ltwl1#

当列类型是整数类型时,jdbc驱动程序通常返回biginteger。
请尝试以下操作:

rs.getObject(1)

让java处理到字符串的转换。
如果你真的想把价值作为 long :

long id = ((BigInteger)rs.getObject(1)).longValue();

相关问题