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