sql异常

bejyjqdl  于 2021-06-23  发布在  Mysql
关注(0)|答案(1)|浏览(258)

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

classcastexception:在连接到mysql时,java.math.biginteger不能转换为java.lang.long(4个答案)
两年前关门了。
这段代码在try语句后的第一行给我抛出了一个错误!我在库文件中有mysql驱动程序,没有问题!我正在使用NetBeans8.0版本!在另一个stackoverflow问题中,我看到了更改tomcat版本!我在这段代码中没有看到可能的类强制转换异常!

package jdbc;
import java.sql.*;
import com.mysql.jdbc.Driver;
public class Jdbc {
    public static void main(String[] args) {
        try
        {
            Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/Dust","root","32577488");
            Statement stmt=con.createStatement();
            ResultSet rs=stmt.executeQuery("CREATE TABLE Persons\n" +
                "(\n" +
                "PersonID int,\n" +
                "LastName varchar(255),\n" +
                "FirstName varchar(255),\n" +
                "Address varchar(255),\n" +
                "City varchar(255)\n" +
                ");");
            System.out.println("Hello world");
            while(rs.next())  
            System.out.println(rs.getInt(1)+"  "+rs.getString(2)+"  "+rs.getString(3));  
            con.close();  

        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    } 
}

日志:

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:422)
    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 jdbc.Jdbc.main(Jdbc.java:9)
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: 1 second)
vwkv1x7d

vwkv1x7d1#

尝试 executeUpdate 而不是 executeQuery . 它应该会回来 0 . 有关更多详细信息,请参阅javadoc。 executeQuery 通常用于javadoc中指出的sql select语句。

相关问题