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
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?
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.