Question: I have created a service in which when user press power button 3 times sos service should be started. I need following requirement
When first time it receive a capture count is equal to 1.
When count=1 timer should be start for 1 minute . In that 1 minute when count =3 my sos service should be started. How to do this?
I have tried this.

```
 public void onReceive(Context paramAnonymousContext, Intent paramAnonymousIntent) {
            Calendar cal = Calendar.getInstance();
            long time = cal.getTimeInMillis();
            String str = String.valueOf(time);
            str = str.substring(0,9);
            time = Long.parseLong(str);

            Long timestamp = pref.getLong("timestamp",0);
            int counter = pref.getInt("counter",0);
            if(timestamp != 0){
                if(time == timestamp){
                    counter++;
                    if(counter == 3){
                        editor.putInt("counter",0);
                        editor.putString("SOS","SOS OFF");
                        editor.putInt("SOS_Flag",0);    //for power button
                        Log.e("power button counter",String.valueOf(counter));
                        startService(new Intent(paramAnonymousContext, SosService.class));
                    }else{
                        editor.putInt("counter",counter);
                        editor.putLong("timestamp",time);
                    }
                }else{
                    editor.putLong("timestamp",time);
                    editor.putInt("counter",counter);
                }
            }else{
                editor.putLong("timestamp",time);
                editor.putInt("counter",counter);
            }
            editor.apply();

```

Answer: 
you could initialize your `Statement` as the following 

```
conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);

```

Due to this you can move the cursor around in the Statement.

Now you could either loop through it. 

```
while(re.next())
    num_row++;
re.beforeFirst();

```

but this is quite unneccessary and the optimal solution would be to just jump to the end of the set and return the row

```
num_row = 0;
if(re.last()) {
   num_row = rs.getRow();
   re.beforeFirst();
}

```
Second `rs.next()` is not working because rs already reached into the end position with your first loop.

You can store `re.next()` this into temporary variable.

For example `ResultSet tmpRs_1 = rs;`
`ResultSet tmpRs_2 = rs;`

and then use this two variable for two loop.

Or , 

You can do all the operation within single loop. So that you should not need to two loop.