About Question enthuware.ocpjp.v7.2.1200 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
The_Nick
Posts: 132
Joined: Thu May 16, 2013 9:23 am
Contact:

About Question enthuware.ocpjp.v7.2.1200 :

Post by The_Nick »

Hi,
I see that the following line of code:

Code: Select all

if(!rs.isAfterLast()){ 
Is fundamental otherwise if I try to remove it I get a SQLException "invalid cursor".
Why would be that?
Furthermore instead of rs.isAfterLast() I could insert (!rw.getRow()==0) and I would have got the same result;

The_Nick

The_Nick
Posts: 132
Joined: Thu May 16, 2013 9:23 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1200 :

Post by The_Nick »

Forget about it, I got what it was.
It probably gets looped when loaded internally, that's why it's needed checking rs.isAfterLast in order to avoid failure.
The_Nick.

The_Nick
Posts: 132
Joined: Thu May 16, 2013 9:23 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1200 :

Post by The_Nick »

Wait a minute, actually I was wrong, the problem still persists:

Code: Select all

while(result.next())//result is a valid ResultSet
		{
			System.out.println(result.getInt(1));
		}
		
The above code does not need the !isAfterLast control so why for the RowSet in evaluate(RowSet rw) it's needed?

Thanks a lot in advance!

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1200 :

Post by admin »

It is not really required if you catch the exception in the filter and return false.

The reason given code uses it is because in dumpRS(), the loop uses while(rs.next()). So after the last rows is printed, the filter is invoked with the pointer pointing to "one after the last row". So now you can either expect an exception and catch it or avoid the exception by checking for isAfterLast.

HTH,
Paul.

The_Nick
Posts: 132
Joined: Thu May 16, 2013 9:23 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1200 :

Post by The_Nick »

admin wrote:It is not really required if you catch the exception in the filter and return false.

The reason given code uses it is because in dumpRS(), the loop uses while(rs.next()). So after the last rows is printed, the filter is invoked with the pointer pointing to "one after the last row". So now you can either expect an exception and catch it or avoid the exception by checking for isAfterLast.

HTH,
Paul.
Ok, however why in the following code there is no need of such a check?

Code: Select all

while(result.next())//result is a valid ResultSet
      {
         System.out.println(result.getInt(1));
      }
Does not it go in afterLast for the last call of getInt(1)?
Thanks in advance.


The_Nick.

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1200 :

Post by admin »

Sorry, not really sure why. I searched for more details in the documentation but couldn't find anything that could give any pointers.
-Paul.

Student
Posts: 53
Joined: Fri Sep 20, 2013 7:20 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1200 :

Post by Student »

Hi,
It says at http://docs.oracle.com/javase/tutorial/ ... ate-object:
To set the criteria for which rows in a FilteredRowSet object will be visible, you define a class that implements the Predicate interface. An object created with this class is initialized with the following:
  • The high end of the range within which values must fall
    The low end of the range within which values must fall
...
Note that the range of values is inclusive, meaning that a value at the boundary is included in the range.
In the implementation of evaluate in the example, it doesn't do this:

Code: Select all

 return gpa > this.level;
Should that not be

Code: Select all

 return gpa >= this.level;
? I am unclear: am I misunderstanding what Oracle are saying, or is what they say a guideline, and the code doesn't follow it? Or perhaps..none of the above :)
Thanks.

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1200 :

Post by admin »

The high/low end test can be implemented either using > or >= (or < or <=) . For example, if an acceptable end is say 3, you can do <=3 or <4 (assuming the data type is int). So there is no restriction on what you comparison code must look like.

In this case, the question has not mentioned any high/low end range. It shows a code and based on that code, you need to determine the result. Had the question mentioned that 4.0 is the low end starting point and then your point might have been valid.

HTH,
Paul.

Ambiorix
Posts: 25
Joined: Thu Jan 10, 2013 8:45 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1200 :

Post by Ambiorix »

Why would you go to the bother of creating a filter class when you could just include something like 'and GPA > 4.0' in the original select statement?

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1200 :

Post by admin »

Ambiorix wrote:Why would you go to the bother of creating a filter class when you could just include something like 'and GPA > 4.0' in the original select statement?
To illustrate an approach, of course :)

Ambiorix
Posts: 25
Joined: Thu Jan 10, 2013 8:45 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1200 :

Post by Ambiorix »

admin wrote:
Ambiorix wrote:Why would you go to the bother of creating a filter class when you could just include something like 'and GPA > 4.0' in the original select statement?
To illustrate an approach, of course :)
I was thinking more generally. What's the point of the FilterRowSet? Is it not always easier just to get the select statement to pick the rows that meet the criteria you want?

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1200 :

Post by admin »

You might want to apply a filter on an existing rowset. It is just a facility/feature. It might not be useful very often but it could be useful when the opportunity to fire query is not present or the way you want to filter is not possible to code in the query.

telston
Posts: 4
Joined: Wed May 21, 2014 8:44 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1200 :

Post by telston »

Section 9 of the exam guide at docs.oracle.com/javase/tutorial/extra/certification/javase-7-programmer2.html#jdbc does not cover the section about FilteredRowSet. It covers only RowSet as a general interface and JdbcRowSet. Does the actual exam cover detailed use of CachedRowSet, WebRowSet, FilteredRowSet, and JoinRowSet?

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1200 :

Post by admin »

Yes, we have seen candidates getting question on these RowSets.

See this: http://www.coderanch.com/t/657283/java- ... -Enthuware
HTH,
Paul.
Last edited by admin on Sat Nov 21, 2015 7:59 pm, edited 1 time in total.
Reason: Added JavaRanch link

lunars
Posts: 3
Joined: Sat Jun 28, 2014 8:43 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1200 :

Post by lunars »

If there is anyone else who, like me, was at first confused about the purpose of there being three evaluate methods, read the javadoc again more carefully! ;)

http://stackoverflow.com/questions/5619 ... ate-method

Basically, the non-RowSet overloads are called by FilteredRowSet when you insert rows, the RowSet one is for queries... Do I have that right?

So in the code from the present Enthuware question, the two non-RowSet overloads would have affected the studentRS if we had moved to the insert row, updated its columns, and ran insertRow(). I suppose, since they returned false in ALL cases, any attempt to insert rows at all would have been blocked?

Thanks for any info in advance!

Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests