0
for(int j=0; j<artifactCount; j++)
{
        String first = r2.getString("context");                                         
        for(int k=0; k<relevantCount; k++)
        {
            String second = r4.getString("context");
            System.out.println(first + "        " + second);
            r4.next();
        }                   
        r2.next();
}

Hello all, I am trying to match each word of a coloumn with the other coloumn entries but first i'm trying to traverse the result using nested for loop. In 1st iteration the result comes correct, as there will be single word and a group of seperate words along side with it. Given below is the sample output of 1st iteration which is correct.

Technology      Products
Technology      Methodology
Technology      Project management
Technology      Risk
Technology      Management
Technology      Processes
Technology      Capitalism
Technology      Supply chain
Technology      Skill
Technology      Production and manufacturing

But in 2nd iteration it gives the following error as it doesn't traverse with the 2nd coming value with all the possible values.

java.sql.SQLException: After end of result set
at com.mysql.jdbc.ResultSet.checkRowPos(ResultSet.java:4353)
at com.mysql.jdbc.ResultSet.getStringInternal(ResultSet.java:2137)
at com.mysql.jdbc.ResultSet.getString(ResultSet.java:2132)
at com.mysql.jdbc.ResultSet.getString(ResultSet.java:2250)
at WebArtifactListener$2.run(WebArtifactListener.java:166)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)

Your help would be appreciated...thanx

0

2 Answers 2

0

You are trying to iterate r4 multiple times, which isn't supported: notice that you never say "go back to the start".

Copy its entries into a list first, outside the outer for loop, then iterate that list instead of r4.

List<String> r4contents = new ArrayList<>();
for(int k=0; k<relevantCount; k++) {
  r4contents.add(r4.getString("context"));
  r4.next();
}

// Then, in the inner loop...
String second = r4contents.get(k);
Sign up to request clarification or add additional context in comments.

Comments

0

You don't show the code that initialized the result sets, but you seem to expect that r4 somehow resets to the beginning just because the outer loop iterates. This is not the case, once r4 reaches the end of the returned rows, any further attempt to fetch from it will cause the error you see.

It may be possible to make a resultset rewindable depending on which database you are using, but you would have to explicitly move the cursor back to the first row at the top of each outer loop iteration.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.