help needed with this jsp code


R

ros

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>
 
Ad

Advertisements

R

Richard Senior

ros said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top