从 ResultSet 读取时出现 SQLException "Column 'java.lang.Integer' not found"

SQLException "Column 'java.lang.Integer' not found" when reading from a ResultSet

我正在尝试进行一些基本的 ResultSet 到 JSONArray 转换。

有问题的代码如下:

ResultSetMetaData rsmd = rs.getMetaData();
JSONObject obj = new JSONObject();

String column_name = rsmd.getColumnClassName(1);

if(rsmd.getColumType(i) == java.sql.Types.Integer) {
    obj.put(column_name, rs.getInt(column_name));
}

当我将代码发布到网络服务器并点击页面时,它抛出以下错误:

java.sql.SQLException: Column 'java.lang.Integer' not found.
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:998)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:937)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:926)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:872)
at com.mysql.jdbc.ResultSetImpl.findColumn(ResultSetImpl.java:1073)
at com.mysql.jdbc.ResultSetImpl.getInt(ResultSetImpl.java:2571)
at com.sun.gjc.spi.base.ResultSetWrapper.getInt(ResultSetWrapper.java:482)
at com.pododdle.util.ToJSON.toJSONArray(ToJSON.java:58)
atcom.pododdle.restful.resources.CategoryResource.returnCategories(CategoryResource.java:44)

这有点疯狂,因为它抱怨找不到 java.lang.Integer - 核心 java class!!!!

当我使用等效字符串并说它也找不到 java.lang.String 时,它也失败了。

请帮忙。我开始失去理智了。

PS 我不知道这是否相关,但是 Maven 没有自动更新我想要的任何新依赖项,所以我手动添加了它们。我不知道这是否表明我的安装中存在更大的问题 - 这是非常新鲜的 - 昨天才在我的新机器上安装了 eclipse 等。

您正在使用 rsmd.getColumnClassName(1) 作为列名。现在显然是方法 ResultSetMetaData.getColumnClassName returns the name of the class for that column. In your case, this column stores an integer value, so java.lang.Integer is returned. Try to use ResultSetMetaData.getColumnName 而不是获取列的 name