有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

这段代码在try语句后的第一行抛出错误!我在库文件中有mysql驱动程序,没有问题!我正在使用netbeans 8.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)

共 (1) 个答案

  1. # 1 楼答案

    试试executeUpdate而不是executeQuery。它应该返回0。见the JavaDoc for more details

    executeQuery通常用于JavaDoc中指出的SQL select语句