如何从mysql获取数组?

时间:2014-05-18 21:08:32

标签: mysql

我想从我的数据库中获取一个演员列表,但是我收到了一个错误,

我在google上搜索了一下,似乎Mysql不支持这种带阵列的操作,我该怎么办?

 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 个答案:

答案 0 :(得分:1)

MySQL不支持按原样存储数组。我假设您正在尝试将actor列表(实际上只是结果集中的多行)放入数组中。
您可以使用此代替if块:

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

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

System.out.println(actors);