所以在我的应用程序中,我试图迭代我的rs记录。我希望能够一次打印出一大堆记录,如果我有100条记录并且缺少记录3,12-15和40-60,我希望它显示已读取的记录。所以我可以知道其他记录丢失/跳过。我在这里发布了一个更完整的代码Java JDBC display first 500 records at a time, commit, than display the next 500 records and etc
updated records 1-2
updated records 4-11
updated records 13-39
updated records 61-100
try {
int rowCount = 0;
while (rs.next()) {
String ingressflag = rs.getString("ingress_flag");
String egressflag = rs.getString("egress_flag");
String ceingressflag = rs.getString("ce_ingress_flag");
String ceegressflag = rs.getString("ce_egress_flag");
int profileid = rs.getInt("profile_id");
preparedStatement.setString(1, ingressflag);
preparedStatement.setString(2, egressflag);
preparedStatement.setString(3, ceingressflag);
preparedStatement.setString(4, ceegressflag);
preparedStatement.setInt(5, profileid);
preparedStatement.addBatch();
rowCount++;
// System.out.println("profileid updated : " + profileid + " timestamp " + new java.util.Date() + "\n");
}
答案 0 :(得分:1)
我会使用一系列布尔值,这些布尔值与您拥有的记录数量相同。然后,当且仅当该记录已更新时,将每个对应值设置为True
。
然后从这个数组中你可以通过简单地循环遍历数组,以你建议的形式给出输出:
int first;
int last = 0;
while(!records[last]) //skip through records 1-x that are missing
last++;
first = last+1;
for(int i = last; i < numRecords; i++){
if(records[i]) //keep going if record there
last++;
else{ //otherwise print out previous "streak" and start anew
if(first == last) //streak of 1
System.out.println("updated record " + first);
else //streak of >1
System.out.println("updated records " + first + "-" + last);
while(!records[i]) //skip over entire missing section
i++;
first = i+1; //new first is next valid value
last = first;
}
}
if(records[numRecords-1])//if end reached without printing last streak
System.out.println("updated records " + first + "-" + last); //print it now
答案 1 :(得分:1)
如果您想查找哪个profile_id
缺失,可以尝试声明int
类型的变量说expectedId
并初始化到表格的起始profile_id
你的情况1.然后在while循环中检查返回的profile_id
是否等于expectedId
如果没有打印出消息,最后将expectedId
增加1,如下图所示。
int expectedId = 1;
while (rs.next()) {
//All your code
if(expectedId != profileid){
System.out.println ("Profile id "+expectedId+" to "+(profileid-1)+" missing.";
expectedId = profileid;
}
expectedId++;
}
每当序列丢失时,它会给出一个输出,说明缺少的id。