- Java code
private static DatabaseManager dbm = new DatabaseManager("codetable", 1000, "com.mysql.jdbc.Driver", "jdbc:mysql://localhost:3306/codetable", "root", "root"); public static void method2() { try { List<CustTable> list = new ArrayList<CustTable>(); List<CustTable> cutList = (List<CustTable>) dbm.executeQuery(list, "select * from custtable"); for (int i = 0; i < cutList.size(); i++) { System.out.println(cutList.get(i).getCustId());//异常产生地方 // System.out.println(cutList.get(i).getClass()); } dbm.close(); } catch (DatabaseException e) { e.printStackTrace(); } }package com.mainClass;public class CustTable { private String custId; private String value; private String codeNum; private String valueId; public CustTable() { } public CustTable(String custId) { this.custId = custId; } public CustTable(String custId, String value, String codeNum, String valueId) { this.custId = custId; this.value = value; this.codeNum = codeNum; this.valueId = valueId; } public String getCustId() { return custId; } public void setCustId(String custId) { this.custId = custId; } public String getValue() { return value; } public void setValue(String value) { this.value = value; } public String getCodeNum() { return codeNum; } public void setCodeNum(String codeNum) { this.codeNum = codeNum; } public String getValueId() { return valueId; } public void setValueId(String valueId) { this.valueId = valueId; } public String toString() { return custId + ", " + value + ", " + codeNum + ", " + valueId; }}
另外:cutList对象可以确定已经有内容不为空,经过调试发现cutList对象中有值。
异常提示是类型转换错误,不知道问题出在什么地方。
下面异常内容如下:
Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.mainClass.CustTable
at com.mainClass.TestClass.method2(TestClass.java:38)
at com.mainClass.TestClass.main(TestClass.java:64)
------解决方案--------------------
说明你的数据库的对象和你的自己的对象没关联起来
------解决方案--------------------
是不是你导入的包有错啊
eg:list 有两种导入包:java.util.list, java.awt.list,导入错误,也是有可能的
------解决方案--------------------
你把cutList.get(i).getCustId().toString()看看是什么