[英]How to correctly get the number of columns from a JDBC query
I have this code that converts a JDBC result set into a list of hash tables: 我有这段代码将JDBC结果集转换为哈希表列表:
private static ArrayList<HashMap<String, Object>> convertResultSet(ResultSet resultSet) throws SQLException {
ArrayList<HashMap<String, Object>> map = new ArrayList<HashMap<String, Object>>();
ResultSetMetaData rsmd = resultSet.getMetaData();
int nColumns = rsmd.getColumnCount();
while(resultSet.next()) {
HashMap<String, Object> hash = new HashMap<String, Object>();
for (int i = 0; i < nColumns; i++)
{
hash.put(rsmd.getColumnName(i), resultSet.getObject(i));
}
map.add(hash);
}
return map;
}
but when run, it throws a "Column index out of range." 但是运行时,它将抛出“列索引超出范围”。 exception. 例外。
Any ideas? 有任何想法吗?
如javadoc中所述,getColumnName(i)的索引从1开始,而不是0。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.