0

So i have created a code that display database record into a html textbox and I am able to move to the next record and move back a record using a javascript function that I assign to a button. but i having trouble creating a function that will allow me to move to the last record and a button to move back to the first record.

<%@ page import="java.sql.*" %>
    <% Class.forName("com.mysql.jdbc.Driver");%>
    <HTML>
        <HEAD>
        </HEAD>
        <BODY>

            <FORM NAME="form1" METHOD="POST">
                <%
                    int current = 1;
                    if (request.getParameter("hidden") != null) {
                        current = Integer.parseInt(request.getParameter("hidden"));
                    }
                    Connection connection = DriverManager.getConnection("jdbc:mysql://localhost", "root", "symphonia23");
                    Statement statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
                    statement.executeQuery("use books");
                    ResultSet resultset = statement.executeQuery("select * from books");

                    if (current < 1) {
                        current = 1;
                    }
                    resultset.last();
                    int rows = resultset.getRow();
                    if (current <= rows) {
                        resultset.absolute(current);
                    }
                %>

                Code: <input type="text" id="code"  value="<%=resultset.getString("BookCode")%>"><br>
                Book Title: <input type="text" name="Code" value="<%=resultset.getString("BookTitle")%>"><br>
                Book Price: <input type="text" name="Code" value="<%=resultset.getString("BookPrice")%>"><br>
                <BR>

                <INPUT TYPE="HIDDEN" NAME="hidden" VALUE="<%= current%>">
                <INPUT TYPE="BUTTON" VALUE="Next" ONCLICK="moveNext()">
                <INPUT TYPE="BUTTON" VALUE="Prev" ONCLICK="movePrevious()">
                 <INPUT TYPE="BUTTON" VALUE="First" ONCLICK="movefirst()">

            </FORM>





                <SCRIPT LANGUAGE="JavaScript">
                    <!--
                function moveNext()
                    {
                        var counter = 0
                        counter = parseInt(document.form1.hidden.value) + 1
                        document.form1.hidden.value = counter
                        form1.submit()
                    }
                    function movePrevious()
                    {
                        var counter = 0
                        counter = parseInt(document.form1.hidden.value) - 1
                        document.form1.hidden.value = counter
                        form1.submit()
                    }

                     function movefirst()
                      {
                        var counter = 0
                        counter = parseInt(document.form1.hidden.value) 
                        document.form1.hidden.value = counter
                        form1.submit()
                    }

                    // --> 
            </SCRIPT>
        </BODY>
    </HTML>
user2827213
  • 37
  • 2
  • 8
  • Suggest you read [this](http://stackoverflow.com/a/3180202/136363) and move your code out of the JSP. There are lots of great tutorials on how to write proper apps. – t0mppa Nov 17 '13 at 21:26

2 Answers2

0

In the moveFirst() method you have bug in the line

counter = parseInt(document.form1.hidden.value) 

You have to remove it. It always rewrite counter value, instead counter must be equals 0

         function movefirst()
                  {
                    document.form1.hidden.value = 0
                    form1.submit()
                }

Using you functional, it is not possible(I think) to create moveLast() method, because you don't know last index of resultset. So you have to create new Parameter = 'count' and put there rows(count of records) value. For example:

int rows = resultset.getRow();
request.setAttribute("count", rows);

In this way your method moveLast() will looks like

                    function moveLast()
                  {
                    document.form1.hidden.value = parseInt(document.form1.count.value) 
                    form1.submit()
                }

sorry for my English :)

P.S. It is better to use lazy load of data . Try to introduce LIMIT into your query and create second query for search count of records

koropatva
  • 11
  • 2
0

Get the information about how big is your result set:

int total = resultSet.size();

Then use it in the function which should take you to the last result. This depends on how many results you're showing at a time. If you're displaying just one result each time, then just set your current to the last result:

function moveLast()
                {                                              
                    // array starts from 0  
                    document.form1.hidden.value = ${total} - 1;
                    form1.submit()
                }

Otherwise set it to the total - amount of records you show. For example if you're displaying 5 results in the textbox then use:

function moveLast()
                {                                  
                    if(${total}>5){            
                    document.form1.hidden.value = (${total} - 5);
                    }else{
                    document.form1.hidden.value = 0;
                    }
                    form1.submit()
                }

I hope that I understood your question correctly and my code doesn't have any errors.. I haven't worked with JSP for a while :)

Dropout
  • 13,653
  • 10
  • 56
  • 109