因此,在我的应用程序中,我试图遍历我的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");
}
发布于 2015-07-13 16:15:35
如果您想找出缺少哪个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。
发布于 2015-07-13 15:46:09
我会使用一个与你拥有的记录数量相同大小的布尔数组。然后将每个对应的值设置为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
https://stackoverflow.com/questions/31387729
复制相似问题