首页 > 解决方案 > 类型不匹配:无法从 void 转换为 ArrayList|| 在 JSP 文件中使用 Arraylist

问题描述

得到一个方法,它返回一个带有查询结果的 ArrayList:

public ArrayList<String> answerRequest(String table, String attribut) {

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

        try (Statement st = getMySQLConnection().createStatement();) {

            ResultSet daten = st.executeQuery("SELECT " + table + "." + attribut + " from " + table);

            while (daten.next()) {
                res.add(daten.getString(1));

            }

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return res;

    }

这很好用,我在我的 JSP 文件中调用该方法,如下所示:

<%
    ArrayList<String> res = new MySQLConnUtils().answerRequest("betreiber", "name");
for (int i = 0; i < res.size(); i++) {
    out.println(res.get(i));
}
%>

我得到了这个错误,我没有落后,因为函数“MySQLConnUtils().answerRequest”清楚地返回了一个数组......

An error occurred at line: [17] in the jsp file: [/TESTJSP.jsp]
Type mismatch: cannot convert from void to ArrayList<String>
14: <body>
15: 
16:     <%
17:         ArrayList<String> res = new MySQLConnUtils().answerRequest("betreiber", "name");
18:     for (int i = 0; i < res.size(); i++) {
19:         out.println(res.get(i));
20:     }

我感谢您的帮助!

标签: jsparraylisttype-mismatch

解决方案


推荐阅读