Hey Guys im newby to Java! Help Needed! Please Help Needed!


This is How it Goes!

DAO.java

private BookRecordViews populateBookObject (ResultSet Results)
                throws ClassNotFoundException , SQLException
    {
            int BookID = Integer.parseInt(Results.getString("BookID"));
            String BookName = Results.getString("BookName");
            String DateOfArrival = Results.getString("DateOfArrival");
            String DateOfPublish = Results.getString("DateOfPublish");
            String AuthorName = Results.getString("AuthorName");
            int StudentID = Integer.parseInt(Results.getString("StudentID"));
            int LecturerID = Integer.parseInt(Results.getString("LecturerID"));
            String Available = Results.getString("Available");
            String Category = Results.getString("Category");

            return new BookViews(BookID, BookName, DateOfArrival, DateOfPublish, AuthorName, StudentID, LecturerID, Available, Category);

        } // This Method Populates the Book Records and gets the fields from BookViews.java


        public ArrayList<BookRecordViews> GetBookRecord()
                throws ClassNotFoundException , SQLException
    {
            try
            {
                ArrayList<BookRecordViews> BookRecords = new ArrayList<BookRecordViews>();
                Statement SQLStatement = getDatabaseConnection();
                String SQLQuery = "SELECT * FROM Book;";
                ResultSet BookResult = SQLStatement.executeQuery(SQLQuery);

                while(BookResult.next())
                {
                    BookRecords.add(populateBookObject(BookResult));
                }
                DestroySQLConnection();
                return BookRecords;
            }

            catch (ClassNotFoundException cnfe)
            {
                System.out.println(cnfe);
                throw cnfe;
            }

            catch (SQLException SQLE)
            {
                System.out.println(SQLE);
                throw SQLE;
            }

        } //This Is The Arraylist from where Records Concerning the Books are listed in Array.

BookRecordViews.java

package DataAccessObject;

/**
 *
 * @author Sagar Joshi
 */
public interface BookRecordViews {

    public int getBookID();
    public void setBookID(int BookID);
    public String getBookName();
    public void setBookName(String BookName);
    public String getDateOfArrival();
    public void setDateOfArrival(String DateOfArrival);
    public String getDateOfPublish();
    public void setDateOfPublish(String DateOfPublish);
    public String getAuthorName();
    public void setAuthorName(String AuthorName);
    public int getStudentID();
    public void setStudentID(int StudentID);
    public int getLecturerID();
    public void setLecturerID(int LecturerID);
    public String getAvailability();
    public void setAvailability(String Available);
    public String getCategory();
    public void setCategory(String Category);

} // Variables Are Listed.

BookViews.java

package DataAccessObject;

/**
 *
 * @author Sagar Joshi
 */
public class BookViews implements BookRecordViews {

    private int BookID;
    private String BookName;
    private String DateOfArrival;
    private String DateOfPublish;
    private String AuthorName;
    private int StudentID;
    private int LecturerID;
    private String Available;
    private String Category;

    public BookViews()
    {
        
    }

    public BookViews(int BookID , String BookName , String DateOfArrival , String DateOfPublish , String AuthorName , int StudentID , int LecturerID
            , String Available , String Category)
    {
        this.BookID = BookID;
        this.BookName = BookName;
        this.DateOfArrival = DateOfArrival;
        this.DateOfPublish = DateOfPublish;
        this.AuthorName = AuthorName;
        this.StudentID = StudentID;
        this.LecturerID = LecturerID;
        this.Available = Available;
        this.Category = Category;

    }

    public int getBookID()
    {
        return BookID;
    }

    public void setBookID(int BookID)
    {
        this.BookID = BookID;
    }

    public String getBookName()
    {
        return BookName;
    }

    public void setBookName(String BookName)
    {
        this.BookName = BookName;
    }

    public String getDateOfArrival()
    {
        return DateOfArrival;
    }

    public void setDateOfArrival(String DateOfArrival)
    {
        this.DateOfArrival = DateOfArrival;
    }

    public String getDateOfPublish()
    {
        return DateOfPublish;
    }

    public void setDateOfPublish(String DateOfPublish)
    {
        this.DateOfPublish = DateOfPublish;
    }

    public String getAuthorName()
    {
        return AuthorName;
    }

    public void setAuthorName(String AuthorName)
    {
        this.AuthorName = AuthorName;
    }

    public int getStudentID()
    {
        return StudentID;
    }

    public void setStudentID(int StudentID)
    {
        this.StudentID = StudentID;
    }

    public int getLecturerID()
    {
        return LecturerID;
    }

    public void setLecturerID(int LecturerID)
    {
        this.LecturerID = LecturerID;
    }

    public String getAvailability()
    {
        return Available;
    }

    public void setAvailability(String Available)
    {
        this.Available = Available;
    }

    public String getCategory()
    {
        return Category;
    }

    public void setCategory(String Category)
    {
        this.Category = Category;
    }



} // Contains The Getter and Setter Method.

ViewBooks.jsp

<%
// The Importance of Having JSPs is to be Present Contents on the Web Pages. They Are Known very Famous for
// displaying Web Contents. Having Database Connections and performing SQL Manipulations on the JSPs can make
// JSPs congested with Codes that are to be actually used in Servlets. A Further Explanation on this Point
// takes us deeper in to the DAO (Data Access Object) Design Pattern. As You Can See the ViewBooks.jsp
// has the DAO File Imported and the Method GetBookRecord() has been called in the JSP rather than
// establishing the SQL Connection, performing the SQL Statement and then projecting the Data.

OshwalDAO dao = DAO.getDAOInterface();

ArrayList GetBookRecord = dao.GetBookRecord();

//String StudentRecord = dao.GetStudentRecordForDelete();


%>
<table align="center" border="1" frame="2" id="table" width="1020">
    <tr><td><b>Book ID</b></td><td><b>Book Name</b></td><td><b>Date Of Arrival</b></td><td><b>Date Of Publish</b></td><td><b>Author Name</b></td><td><b>Student ID</b></td>
        <td><b>Lecturer ID</b></td><td><b>Available</b></td><td><b>Category</b></td></tr>

    <%      BookRecordViews Book;
            for(Iterator i = GetBookRecord.iterator(); i.hasNext();) {

                Book = (BookRecordViews)i.next();
    %>

    <tr>
        <td><%=Book.getBookID()%></td>
        <td><%=Book.getBookName() %></td>
        <td><%=Book.getDateOfArrival() %></td>
        <td><%=Book.getDateOfPublish() %></td>
        <td><%=Book.getAuthorName() %></td>
        <td><%=Book.getStudentID() %></td>
        <td><%=Book.getLecturerID() %></td>
        <td><%=Book.getAvailability()%></td>
        <td><%=Book.getCategory()%></td>
        <td><a href="UpdateBook.jsp?BookName=<%=Book.getBookName()%>">Update</a></td>
    </tr>

    <% } %>
</table>
    </body>
</html>

//In The JSP it is just getting all the records. Now How should i modify such that it displays one record only and then i click on next and it displays the next record. I know i have used an Array but dnt knw what to implement. PLEASE HELPPPPPPPPPPPPPPPP!

Since you call the GetBookRecord in the jsp then this is the easy, but not so efficient way:

In the jsp have an int variable let's say it counter. At first display the 1st record (counter=0) GetBookRecord.get(counter); Have NEXT, BACK buttons and put the counter value in a hidden field.

Once you click any of the buttons submit to the same page. Then determine which button was clicked. If NEXT was clicked, take the counter from the request, increase its value by one and display that BookView from the list. If BACK was clicked, decrease the counter by one and do the same. If none of the buttons were clicked then that means that you went to page from somewhere else so display the (counter=0)

You can put hidden fields and when you click the button update the field with javascript in order to determine which button was clicked.

<form action="">

-- buttons

<input type="hidden" name="counter" value="<%=counter%>" />
<input type="hidden" name="butPressed" id="butPressed" value="" />
</form>

When you click the buttons, change the value of the butPressed field to whatever you want depending on which button was clicked and then submit the form
Take from the request the butPressed and the counter. If the butPressed is not null, it means that one of the buttons was clicked. Determine which one it was and follow the instructions

int counter = 0;
if (NEXT) {
  counter = <counter from the request> + 1;
} else if (BACK) {
  counter = <counter from the request> - 1;
}

Book = GetBookRecord.get(counter);

<form action="">

display Book

-- buttons

<input type="hidden" name="counter" value="<%=counter%>" />
<input type="hidden" name="butPressed" id="butPressed" value="" />
</form>

You could use links instead of buttons if you want:

<a href="ViewBooks.jsp?butPressed=back&counter=<%=counter%>" >
BACK
</a>

<a href="ViewBooks.jsp?butPressed=next&counter=<%=counter%>" >
NEXT
</a>

Hey JavaAddict! Thanks For The Reply! I needed to ask that already the array is displayin all the record so in cannot display a single record yes! Now how would i first put the counter in the GetBookRecord because when i tried it said that i requires an object and it has found int. The GetBook Record is an array so it will display all record. dont know how to implement the counter.

Hey This is The Probz im facing

<%-- 
    Document   : ViewBooks
    Created on : Jul 21, 2011, 11:08:26 PM
    Author     : Sagar Joshi
--%>

<%@page import="LibraryCart.Books"%>
<%@page import="java.awt.print.Book"%>
<%@page import="antlr.debug.Event"%>
<%@page import="java.util.Iterator"%>
<%@page import="java.util.ArrayList"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
<%@ page import= "DataAccessObject.*, java.util.*" %>

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link rel="stylesheet" href="SiteCSS/style.css" type="text/css" />
        <title>VIEW BOOK RECORDS</title>
    </head>
    <body background="SiteImages/bg.jpg">
        <%
            //String UserSession = (String)session.getAttribute("Role");

            //if (UserSession == null ? "Role" != null: !UserSession.equals("Librarian"))
                //{
                    //response.sendRedirect("Error.jsp");
                //}
            //else if (UserSession == null)
                //{
                    //response.sendRedirect("Error.jsp");
                //}

        %>
    <center>
<ul id="nav">
        <li><a href="AdminMenu.jsp">BACK TO ADMIN'S AREA</a></li>
	<li><a href="#">ADMIN'S PORTAL</a>
		<ul>
			<li><a href="#">VIEW RECORDS FOR:</a>
                            <ul>
                                <li><a href="ViewStudent.jsp">STUDENT</a>
                                    <a href="ViewLecturer.jsp">LECTURER</a>
                                    <a href="ViewBooks.jsp">BOOKS</a></li>
                            </ul>
                        </li>

                        <li><a href="#">UPDATE RECORDS FOR:</a>
                            <ul>
                                <li><a href="UpdateStudent.jsp">STUDENT</a>
                                    <a href="UpdateLecturer.jsp">LECTURER</a>
                                    <a href="UpdateBooks.jsp">BOOKS</a></li>
                            </ul></li>

                        <li><a href="#">DELETE RECORDS FOR:</a>
                            <ul>
                                <li><a href="DeleteStudent.jsp">STUDENT</a>
                                    <a href="DeleteLecturer.jsp">LECTURER</a>
                                    <a href="DeleteBook.jsp">BOOKS</a></li>
                            </ul></li>

                        <li><a href="#">ADD RECORDS FOR:</a>
                            <ul>
                                <li><a href="AddBook.jsp">BOOKS</a>
                                    <a href="AddJournal.jsp">JOURNALS</a>
                                    <a href="AddMagazine.jsp">MAGAZINES</a></li>
                            </ul></li>
		</ul>
	</li>
	<li><a href="SessionDestroyed.jsp">LOGOUT FROM SYSTEM</a>
	</li>
</ul>
    </center>
        <script src="SiteJavaScript/jquery.min.js" type="text/javascript" charset="utf-8"></script>
        <script src="SiteJavaScript/jquery.effects.core.js" type="text/javascript"></script>
        <script type="text/javascript" src="SiteJavaScript/scripts.js"></script>
<br/>
<br/>
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
<br/>
<h1 id="heading1">
    YOU ARE CURRENTLY VIEWING THE BOOK RECORDS
</h1>

&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
<br>
<br>
<%
// The Importance of Having JSPs is to be Present Contents on the Web Pages. They Are Known very Famous for
// displaying Web Contents. Having Database Connections and performing SQL Manipulations on the JSPs can make
// JSPs congested with Codes that are to be actually used in Servlets. A Further Explanation on this Point
// takes us deeper in to the DAO (Data Access Object) Design Pattern. As You Can See the ViewBooks.jsp
// has the DAO File Imported and the Method GetBookRecord() has been called in the JSP rather than
// establishing the SQL Connection, performing the SQL Statement and then projecting the Data.

OshwalDAO dao = DAO.getDAOInterface();

ArrayList GetBookRecord = dao.GetBookRecord();

//int Counter = 0;

//String StudentRecord = dao.GetStudentRecordForDelete();




%>
<table align="center" border="1" frame="2" id="table" width="1020">
    <tr><td><b>Book ID</b></td><td><b>Book Name</b></td><td><b>Date Of Arrival</b></td><td><b>Date Of Publish</b></td><td><b>Author Name</b></td><td><b>Student ID</b></td>
        <td><b>Lecturer ID</b></td><td><b>Available</b></td><td><b>Category</b></td></tr>

    <%
            int Counter = 0;
            //BookRecordViews Book;
            //for(Iterator i = GetBookRecord.iterator(); i.hasNext();) {



            Books = GetBookRecord.get(Counter); ** It is giving me probz here.
    %>

    <tr>
        <td><%=Book.getBookID()%></td>
        <td><%=Book.getBookName() %></td>
        <td><%=Book.getDateOfArrival() %></td>
        <td><%=Book.getDateOfPublish() %></td>
        <td><%=Book.getAuthorName() %></td>
        <td><%=Book.getStudentID() %></td>
        <td><%=Book.getLecturerID() %></td>
        <td><%=Book.getAvailability()%></td>
        <td><%=Book.getCategory()%></td>
        <td><a href="UpdateBook.jsp?BookName=<%=Book.getBookName()%>">Update</a></td>
        <a href="ViewBooks.jsp?butpressed=back&Counter=<%=Counter%>">Next Record</a>
        <a href="ViewBooks.jsp?butpressed=next&Counter=<%=Counter%>">Previous Record</a>

    <% //} %>
</table>
    </body>
</html>

Even if it is an array are u usre that using counter will force the program to show only one record. Becoz im new to java, im finding it very difficult to handle complex errors like these.

Thank You Indeed!

It is a given that you will not loop this time.

OK! Understood about that! Last Questions having the problem in this section of the code:

Books = GetBookRecord.get(Counter); ** It is giving me probz here.

int counter = 0;
if (NEXT) {
  counter = <counter from the request> + 1;
} else if (BACK) {
  counter = <counter from the request> - 1;
} // Also Having issues here.

Help me on this section please.

Thanks :-)

Ok there is progress! To Display one record all i had to do is to remove the while in the following:

public ArrayList<BookRecordViews> GetBookRecord()
                throws ClassNotFoundException , SQLException
    {
            try
            {
                ArrayList<BookRecordViews> BookRecords = new ArrayList<BookRecordViews>();
                Statement SQLStatement = getDatabaseConnection();
                String SQLQuery = "SELECT * FROM Book;";
                ResultSet BookResult = SQLStatement.executeQuery(SQLQuery);
 
                [B][I]while[/I][/B](BookResult.next()) // That is what i did
                {
                    BookRecords.add(populateBookObject(BookResult));
                }
                DestroySQLConnection();
                return BookRecords;
            }
 
            catch (ClassNotFoundException cnfe)
            {
                System.out.println(cnfe);
                throw cnfe;
            }
 
            catch (SQLException SQLE)
            {
                System.out.println(SQLE);
                throw SQLE;
            }
 
        }

Now the JSP Displays the First Record only. Only Help is how to move next record and that is it/

Help Please Help Guys! Im nt understanding how to implement the logic! Please Help! Thanks

The method must return a list with all the elements. And at the jsp you will display only one. The one selected. Now even if you return the first element how will you get the ith element and how will you know that would be the value of i?


The solution has already been given in previous posts. I will try to give you a more simpler one.


You will have a java variable counter which will tell you which element from the list you will display:

int counter = 0;

// more code here

Book = GetBookRecord.get(counter);
//display only Book. One element.

The counter's value will be taken from the request. In order to do that the links will redirect to the same page. They will have one parameter. The value of the counter that needs to be displayed

<a href="ViewBooks.jsp?counter=<%=counter-1%>" >
BACK
</a>

<a href="ViewBooks.jsp?counter=<%=counter+1%>" >
NEXT
</a>

When those links are pressed you will go to the same page, but this time the counter would be in the request and it will not have the same value. It will be increased or decrased by one so it will point to the next or previous element.

So:
At the begining of the page you will always get the "counter" parameter:
request.getParameter("counter")

If it is null, it means that this is the first time you come to this page so you must display the first element, so you will give to the java variable value 0.

If it is not null, then it means that you came to this page from one of the links, so the value of that parameter holds the element you need to display.
So if the request.getParamter("counter") is not null, then you will turn it into an int and put it in the java variable counter.

So in the end the java variable counter will have the right value. 0 or the value from the request and you will display that element.

Book = GetBookRecord.get(counter);

And at the end this time the links will have as value at the counter parameter (counter+1) or (counter-1), so the next time you click them you will send t0 the page the "next"(+1) or the "previous"(-1) element to display

You will also need to add some checks if the counter is negative. The first time the pgae loads and counter is zero you must display the "BACK" link. And if the counter points to the last element you shouldn't dispaly the "NEXT" link.

Hello JavaAddict i want to apologize for the late reply! as i very much appreciate ur efforts in helping me out!

This is the Code i have and the proble it shows is required Books.BookrecordViews found Books.Object

<%@page import="LibraryCart.Books"%>
<%@page import="java.awt.print.Book"%>
<%@page import="antlr.debug.Event"%>
<%@page import="java.util.Iterator"%>
<%@page import="java.util.ArrayList"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
<%@ page import= "DataAccessObject.*, java.util.*" %>
 
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link rel="stylesheet" href="SiteCSS/style.css" type="text/css" />
        <title>VIEW BOOK RECORDS</title>
    </head>
    <body background="SiteImages/bg.jpg">
        <%
            //String UserSession = (String)session.getAttribute("Role");
 
            //if (UserSession == null ? "Role" != null: !UserSession.equals("Librarian"))
                //{
                    //response.sendRedirect("Error.jsp");
                //}
            //else if (UserSession == null)
                //{
                    //response.sendRedirect("Error.jsp");
                //}
 
        %>
    <center>
<ul id="nav">
        <li><a href="AdminMenu.jsp">BACK TO ADMIN'S AREA</a></li>
	<li><a href="#">ADMIN'S PORTAL</a>
		<ul>
			<li><a href="#">VIEW RECORDS FOR:</a>
                            <ul>
                                <li><a href="ViewStudent.jsp">STUDENT</a>
                                    <a href="ViewLecturer.jsp">LECTURER</a>
                                    <a href="ViewBooks.jsp">BOOKS</a></li>
                            </ul>
                        </li>
 
                        <li><a href="#">UPDATE RECORDS FOR:</a>
                            <ul>
                                <li><a href="UpdateStudent.jsp">STUDENT</a>
                                    <a href="UpdateLecturer.jsp">LECTURER</a>
                                    <a href="UpdateBooks.jsp">BOOKS</a></li>
                            </ul></li>
 
                        <li><a href="#">DELETE RECORDS FOR:</a>
                            <ul>
                                <li><a href="DeleteStudent.jsp">STUDENT</a>
                                    <a href="DeleteLecturer.jsp">LECTURER</a>
                                    <a href="DeleteBook.jsp">BOOKS</a></li>
                            </ul></li>
 
                        <li><a href="#">ADD RECORDS FOR:</a>
                            <ul>
                                <li><a href="AddBook.jsp">BOOKS</a>
                                    <a href="AddJournal.jsp">JOURNALS</a>
                                    <a href="AddMagazine.jsp">MAGAZINES</a></li>
                            </ul></li>
		</ul>
	</li>
	<li><a href="SessionDestroyed.jsp">LOGOUT FROM SYSTEM</a>
	</li>
</ul>
    </center>
        <script src="SiteJavaScript/jquery.min.js" type="text/javascript" charset="utf-8"></script>
        <script src="SiteJavaScript/jquery.effects.core.js" type="text/javascript"></script>
        <script type="text/javascript" src="SiteJavaScript/scripts.js"></script>
<br/>
<br/>
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
<br/>
<h1 id="heading1">
    YOU ARE CURRENTLY VIEWING THE BOOK RECORDS
</h1>
 
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
<br>
<br>
<%
// The Importance of Having JSPs is to be Present Contents on the Web Pages. They Are Known very Famous for
// displaying Web Contents. Having Database Connections and performing SQL Manipulations on the JSPs can make
// JSPs congested with Codes that are to be actually used in Servlets. A Further Explanation on this Point
// takes us deeper in to the DAO (Data Access Object) Design Pattern. As You Can See the ViewBooks.jsp
// has the DAO File Imported and the Method GetBookRecord() has been called in the JSP rather than
// establishing the SQL Connection, performing the SQL Statement and then projecting the Data.
 
OshwalDAO dao = DAO.getDAOInterface();
 
ArrayList GetBookRecord = dao.GetBookRecord();
 
//int Counter = 0;
 
//String StudentRecord = dao.GetStudentRecordForDelete();
 
 
 
 
%>
<table align="center" border="1" frame="2" id="table" width="1020">
    <tr><td><b>Book ID</b></td><td><b>Book Name</b></td><td><b>Date Of Arrival</b></td><td><b>Date Of Publish</b></td><td><b>Author Name</b></td><td><b>Student ID</b></td>
        <td><b>Lecturer ID</b></td><td><b>Available</b></td><td><b>Category</b></td></tr>
 
    <%
            int Counter = 0;
            //BookRecordViews Book;
            //for(Iterator i = GetBookRecord.iterator(); i.hasNext();) {
 
 
 
            Books = GetBookRecord.get(Counter); ** This is where the above problem stated appears! So im being stuck here so much finding it difficult to understand the logic.
    %>
 
    <tr>
        <td><%=Book.getBookID()%></td>
        <td><%=Book.getBookName() %></td>
        <td><%=Book.getDateOfArrival() %></td>
        <td><%=Book.getDateOfPublish() %></td>
        <td><%=Book.getAuthorName() %></td>
        <td><%=Book.getStudentID() %></td>
        <td><%=Book.getLecturerID() %></td>
        <td><%=Book.getAvailability()%></td>
        <td><%=Book.getCategory()%></td>
        <td><a href="UpdateBook.jsp?BookName=<%=Book.getBookName()%>">Update</a></td>
        <a href="ViewBooks.jsp?butpressed=back&Counter=<%=Counter%>">Next Record</a>
        <a href="ViewBooks.jsp?butpressed=next&Counter=<%=Counter%>">Previous Record</a>
 
    <% //} %>
</table>
    </body>
</html>

When you were using the Iterator to get each element you were doing this: Book = (BookRecordViews)i.next(); With the way you declare the ArrayList you have it contain Objects. So you need to cast them to BookRecordViews when you use the get method like above.

int Counter = 0;
            //String Books = "";
            BookRecordViews Book;
            //Book = GetBookRecord.get(Counter);
            for(Iterator i = GetBookRecord.iterator(); i.hasNext();) {
            Book = (BookRecordViews)GetBookRecord.get(Counter);

Hello JavaAddict! First Of All Wanna Thank u very much for the Help and Assistance u have give throughout the tutorial! I have made quite a few progress and have made it to some point! Ok the code above wanna ask for help if u would see it and ask if there are any mistakes!

When i run the jsp i duplicates it continuously and hangs the browser! i am sure i am close to the solution!

Ur kind assistance is of great appreciation!

Thank u alot!

int Counter = 0;
            BookRecordViews Book;
            Book = (BookRecordViews)GetBookRecord.get(Counter);


            <a href="ViewBooks.jsp?Counter=<%=Counter+1%>">NEXT RECORD</a>
            <a href="ViewBooks.jsp"?Counter=<%=Counter-1%>">PREVIOUS RECORD</a>

Hello! I have made it this far! But only issue! It is displaying the first record. but when i click on the next record it does not go! I am very close to finish!

Where the problem is? Just assist me so i can wrap up this section!

Thanking u indeed!

And correction! with the use of iterator it gets all the records together! starting from the first record to the last record!

Dear JavaAddict Please assist! i knw i am close to the solution! Just see the previous post! i knw i have made it close or if u cn be able to suggest another solution for me which is efficient and easy please help me! u cn also email me if u wish so!

Thanking u in advance.

all members anyone with any idea on what the code is missing? please help

You will display only one element, so you don't need a loop. Just display only one element from the list. Which one will be determined from the links "NEXT", "BACK".
They will submit to the same page you will get what is in the request (the index(counter) of the element in the list ) and display it.

How you will write those links and how you will get the index from the request it has been described.

If the counter is not in the request (if the request.getParameter returns null) you will display the first element and use that as starting point.
Else just convert what you get from the request to an int and use that as index.

String count = request.getParamer("counter");
int counter = ...

// code that has been described

<a href="ViewBooks.jsp?counter=<%=counter-1%>" >
BACK
</a>

<a href="ViewBooks.jsp?counter=<%=counter+1%>" >
NEXT
</a>

if "count" is null
you will set the int java variable counter to 0,
else you will set it to what is inside the "count" (from the request)

So if next was clicked you have the previous counter+1, if back was clicked you have the previous counter-1.
Get the element, display it, and the rest of the code displays the links with the new values calculated to point to the next, previous element.

Additional description has been given in one of my previous posts.
Also I don't remember if it is against the rules or not, but it is rude to keep sending PMs asking for the code, especially when it has been given to the public forum.

Still Fail to understand why it does not work! i DID EVERYTHING U SHOWED ME IN THE PREVIOUS POSTS AND THE OTHER POSTS WHICH SHOWED ME A SIMPLIFIED VERSION.

Strinf count = request.getParameter("Counter");
            int Counter = 0;
            BookRecordViews Book;
            Book = (BookRecordViews)GetBookRecord.get(Counter);
 
            <table>
            <td><%Book.getBookID()%></td>
            <td><%Book.getBookName()%></td>
 
            <a href="ViewBooks.jsp?Counter=<%=Counter+1%>">NEXT RECORD</a>
            <a href="ViewBooks.jsp"?Counter=<%=Counter-1%>">PREVIOUS RECORD</a>

Everything is in order yet it shows the in the address bar that Counter = 1 but does not display the next record. It is okay! If u cn show me another way when u get some time u cn show me!

Sorry for the PM. Will not be sending them anymore. If u cn show me a full code with another way of achieving the same it is okay! u cn show me the code when u get time.

Thanking u very much for all the help and efforts that have been throughout the thread.

Best Regards and Appreciation

sagar_danceking

it shows the in the address bar that Counter = 1 but does not display the next record.

It doesn't display the next record because you don't make it display it, nor you read the next record. Look at the code you have written. You always set the java variable Counter to 0. You never change its value, so of course it always displays the first record. Understand what you are writing:

int Counter = 0;
BookRecordViews Book = (BookRecordViews)GetBookRecord.get(Counter);

Counter is always 0. You never change its value.

Also the links are wrong. You haven't written correctly one of them.
When you submit to the same page, you pass the Counter(address bar) for a reason, but you don't use it when you take it from the request.

Yes i have followed all your instructions dear friend yet it is still the same issue! cannot show the record.

String count = request.getParameter("Counter");
      int Counter = 0;
      BookRecordViews Book = (BookRecordViews)GetBookRecord.get(Counter);

      <a href="ViewBooks.jsp?Counter=<%=Counter+1%>">View Next Record</a>
      <a href="ViewBooks.jsp?Counter=<%=Counter-1%>">View Previous Record</a>

All this code is done in accordance to your previous posts! Still NO LUCK! Or cud there be a problem in the ArrayList for the java file?

Here is the java code once again! Im showing everything that i can:

DAO.java {This is the DATA ACCESS OBJECT Pattern file}

private BookRecordViews populateBookObject (ResultSet Results)
                throws ClassNotFoundException , SQLException
    {
            int BookID = Integer.parseInt(Results.getString("BookID"));
            String BookName = Results.getString("BookName");
            String DateOfArrival = Results.getString("DateOfArrival");
            String DateOfPublish = Results.getString("DateOfPublish");
            String AuthorName = Results.getString("AuthorName");
            int StudentID = Integer.parseInt(Results.getString("StudentID"));
            int LecturerID = Integer.parseInt(Results.getString("LecturerID"));
            String Available = Results.getString("Available");
            String Category = Results.getString("Category");
 
            return new BookViews(BookID, BookName, DateOfArrival, DateOfPublish, AuthorName, StudentID, LecturerID, Available, Category);
 
        } // This Method Populates the Book Records and gets the fields from BookViews.java
 
 
        public ArrayList<BookRecordViews> GetBookRecord()
                throws ClassNotFoundException , SQLException
    {
            try
            {
                ArrayList<BookRecordViews> BookRecords = new ArrayList<BookRecordViews>();
                Statement SQLStatement = getDatabaseConnection();
                String SQLQuery = "SELECT * FROM Book;";
                ResultSet BookResult = SQLStatement.executeQuery(SQLQuery);
 
                while(BookResult.next())
                {
                    BookRecords.add(populateBookObject(BookResult));
                }
                DestroySQLConnection();
                return BookRecords;
            }
 
            catch (ClassNotFoundException cnfe)
            {
                System.out.println(cnfe);
                throw cnfe;
            }
 
            catch (SQLException SQLE)
            {
                System.out.println(SQLE);
                throw SQLE;
            }
 
        } //This Is The Arraylist from where Records Concerning the Books are listed in Array.

BookRecordViews with the getter and setter methods.

package DataAccessObject;
 
/**
 *
 * @author Sagar Joshi
 */
public interface BookRecordViews {
 
    public int getBookID();
    public void setBookID(int BookID);
    public String getBookName();
    public void setBookName(String BookName);
    public String getDateOfArrival();
    public void setDateOfArrival(String DateOfArrival);
    public String getDateOfPublish();
    public void setDateOfPublish(String DateOfPublish);
    public String getAuthorName();
    public void setAuthorName(String AuthorName);
    public int getStudentID();
    public void setStudentID(int StudentID);
    public int getLecturerID();
    public void setLecturerID(int LecturerID);
    public String getAvailability();
    public void setAvailability(String Available);
    public String getCategory();
    public void setCategory(String Category);
 
} // Variables Are Listed.

BookViews.java (implements the BookRecordViews)

package DataAccessObject;
 
/**
 *
 * @author Sagar Joshi
 */
public class BookViews implements BookRecordViews {
 
    private int BookID;
    private String BookName;
    private String DateOfArrival;
    private String DateOfPublish;
    private String AuthorName;
    private int StudentID;
    private int LecturerID;
    private String Available;
    private String Category;
 
    public BookViews()
    {
 
    }
 
    public BookViews(int BookID , String BookName , String DateOfArrival , String DateOfPublish , String AuthorName , int StudentID , int LecturerID
            , String Available , String Category)
    {
        this.BookID = BookID;
        this.BookName = BookName;
        this.DateOfArrival = DateOfArrival;
        this.DateOfPublish = DateOfPublish;
        this.AuthorName = AuthorName;
        this.StudentID = StudentID;
        this.LecturerID = LecturerID;
        this.Available = Available;
        this.Category = Category;
 
    }
 
    public int getBookID()
    {
        return BookID;
    }
 
    public void setBookID(int BookID)
    {
        this.BookID = BookID;
    }
 
    public String getBookName()
    {
        return BookName;
    }
 
    public void setBookName(String BookName)
    {
        this.BookName = BookName;
    }
 
    public String getDateOfArrival()
    {
        return DateOfArrival;
    }
 
    public void setDateOfArrival(String DateOfArrival)
    {
        this.DateOfArrival = DateOfArrival;
    }
 
    public String getDateOfPublish()
    {
        return DateOfPublish;
    }
 
    public void setDateOfPublish(String DateOfPublish)
    {
        this.DateOfPublish = DateOfPublish;
    }
 
    public String getAuthorName()
    {
        return AuthorName;
    }
 
    public void setAuthorName(String AuthorName)
    {
        this.AuthorName = AuthorName;
    }
 
    public int getStudentID()
    {
        return StudentID;
    }
 
    public void setStudentID(int StudentID)
    {
        this.StudentID = StudentID;
    }
 
    public int getLecturerID()
    {
        return LecturerID;
    }
 
    public void setLecturerID(int LecturerID)
    {
        this.LecturerID = LecturerID;
    }
 
    public String getAvailability()
    {
        return Available;
    }
 
    public void setAvailability(String Available)
    {
        this.Available = Available;
    }
 
    public String getCategory()
    {
        return Category;
    }
 
    public void setCategory(String Category)
    {
        this.Category = Category;
    }
 
 
 
} // Contains The Getter and Setter Method.

The JSP once again

OshwalDAO dao = DAO.getDAOInterface();
 
ArrayList GetBookRecord = dao.GetBookRecord();
 
//String StudentRecord = dao.GetStudentRecordForDelete();
 
 
%>
<table align="center" border="1" frame="2" id="table" width="1020">
    <tr><td><b>Book ID</b></td><td><b>Book Name</b></td><td><b>Date Of Arrival</b></td><td><b>Date Of Publish</b></td><td><b>Author Name</b></td><td><b>Student ID</b></td>
        <td><b>Lecturer ID</b></td><td><b>Available</b></td><td><b>Category</b></td></tr>
 
    <%      
        String Count = request.getParameter("Counter");
        int Counter= 0;
        BookRecordViews Book = (BookRecordViews)GetBookRecord.get(Counter);
    %>
 
    <tr>
        <td><%=Book.getBookID()%></td>
        <td><%=Book.getBookName() %></td>
        <td><%=Book.getDateOfArrival() %></td>
        <td><%=Book.getDateOfPublish() %></td>
        <td><%=Book.getAuthorName() %></td>
        <td><%=Book.getStudentID() %></td>
        <td><%=Book.getLecturerID() %></td>
        <td><%=Book.getAvailability()%></td>
        <td><%=Book.getCategory()%></td>
        <td><a href="ViewBooks.jsp?Counter=<%Counter+1%>">Next Record</a></td>
        <td><a href="ViewBooks.jsp?Counter=<%Counter-1%>">Previous Record</a></td>
    </tr>
 
    <% //} %>
</table>
    </body>
</html>

Everything i did yet no result!

No you didn't follow my instructions. In my exact previous post I stated the error and told you what you need to use. Descriptions of the logic has been given in another previous post.
The links pass at the request the increased/decreased value of the counter, which you don't use it, as stated many times.

LIKE THIS!

String Count = request.getParameter("Counter");
      int Counter = Integer.parseInt(Count);

      Book = (BookRecordViews)GetBookRecord.get(Counter);

      <a href="ViewBooks.jsp?Counter=<%=Counter+1%>">NEXT RECORD</a>
      <a href="ViewBooks.jsp?Counter=<%=Counter-1%>">PREVIOUS RECORD</a>

My Friend!!!!!!!!!!!! WE DID IT! WE DID IT! THANKS ONCE AGAIN IT IS WORKING NOW! Although it gives me arrayindexoutofboundexception when there is no next record but it works here is the final code:

<%
String Count = request.getParameter("Counter");
int Counter = 0;
if(request.getParameter("Counter") != null) {
  Counter = Integer.parseInt(request.getParameter("Counter"));
}
BorrowerRecordViews Borrower = (BorrowerRecordViews)GetBorrowersRecord.get(Counter);            
%>

<a href="ViewStudent.jsp?Counter=<%=Counter+1%>">VIEW NEXT RECORD</a>
<a href="ViewStudent.jsp?Counter=<%=Counter-1%>">VIEW PREVIOUS RECORD</a>

Thanks once again friend! You have been a big assistance throughout this thread! Could not have done without you! I want to post another thread! Would you like to participate in it! If ur interested!

Best regards

sagar!

In order to avoid the exception use if statements. If the counter+1 exceeds the size of the list don't display the NEXT.
The same for PREVIOUS. If counter-1 is negative don't display the PREVIOUS

If u cud give me an example with the current code that i have displayed in my previous post it shall be highly appreciated!

THANK U ONCE AGAIN FRIEND!

<%
if () {
%>
// next link
<%
}
%>

<%
if () {
%>
// previous link
<%
}
%>

Like this!

<%
String Count = request.getParameter("Counter");
int Counter = 0;
if(request.getParameter("Counter") != null) {
  Counter = Integer.parseInt(request.getParameter("Counter"));
}
BorrowerRecordViews Borrower = (BorrowerRecordViews)GetBorrowersRecord.get(Counter);            
%>
<% <%if (Counter>0) {%> 
<a href="ViewStudent.jsp?Counter=<%=Counter+1%>">VIEW NEXT RECORD</a>

<%}%>

Almost. You need to get the logic right. By now you must know that the index that you pass at a list must be between 0 and list.size-1
So you must make sure that when you display the links, what you send is between that range.

Hy Friend!

After lots of thinking i arrived to this Status!

<%
String Count = request.getParameter("Counter");
int Counter = 0;
if(request.getParameter("Counter") != null) {
  Counter = Integer.parseInt(request.getParameter("Counter"));
}
BorrowerRecordViews Borrower = (BorrowerRecordViews)GetBorrowersRecord.get(Counter);            
%>
<% <%if (Counter>(BorrowerRecordViews)GetBookRecord.get(Counter)) {%> 
<a href="ViewStudent.jsp?Counter=<%=Counter+1%>">VIEW NEXT RECORD</a>
 
<%}%>

Do You See any problems?

IF YES THEN PLEASE RECTIFY FOR ME!

THANK U FRIEND!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.