help needed with this jsp code

Discussion in 'Java' started by ros, May 12, 2007.

  1. ros

    ros Guest

    Hello,

    I am trying to modify this sample code from SAMS JSP in 21 days. This
    code shows the records from a database table. Presently it has 2
    buttons, one for showing the "next record" and the other for "previous
    record". On clicking the buttons Javascript functions are called.

    I have added 2 buttons to it so that I can move from any record to the
    first record and last record.

    But I am stuck and don't understand how to go about it. Would be
    really thankful if somebody guides me with this one. I have pasted the
    complete code below.

    Thanks in advance.
    ros




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

    <HTML>
    <HEAD>
    <TITLE>Navigating in a Database Table </TITLE>
    </HEAD>

    <BODY>
    <H1>Navigating in a Database Table </H1>
    <FORM NAME="form1" ACTION="ch17_04.jsp" METHOD="POST">

    <%
    int current = 1;
    if(request.getParameter("hidden") != null) {
    current =
    Integer.parseInt(request.getParameter("hidden"));
    }

    Connection connection =
    DriverManager.getConnection("jdbc:mysql://localhost/my-database",
    "root", "password");
    Statement statement =
    connection.createStatement( ResultSet.TYPE_SCROLL_INSENSITIVE,
    ResultSet.CONCUR_READ_ONLY);
    ResultSet resultset = statement.executeQuery("select *
    from customer");

    if(current < 1){
    current = 1;
    }

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

    <TABLE BORDER="1">
    <TR>
    <TH>Customer ID</TH>
    <TH>First Name</TH>
    <TH>Last Name</TH>
    <TH>Dat Of Birth</TH>
    <TH>Club Member</TH>
    </TR>
    <TR>
    <TD> <%= resultset.getString(1) %> </TD>
    <TD> <%= resultset.getString(2) %> </TD>
    <TD> <%= resultset.getString(3) %> </TD>
    <TD> <%= resultset.getString(4) %> </TD>
    <TD> <%= resultset.getString(5) %> </TD>
    </TR>
    </TABLE>
    <BR>
    <INPUT TYPE="HIDDEN" NAME="hidden" VALUE="<%= current %>">
    <INPUT TYPE="BUTTON" VALUE="First Record"
    ONCLICK="moveFirst()">
    <INPUT TYPE="BUTTON" VALUE="Next Record"
    ONCLICK="moveNext()">
    <INPUT TYPE="BUTTON" VALUE="Previous Record"
    ONCLICK="movePrevious()">
    <INPUT TYPE="BUTTON" VALUE="Last Record"
    ONCLICK="moveLast()">
    </FORM>

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

    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 moveLast()
    {
    var counter = 0
    counter = parseInt(document.form1.hidden.value) + 1
    document.form1.hidden.value = counter
    form1.submit()
    }
    // -->
    </SCRIPT>
    </BODY>
    </HTML>
     
    ros, May 12, 2007
    #1
    1. Advertising

  2. ros wrote:

    > I have added 2 buttons to it so that I can move from any record to the
    > first record and last record.


    ....

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


    I think if you set document.form1.hidden.value == 1 in the moveFirst()
    function and document.form1.hidden.value == Integer.MAX_VALUE in the
    moveLast() function, it should work. Not pretty but it is just an example.

    When the form is submitted with something in the hidden field, the value
    is copied to the variable "current". The value of "current", possibly
    incremented or decremented, is then embedded back into the value of the
    hidden field. This maintains the current record index between requests.

    If "current" is 1, the fragment of code above works out how many rows
    there are (which it doesn't need to do in this case) and then sets the
    absolute position in the resultset to position 1, i.e. the first record.

    If "current" is a number larger than the number of rows, the fragment of
    code shown above, will have already moved to the last record to
    determine the number of rows but will then not attempt to set an
    absolute position because Long.MAX_VALUE > rows. Obviously this breaks
    if the number of rows in the table > Integer.MAX_VALUE.

    --
    Regards,

    Richard
     
    Richard Senior, May 12, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Miles Davenport
    Replies:
    4
    Views:
    581
    Andrew Thompson
    Feb 6, 2004
  2. DigitalUncle
    Replies:
    0
    Views:
    991
    DigitalUncle
    Nov 2, 2004
  3. Replies:
    0
    Views:
    4,544
  4. sdavids

    JSP Help Needed

    sdavids, Jun 8, 2006, in forum: Java
    Replies:
    14
    Views:
    12,838
    vaibhav.pansambal
    Oct 30, 2012
  5. Replies:
    5
    Views:
    522
Loading...

Share This Page