Mysql表对象列表到字符串数组由System.arraycopy得到ArrayStoreException?

问题描述 投票:0回答:1

我在Mysql,Hibernate的帮助下尝试java中的示例。我在Mysql中创建了2个表,名为table1,table2。每个都有3列table1.rowindextable1.column1table1.column2 table2.rowindextable2.column1table2.column2

场景为1.有时table1.column1值等于table2.column2。 2.它将通过table1.column2值找到。

所以查询我用过

SELECT a.column1, b.column1, a.column2, b.column2 FROM table1 a, table2 b where a.column1 = b.column1 and a.column2 ='Mandar';

它在Mysql工作台中显示了准确的结果。然后我写了java程序来获取这个结果并试图打印它。

我写了表提取函数如下: -

protected static List get_combined_two_tables(String string) {
    if (resultList != null) {
        resultList.clear();
    }
    flag_file = filecheckhibernate1(false);
    if (flag_file) {
        session = HibernateUtil.getSessionFactory("hibernate1.cfg.xml").openSession();
        if (session != null) {
            if (session.isOpen() == true) {
                try {
                    session.beginTransaction();
                } catch (TransactionException e) {
                    e.printStackTrace();

                } catch (JDBCConnectionException e) {
                    e.printStackTrace();

                }
                String selectQuery = "SELECT a.column1, b.column1, a.column2, b.column2 FROM table1 a, table2 b where a.column1 = b.column1 and a.column2 =:name";
                System.out.println("selectQuery = " + selectQuery);
                Query q = null;
                try {
                    q = session.createQuery(selectQuery);
                    q.setString("name", "Mandar");
                } catch (org.hibernate.QueryException e) {
                    e.printStackTrace();
                }
                if (q != null) {
                    resultList = q.list();
                    session.getTransaction().commit();
                }
                if (session.isOpen()) {
                    session.close();
                }

            }
        }
    }
    return resultList;
}

对于打印我写了以下功能: -

                        List<?> combined_table_List = null;
                    combined_table_List = get_combined_two_tables("Mandar");
                    if(combined_table_List != null) {
                        String[] dest = null;
                        for (int i = 0; i < combined_table_List.size(); i++) {
                                dest = new String[combined_table_List.get(i).toString().length()];
                                if(dest != null) {
                                    try {
                                        System.arraycopy(combined_table_List,0,dest,0,combined_table_List.get(i).toString().length());
                                    } catch (Exception e) {
                                        e.printStackTrace();
                                    }
                                        if(Arrays.toString(dest) != null) {
                                             System.out.println("Arrays.toString(dest) ="+Arrays.toString(dest));
                                        }   
                                }

                        }   
                    }

当我运行程序时,我得到如下异常: -

java.lang.ArrayStoreException
at java.lang.System.arraycopy(Native Method)
at test_db$1.run(test_db.java:132)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Arrays.toString(dest) =[null, null, null, null, null, null, null, null, 
null, null, null, null, null, null, null, null, null, null, null, null, 
null, null, null, null, null, null, null, null]

需要帮助来解决这个问题。

java mysql hibernate
1个回答
0
投票

get_combined_two_tables返回的List将是一个Object数组列表,每个数组将表示查询返回的行。

Hibernate Query list docs

当您尝试将此List复制到“dest”String []时,这将失败,因为这两种类型不兼容。即您正在尝试将对象数组列表插入到字符串数组中。

ArrayStoreException Docs

您需要将列表中的Object数组解析为字符串数组。

© www.soinside.com 2019 - 2024. All rights reserved.