Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Unreasonable error: Invalid operation at current cursor position.

 
Rosie Fairfield
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I use the following method to find and display the record of a book with a title either passed into
the method or input from JTextField textSearchTitle if the empty string is passed in.

In order to return the ResultSet back to the current record so I can scroll forwards and backwards properly,
I call searchAndDisplayRecord() passing it the title of the current book; see line 50.

When I did this I began to get an error: Invalid operation at current cursor position. and my navigation
buttons didn't work.

I fixed this with the recordsBooks.first() call on line 48.

My question is: since I call recordsBooks.first() on line 25 before I run the ResultSet to find the book,
why do I have to do it again on line 48? It doesn't make sense and feels unclean to me or like I'm
missing something.

Any ideas?
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's the purpose of this method? As I understand: user enters a title of a book, the code searches the database for that book. If found, that book is displayed in the gui. If not found, the displayed book prior to the search is shown. Correct?
 
Rosie Fairfield
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel,

Yes that's right.
 
Dave Tolls
Ranch Hand
Posts: 2110
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where exactly is that error being thrown?

Also, where does resultSet reside?

Is it possible it's being shared somehow and somewhere else in your code it is being reused?
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rosie Fairfield wrote:Roel,

Yes that's right.

Based on your code it seems you have a resultset which is populated with the results of a SELECT * FROM Book query. And you are traversing this resultset to look for a title entered by the user. This is a very poor approach. First of all, if you have a million books in your Book table, you'll have to iterate over 815632 books to display book number 815633 and that's pretty slow. What if several books have the same title? You risk resource leaks if you don't close the resultset. And finally, it seems the resultset stys available during the complete execution of the application, even when it's not used (and the user goes out for a 1h lunch). So if your database allows only 10 active connections at any given time, user 11 can only run the application if one of the 1st 10 users closes the application (and you properly close the used resultsets, statements and connections).

Why don't you implement this method as follows:
- user enters title to search books
- the query SELECT * FROM Book WHERE title = ? is executed (? is replaced with the title entered by the user)
- if the resultset is not empty, these books are displayed (because title can result in more than 1 search result, unless it's a primary key)
- if the resultset is empty, an error message is shown to the user

This method implementation makes much more sense and doesn't have any of the aforementioned issues of the current implementation.

Hope it helps!
Kind regards,
Roel
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic