"java.lang.ClassCastException: org.apache.derby.client.net.NetConnection cannot be cast to smartdatabase.Connection" anyone got any idea why?

huangapple go评论81阅读模式
英文:

"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;

huangapple
  • 本文由 发表于 2020年9月1日 02:41:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/63676433.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定