1

I'm making an application to get data from Database , But I can't use executeQuery in my code. This is exception of my code : java.lang.ClassCastException: org.apache.derby.client.am.ClientStatement cannot be cast to java.beans.Statement at Database.main(Database.java:28) Where is the problem?

Connection MyconObj=null;
Statement MystateObj =null;
ResultSet MyresObj = null;
try {
 MyconObj = DriverManager.getConnection("jdbc:derby://localhost:1527/students", "root", "root");
 MystateObj = (Statement) MyconObj.createStatement();
 String query = "SELECT * FROM ROOT.INFORMATION";
 ResultSet MyresObj = MystateObj.executeQuery(query);
 while(MyresObj.next()){
 int id = MyresObj.getInt("id");
 String name = MyresObj.getString("name");
 String lastname = MyresObj.getString("lastname");
 System.out.println(id + "" + name + "" + lastname);
 }
} catch (Exception e) {
 e.printStackTrace();
}
Mark Rotteveel
110k241 gold badges160 silver badges233 bronze badges
asked Apr 18, 2019 at 11:16
8
  • 1
    what is the exception message? Commented Apr 18, 2019 at 11:18
  • What do you mean by 'can't use executeQuery in my code.' ? Are you getting an exception at runtime? Commented Apr 18, 2019 at 11:18
  • Please provide more information, like the error or exception stacktrace. And I strongly suggest that you start using the Java naming conventions (variables and fields starting with a lowercase letter: that makes your code more readable for people familiar with those conventions (ie most Java developers). Commented Apr 18, 2019 at 11:18
  • @FindOutIslamNow java.lang.ClassCastException: org.apache.derby.client.am.ClientStatement cannot be cast to java.beans.Statement at Database.main(Database.java:28) Commented Apr 18, 2019 at 11:19
  • 2
    Remove the cast and make sure that you import java.sql.Statement, not java.beans.Statement. Commented Apr 18, 2019 at 11:20

1 Answer 1

1

This is a classic example of how classes with the same name can cause confusion and runtime exceptions:

MystateObj = (Statement) MyconObj.createStatement();

The imports used by the implementation have not been included in the OP code, however, based on the thrown exception:

java.lang.ClassCastException: org.apache.derby.client.am.ClientStatement cannot be cast to java.beans.Statement

The Statement class used in the cast is java.beans.Statement, which has probably been imported. The object which has been returned by MyconObj.createStatement() is of the type org.apache.derby.client.am.ClientStatement and this causes the casting exception during runtime - the returned Object is an extension of java.sql.Statement, as mentioned in the comments above (by Mark Rotteveel).

The import of the Statement is correct but from the wrong library. This can easily occur when using IDEs which allow auto import generation (when more then one class matches the used class name, a drop-down list is displayed for possible imports. Selecting the wrong import is a common event, unfortunately).

answered Apr 18, 2019 at 11:27
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.