ClassCastException: java.math.BigInteger 在连接到 MySQL 时不能转换为 java.lang.Long
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46131295/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ClassCastException: java.math.BigInteger cannot be cast to java.lang.Long on connect to MySQL
提问by Prashant Priyadarshi
When connecting to MySQL, I get an error (see below).
连接到 MySQL 时,出现错误(见下文)。
Click herefor 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?
我该如何解决这个问题?
采纳答案by Rizwan
Your error clearly says casting is not possible, because a java.math.BigInteger class instance is not an instance of java.lang.Long class.
您的错误清楚地表明无法进行转换,因为 java.math.BigInteger 类实例不是 java.lang.Long 类的实例。
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.
现在的问题是,谁在哪个级别进行转换,当我们要求 JDBC 驱动程序建立连接时,它在实际将连接的正确工作对象返回给我们之前在幕后做了很多工作。
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.
问题似乎出在您的 MySQL 版本与 mysql-connector.jar 版本的结合上。尝试使用较新版本的 MySQL Connector/J(有关最新版本,请参阅https://dev.mysql.com/downloads/connector/j/),例如,如果您使用的是较旧版本,请升级到 5.1.47 或 8.0.12版本。
回答by Tom Taylor
This issue is not there with 5.1.45 as mentioned in the above comments. Available to download at,
如上述评论中所述,5.1.45 不存在此问题。可在以下网址下载,
https://repo1.maven.org/maven2/mysql/mysql-connector-java/5.1.45/
https://repo1.maven.org/maven2/mysql/mysql-connector-java/5.1.45/
回答by Francois
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.
对我来说,更新连接器是不够的,我还必须DriverManager.getConnection()
用所有参数完成我的url 参数,即使错误消息没有提到这个问题。
In my case this parameters were needed :
"jdbc:mysql://127.0.0.1:3306/database?zeroDateTimeBehavior=convertToNull&serverTimezone=UTC"
在我的情况下,需要这个参数:
"jdbc:mysql://127.0.0.1:3306/database?zeroDateTimeBehavior=convertToNull&serverTimezone=UTC"
Connector : mysql-connector-java-8.0.17.jar
mysql version : 8.0.17
连接器:mysql-connector-java-8.0.17.jar
mysql 版本:8.0.17
Using java on netbeans.
在 netbeans 上使用 java。
回答by Ashutosh K Singh
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(4.6MB)
您的 MySQL 版本之间存在不匹配,可能是最新的 8.0.19,但较旧的 MySQL 驱动程序文件可能是 5.1.23,通常在 NetBeans IDE 中可用。要解决此问题,请从此链接下载 mysql-connector-java-5.1.48.jar,位于您的 PC下载连接器/j 5.1.48 zip 文件(4.6MB)
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 'mysql-connector-java-5.1.48.zip' 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.
现在在 netbeans IDE 中右键单击项目名称,转到服务,在其中选择“库”,在其中选择“添加库”,然后不要选择可用库(下拉菜单将列出一个 JDBCDriver 文件其中包含“mysql-connector-java-5.1.23.jar”文件,这是一个较旧的版本,这会导致不匹配)。因此,改为单击“创建库”,现在给它任何您选择的名称,然后单击“创建”按钮,将弹出一个浏览窗口,转到您下载“mysql-connector-java-5.1”的目录.48.zip'文件夹,打开它并选择java jar文件'mysql-connector-java-5.1.48.jar'并点击'ok'。项目树的库文件夹现在将显示“JDBCDriver-mysql-connector-java-5.1.48.jar” 添加到库列表(JDK 和 Tomcat)中。现在尝试通过单击“运行”按钮再次连接到您的数据库,转到 JSP 链接,您会看到这次已连接。