简体   繁体   English

错误:线程“ AWT-EventQueue-0”中的异常java.lang.ClassCastException:

[英]Error :Exception in thread “AWT-EventQueue-0” java.lang.ClassCastException:

My code is : 我的代码是:

try {
    Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
    String connectionUrl = "jdbc:sqlserver://localhost:1433;" +
    "databaseName=Java-Test;user=sa;password=199088037635;";
    link = (Connection) DriverManager.getConnection(connectionUrl);

} catch (ClassNotFoundException e) {
    System.out.println("Class Error: "+ e.toString());
} catch (SQLException se) {
    System.out.println("Driver Error: " + se.toString());
}

I get the error : 我得到错误:

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: com.microsoft.sqlserver.jdbc.SQLServerConnection cannot be cast to com.sun.corba.se.pept.transport.Connection at SQL.createAndShowGUI(SQL.java:42) 线程“ AWT-EventQueue-0”中的异常java.lang.ClassCastException:com.microsoft.sqlserver.jdbc.SQLServerConnection无法转换为SQL.createAndShowGUI(SQL.java的com.sun.corba.se.pept.transport.Connection :42)

Similar code in other projects runs correctly. 其他项目中的类似代码可以正确运行。 But in this project it doesn't run. 但是在这个项目中它没有运行。

Check your imports. 检查您的进口。 When you type in a class name the IDE checks whether it's something already in scope, and, if not, it gives you suggestions for what you can import. 当您键入一个类名时,IDE会检查它是否已经在范围内,如果没有,它将为您提供有关导入内容的建议。 The problem is, the IDE doesn't know which suggestions are more relevant so it can't order them so the most likely one comes up first. 问题是,IDE不知道哪些建议更相关,因此无法排序建议,因此最有可能先提出建议。 Nevertheless, people get used to taking the IDE's first suggestion reflexively, which can result in getting something (with the same name but from a totally different package) that isn't what you want at all. 但是,人们已经习惯于反身接受IDE的第一个建议,这可能会导致根本不需要的东西(具有相同的名称,但来自完全不同的软件包)。

I'm guessing the IDE inserted a line like 我猜IDE插入了这样的行

import com.sun.corba.se.pept.transport.Connection;

where it should be 应该在哪里

import java.sql.Connection;

which is the JDBC interface that com.microsoft.sqlserver.jdbc.SQLServerConnection implements. 这是com.microsoft.sqlserver.jdbc.SQLServerConnection实现的JDBC接口。

DriverManager.getConnection returns a java.sql.Connection and usually that interface exposes all the funcitonality you need, so you shouldn't need a cast here. DriverManager.getConnection返回一个java.sql.Connection,通常该接口公开您需要的所有功能,因此您在这里不需要强制转换。

Include this library in the build path of your project. 将此库包含在项目的构建路径中。

In Eclipse, this is done by **right clicking on the project > properties > Java Build Path > Add JARs... 在Eclipse中,这是通过**右键单击项目>属性> Java构建路径>添加JAR ...来完成的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 线程“ AWT-EventQueue-0”中的异常java.lang.ClassCastException错误 - Exception in thread “AWT-EventQueue-0” java.lang.ClassCastException error 线程“ AWT-EventQueue-0”中的java异常java.lang.ClassCastException - java Exception in thread “AWT-EventQueue-0” java.lang.ClassCastException 线程“AWT-EventQueue-0”中的异常java.lang.ClassCastException - Exception in thread “AWT-EventQueue-0” java.lang.ClassCastException 线程“ AWT-EventQueue-0”中的异常java.lang.ClassCastException:add()异常 - Exception in thread “AWT-EventQueue-0” java.lang.ClassCastException: Exception with add() 线程“AWT-EventQueue-0”中的异常java.lang.ClassCastException:java.util.ArrayList - Exception in thread “AWT-EventQueue-0” java.lang.ClassCastException: java.util.ArrayList 我的JFrame类中的线程“AWT-EventQueue-0”java.lang.ClassCastException中的异常 - Exception in thread “AWT-EventQueue-0” java.lang.ClassCastException in my JFrame class 线程“AWT-EventQueue-0”中的异常 java.lang.ClassCastException:javax.swing.JTable - Exception in thread “AWT-EventQueue-0” java.lang.ClassCastException: javax.swing.JTable 错误:线程“ AWT-EventQueue-0”中的异常java.lang.NullPointerException - ERROR : Exception in thread “AWT-EventQueue-0” java.lang.NullPointerException 线程“ AWT-EventQueue-0”中的异常java.lang.ArrayIndexOutOfBoundsException:-1错误 - Exception in thread “AWT-EventQueue-0” java.lang.ArrayIndexOutOfBoundsException: -1 error 错误“线程“AWT-EventQueue-0”中的异常java.lang.NumberFormatException” - Error "Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM