java.sql.SQLException: column 0 out of bounds [1,1]
程序员文章站
2022-06-15 13:43:10
...
使用ResultSet接收从数据库的查询结果,取里边的元素时报的这个错。
while (rs.next()){
System.out.println(rs.getObject(0));
}
java.sql.SQLException: column 0 out of bounds [1,1]
at org.sqlite.core.CoreResultSet.checkCol(CoreResultSet.java:84)
at org.sqlite.core.CoreResultSet.markCol(CoreResultSet.java:97)
at org.sqlite.jdbc3.JDBC3ResultSet.getObject(JDBC3ResultSet.java:599)
简单来说,下标要从1开始,不能从0开始,把下标改成1就OK了。
再分享一个ResultSet转List的方法,毕竟使用集合更顺手一些
private ArrayList<LinkedHashMap<String,String>> convertList(ResultSet rs) {
ArrayList<LinkedHashMap<String,String>> list = new ArrayList();
ResultSetMetaData md = null;
try{
md = rs.getMetaData();
int columnCount = md.getColumnCount();
while (rs.next()){
LinkedHashMap rowData = new LinkedHashMap();
for (int i = 1; i <= columnCount; i++) {
rowData.put(md.getColumnName(i), rs.getObject(i));
}
list.add(rowData);
}
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
下一篇: css实现四种常见边框内外角组合
推荐阅读
-
java.sql.SQLException: Incorrect string value: ‘\xF0\x9F\x8C\xB01\xEF...‘ for column ‘content‘ at
-
java.sql.SQLException: Incorrect string value: '\xF0\x9F\x98\x8E' for column 'name' at row 1
-
java.sql.SQLException: column 0 out of bounds [1,1]
-
java.sql.SQLException: Incorrect string value: ‘\xF0\x9F\x8C\xB01\xEF...‘ for column ‘content‘ at
-
java.sql.SQLException: Incorrect string value: '\xF0\x9F\x98\x8E' for column 'name' at row 1