Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > help needed with this jsp code

Reply
Thread Tools

help needed with this jsp code

 
 
ros
Guest
Posts: n/a
 
      05-12-2007
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>

 
Reply With Quote
 
 
 
 
Richard Senior
Guest
Posts: n/a
 
      05-12-2007
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help needed on this 857W config. Repost to be clearer what the problemsare and the help needed sparticle Cisco 3 08-30-2007 07:47 PM
data, jsp and jsp tag problem, best practice needed quickcur@yahoo.com Java 5 10-05-2006 04:19 AM
[JSP] difference between jsp:forward and jsp:include alexjaquet@gmail.com Java 0 06-02-2006 01:21 PM
How to get the html output of a local JSP page from my Java code, or use a JSP as an HTML templates. DigitalUncle Java 0 11-02-2004 05:22 PM
Help needed with updating JSP Structs Bean using Tomcat Miles Davenport Java 4 02-06-2004 04:24 AM



Advertisments