0

i am trying to get a list of actors from my database ,but i am getting an error,

I search a little on google and it seems Mysql does not support this kind of operation with arrays ,How i can do it ?

 public void GetCast() throws SQLException, IOException {
                try {
                    int ID = idMovie;
                    String IDMOVIE = Integer.toString(ID);
                    IDMovieLabel.setText(IDMOVIE);



                    Class.forName("com.mysql.jdbc.Driver");

                    con = DriverManager.getConnection("jdbc:mysql://localhost/whichmovie", "Asis", "dekrayat24");
                    String sql ="SELECT Name From actor";
                    st = con.prepareStatement(sql);
                    rs = st.executeQuery(sql);
                    if (rs.next()) {

                        Array actors=rs.getArray(1);
                        String [] ListActors = (String [])actors.getArray();

                        System.out.println(ListActors);

                    }
                    rs.close();
                    st.close();
                    con.close();
                } catch (ClassNotFoundException ex) {
                   System.out.println(ex.getMessage());
                }



            }



Grave: null
java.sql.SQLFeatureNotSupportedException
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    at java.lang.Class.newInstance(Class.java:374)

1 Answer 1

1

MySQL does not support storing arrays as is. I'm assuming you are trying to get the list of actors (which are actually just multiple rows in the resultset) into an array.
You can use this instead of the if block:

ArrayList<String> actors = new ArrayList<>();

while (rs.next())
{
    actors.add(rs.getString(1));
}

System.out.println(actors);
Sign up to request clarification or add additional context in comments.

3 Comments

Yes ,they are just multiple rows .i will try your answer.
Great, it works .i Can't vote the answer because of the reputation.anyway Thank you very much for your help.
@Asis: On the left you should see a tick mark below the 'up' and 'down' symbols. Click it to mark the post as the answer, if you agree that it answered your question.

Your Answer

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.