连接到数据库时出现SQL异常

时间:2018-05-26 18:58:26

标签: java mysql

此代码在try语句后的第一行中抛出错误!我在库文件中有mysql驱动程序没有问题!我正在使用netbeans 8.0版本!在另一个stackoverflow问题中我看到改变tomcat版本!我在这段代码中看不到可能的类强制转换Exception!

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)

1 个答案:

答案 0 :(得分:0)

尝试使用executeUpdate代替executeQuery。它应该返回0。请参阅the JavaDoc for more details

executeQuery通常用于Java select语句,如JavaDoc中所示。