运行此代码时出现“无效游标状态”错误。
我使用此代码从Access数据库中获取结果集,然后将其存储在表中。
此段代码仅用于从结果集中获取数据:
data=new Object[numberOfRows][numberOfColumns];
for(int i=0;i<numberOfRows;i++){
int j=0;
while(j<numberOfColumns){
// int colType=rsmd.getColumnType(j+1);
// System.out.println(colType);
data[i][j]=rs.getInt("course_id");
System.out.print("0th: "+data[i][j]+" ");
j++;
data[i][j]=rs.getString("course_name");
System.out.print("1st:"+data[i][j]+" ");
j++;
data[i][j]=rs.getString("course_date");
System.out.print("2nd:"+data[i][j]+" ");
j++;
data[i][j]=rs.getString("course_number");
System.out.print("3rd: "+data[i][j]+" ");
j++;
System.out.print("j:"+j);
}
System.out.print("Done line:"+i);
}
答案 0 :(得分:2)
我认为主要的问题是你永远不会调用rs.next()
来滚动光标。但是,根据您对记录结构的澄清,您的while
循环似乎也搞砸了。请尝试以下方法:
data=new Object[numberOfRows][numberOfColumns];
for(int i=0;i<numberOfRows;i++) {
if (!rs.next())
throw new IllegalStateException();
int j=0;
// int colType=rsmd.getColumnType(j+1);
// System.out.println(colType);
data[i][j]=rs.getInt("course_id");
System.out.print("0th: "+data[i][j++]+" ");
data[i][j]=rs.getString("course_name");
System.out.print("1st:"+data[i][j++]+" ");
data[i][j]=rs.getString("course_date");
System.out.print("2nd:"+data[i][j++]+" ");
data[i][j]=rs.getString("course_number");
System.out.print("3rd: "+data[i][j++]+" ");
while(j<numberOfColumns) {
data[i][j]=rs.getString(j + 1);
System.out.print(j+"th: "+data[i][j++]+" ");
}
System.out.println();
System.out.println("Done line:"+i);
}