执行结果集的遍历时出问题,求解

leo0235 2010-03-13 10:21:26

List list = null;
try {
if (con == null || !con.isClosed()) {
con = getConnection();
}
stmt = con.createStatement();
rs = stmt.executeQuery(sql);
if (rs != null) {
list = new ArrayList();
while (rs.next()) {
ArrayList temp = new ArrayList();
for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) {
temp.add(rs.getObject(i));
}
list.add(temp);
}
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
closed(); //关闭连接,释放资源
return list;
}
sql是个登录的sql语句。断点调试时,执行到while(rs.next())时值为true,继续执行却不进入循环而再次执行while(rs.next())值为false直接跳出循环。后面就抛异常java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
停止tomcat时有时候出错,信息如下:
ERROR: JDWP Unable to get JNI 1.2 environment, jvm->GetEnv() return code = -2
JDWP exit error AGENT_ERROR_NO_JNI_ENV(183): [../../../src/share/back/util.c:820]
是否是这个原因导致问题啊?
...全文
236 11 打赏 收藏 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
leo0235 2010-03-16
  • 打赏
  • 举报
回复
首先感谢,确实不能读两次.但是后续又出现问题.
登陆密码错误时,rs应该为空,但是却执行了
if (rs != null) {
list = new ArrayList();
将list初始化为Arraylist,返回list不为null导致后续代码异常

YangMacgrady 2010-03-15
  • 打赏
  • 举报
回复
学习下 纯粹····
awusoft 2010-03-15
  • 打赏
  • 举报
回复
有的,曾经试过rs.getString同一个字段不能读取两次
awusoft 2010-03-14
  • 打赏
  • 举报
回复
不要读取两次吧.改一下看看:

if (rs != null) {
list = new ArrayList();
int count = rs.getMetaData().getColumnCount();
while (rs.next()) {
ArrayList temp = new ArrayList();
for (int i = 1; i <= count; i++) {
temp.add(rs.getObject(i));
}
list.add(temp);
}
}

leo0235 2010-03-14
  • 打赏
  • 举报
回复
效果没有不同吧,rs.getMetaData().getColumnCount()就一常量,问题就出在temp.add(rs.getObject(i));但就想不通为什么.
sirtener 2010-03-13
  • 打赏
  • 举报
回复
把错误信息贴出来看看
sirtener 2010-03-13
  • 打赏
  • 举报
回复
是第几行报错
michaellufhl 2010-03-13
  • 打赏
  • 举报
回复
catch (SQLException e) -> catch (Exception e)
然后看trace.
leo0235 2010-03-13
  • 打赏
  • 举报
回复
List list = null;
try {
if (con == null || !con.isClosed()) {
con = getConnection();
}
stmt = con.createStatement();
rs = stmt.executeQuery(sql);
if (rs != null) {
list = new ArrayList();
while (rs.next()) {
ArrayList temp = new ArrayList();
System.out.println((String)rs.getObject(1));
System.out.println((String)rs.getObject(2));
System.out.println((String)rs.getObject(3));
System.out.println((Boolean)rs.getObject(4));
System.out.println((Integer)rs.getObject(5));
System.out.println((String)rs.getObject(6));
System.out.println((String)rs.getObject(7));
System.out.println(rs.getObject(8));
System.out.println((Integer)rs.getObject(9));
for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) {
temp.add(rs.getObject(i));
}
list.add(temp);
}
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println(e.getMessage().toString());
return null;
} finally {
closed();

测试数据获取正常,但是一进入for循环第一次执行temp.add(rs.getObject(i))则抛出异常 java.sql.SQLException: [Microsoft][ODBC SQL Server Driver]无效的描述符索引
xianrenxw 2010-03-13
  • 打赏
  • 举报
回复
错了, 回答错了。 打印出 rs看看。。
xianrenxw 2010-03-13
  • 打赏
  • 举报
回复
你的List 都是 null 呢, 。

62,620

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧