• Post Reply Bookmark Topic Watch Topic
  • New Topic

Pagination (loop and conditional) Navigation for recordset  RSS feed

 
Erick Torrefranca
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Beginner here!
I dont know much in manipulating recordset and to use or call plug ins whatever it was called (total noob) or something or using taglib.
I want to organize my records in recordset into page so I coded some condition and loop statement to create pagination for my recordset.
I know that its a waste/bad example and there's some proper way out there but I just want to share my codes mostly the logic inside to my fellow beginners.
I really want to hear advice and critic!

btw the code is working and i didnt use try catch.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66260
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

you should not be doing DB access in your JSP page. In fact, a JSP should have no Java code in it at all!

What you should be doing is to perform the database access in Java classes in your Model layer, and to copy the data into "normal" Java collections (such as a List) as quickly as possible. Resultsets are expensive resources and should be closed as quickly as possible.

The List with the data grabbed from the database is what should be passed to the JSP page, which displays the data using the modern JSTL and EL (and not Java code).
 
Stefan Evans
Bartender
Posts: 1836
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear has already gone over the "don't mix java code in a JSP". 

Feedback on the code explicitly:

Your declarations:
You have used double variables. 
These should more likely be of type "int" rather than double, as they are always going to be whole numbers.



Potential for SQL injection with this style of SQL programming.
Should instead have a prepared statement:


Your method of outputting the results (scroll through every element in the result set until you get to the one you want)
It works, but isn't necessarily the most efficient :-)
You might consider using another method on the result set to do that navigation.  e.g. rs.absolute(x)



Remember with string comparisons, you need to use the .equals() method rather than the == operator.

 
Erick Torrefranca
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks @bear bibeault and @stefan evans
I dont have proper knowledge in java--  and I tried it while looking and copying codes (reading kills me). I get what stefan evans saying but
@ bear bibeault whats model layer? more likely can you guide me or give some guidelines in accessing db and displaying records in a web page.


 
Dave Tolls
Ranch Foreman
Posts: 3001
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The model is a set of Java classes you write to represent a row in your query (at least in your case).
That way the database classes (Connection, Statement, ResultSet) don't "leak" out into your program.  The rest of your code won't care where the data came from.

The database access is (when using JDBC, as you are) usually done in something called a DataAccessObject (DAO for short).
This would have methods like findAll(), findById(long id) and that sort of thing, which would return List<YourModel> in the first case and YourModel in the second.

That way all your database stuff is hidden away and, once you have the DAO working, you can forget about it.
 
Dave Tolls
Ranch Foreman
Posts: 3001
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Once you have the model and DAO then your servlet simply becomes something like:


Actually, that raises an interesting question.  How can you be paging when you are searching on an id?
That ought to return a single row.

Anyway, the version for a findAll would be:

The JSP then would use el/tags to display the List as a table.
 
Erick Torrefranca
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Dave Toll thanks.
I dont know how to set up a java class or use servlet taglib etc,
I just installed Java SE, Tomcat and Mysql then pasted the connector in web folder then started trying some codes.

what I suppose to do is move my connection in the DOA/Model layer/JSP page then just send request from a jsp page and receive the result from another page.

can you guys show some codes/example or url of how DOA/Model layer looks like

thanks
 
Dave Tolls
Ranch Foreman
Posts: 3001
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I would suggest learning Java before even considering going into web applications.

And then, when learning how Java web apps fit together, you should be starting with servlets, not JSPs.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66260
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Indeed, you need at least a rudimentary grounding in basic Java and a good understanding of OO concepts.

The, read this article to understand how to structure modern Java web applications. When you can understand the article, you are ready to begin.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!