英文:
"java.lang.ClassCastException: org.apache.derby.client.net.NetConnection cannot be cast to smartdatabase.Connection" anyone got any idea why?
问题
我非常确定我的连接名称是正确的,“smartdatabase”是此代码所在的包,“SmartData”是我在NetBeans IDE 8.2中的嵌入式数据库(我认为它是Derby数据库)。
package smartdatabase;
import java.sql.*;
import org.apache.derby.jdbc.EmbeddedDriver;
public class Connection {
private Connection conn = null;
private Statement stmt = null;
public Connection() {
}
public void getStudents() {
try {
Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance();
conn = (Connection) DriverManager.getConnection("jdbc:derby://localhost:1527/SmartData;create=true;", "root", "The password");
String sql;
sql = "SELECT Firstname, Surname FROM tblStudents";
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
String Firstname = rs.getString("FirstName");
String Surname = rs.getString("Surname");
System.out.println(Firstname + " " + Surname);
}
} catch (SQLException se) {
se.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
英文:
I am 100% sure my connection name is correct, "smartdatabase" is the package in which this code exists and "SmartData" is my embedded database within NetBeans IDE 8.2 (I believe it is a Derby database).
package smartdatabase;
import java.sql.*;
import org.apache.derby.jdbc.EmbeddedDriver;
public class Connection {
private Connection conn = null;
private Statement stmt = null;
public Connection() {
}
public void getStudents() {
try {
Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance();
conn = (Connection)
DriverManager.getConnection("jdbc:derby://localhost:1527/SmartData;create=true;", "root", "The
password");
` `String sql;
sql = "SELECT Firstname, Surname FROM tblStudents";
ResultS``et rs = stmt.executeQuery(sql);
while (rs.next()) {
String Firstname = rs.getString("FirstName");
String Surname = rs.getString("Surname");
System.out.println(Firstname + " " + Surname);
}
} catch (SQLException se) {
se.printStackTrace();
} catch (`Exception e) {
e.printStackTrace();
}
}
}
答案1
得分: 2
你的smartdatabase
包中可能有一个名为Connection
的类。
这个类用于替代java.sql.Connection
。
默认情况下,编译器会从同一个包中解析Connection
。
尝试重命名该类,或者使用完全限定名(java.sql.Connection
)而不仅仅是Connection
:
private java.sql.Connection conn = null;
英文:
You probably have a class Named Connection
in your smartdatabase
package.
This class is used instead of java.sql.Connection
By default the compiler resolves Connection
from the same package.
Try renaming that class or use the fully qualified name (java.sql.Connection
) instead of just Connection:
private java.sql.Connection conn = null;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论