gpt4 book ai didi

java - Mysql 表对象列表到字符串数组通过 System.arraycopy 得到 ArrayStoreException?

转载 作者:行者123 更新时间:2023-11-29 18:05:37 24 4
gpt4 key购买 nike

我正在尝试在 Mysql、Hibernate 的帮助下用 java 编写示例。我在Mysql中创建了2个表,分别命名为table1、table2。每个有 3 列表1.行索引表1列1表1列2table2.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]

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

最佳答案

get_combined_two_tables 返回的列表将是一个对象数组列表,每个数组将代表查询返回的一行。

Hibernate Query list docs

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

ArrayStoreException Docs

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

关于java - Mysql 表对象列表到字符串数组通过 System.arraycopy 得到 ArrayStoreException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47882855/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com