Difference between revisions of "Java-Business-Application/C2/Creating-and-viewing-inventories/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
(Created page with ''''Title of script''': Creating and viewing inventories '''Author: arya ''' '''Keywords: video, tutorial,fetch book details, fetch borrowed book details''' {| style="border-…')
 
Line 4: Line 4:
  
 
'''Keywords: video, tutorial,fetch book details, fetch borrowed book details'''
 
'''Keywords: video, tutorial,fetch book details, fetch borrowed book details'''
 
 
  
 
{| style="border-spacing:0;"
 
{| style="border-spacing:0;"

Revision as of 15:27, 25 June 2014

Title of script: Creating and viewing inventories

Author: arya

Keywords: video, tutorial,fetch book details, fetch borrowed book details

Visual Cue
Narration
Slide 1 Welcome to the spoken-tutorial on Creating and viewing inventories.
Slide 2 In this tutorial we will learn to:
  • Modify the login page to redirect to admin page
  • Fetch all the book details
  • Fetch borrowed book details
  • To display the books borrowed by logged in user


Slide 3

Software Requirements

Here we are using
  • Ubuntu Version 12.04
  • Netbeans IDE 7.3
  • JDK 1.7
  • Firefox web-browser 21.0

You can use any web-browser of your choice.

Slide 4

Prerequisites

To follow this tutorial you must have knowledge of


  • Basics of Java Servlets and JSPs
  • Database and validation of fields

If not, for relevant tutorials please visit our website.

Switch to NetBeans IDE Now, let us switch to NetBeans IDE
Point to the Books table. I have created the Books table.

You can see the different fields in the table

I have inserted 10 books into this table.
Point to Checkout table. I have created Checkout table to store borrowed books.
I have inserted 5 entries into Checkout table.
I have also created a model for Book and Checkout.
Open Book.java and Checkout.java. Book.java is a book model.

And Checkout.java is a checkout model.

Switch to the browser. Now, come to the browser.
Let us login as the admin.
Type the username as admin and password as admin. So I will type the username and password as admin. Then click on Sign In.
Point your mouse on the Admin Section Page.


We can see that we come to the Admin Section Page.
Switch to Netbeans IDE. We will come back to this page. Now let us switch to Netbeans IDE.
We will see we how we modified the GreetingServlet to redirect to Admin Page.
Open GreetingServlet.java. Let us see the GreetingServlet.java.
Highlight if(username.equals("admin") && password.equals("admin")) {

RequestDispatcher view = request.getRequestDispatcher("adminsection.jsp");

view.forward(request, response);

return;

}


Here we check if username and password equals admin.

If yes, then we redirect to adminsection.jsp.


We have already seen how to forward to another page using RequestDispatcher.

Switch to browser. Now, switch back to the browser.
We have two options here.
Click on the radio button for List Books. We will click on the radio button for List Books.

Then click on Submit button.

Point to the list of all the books. Here, we can see that we have the list of all the Books.

It has all the details like Book Id, BookName,Author Name, Publisher, ISBN, Total Copies and Available copies.

Now I will show you how this is done..
Switch back to the IDE. Switch back to the IDE.
Open adminsection.jsp. Now, let us come to the adminsection dot jsp.
Highlight <input type="radio" name="menuselection" value="listbooks">List Books <br>

<input type="radio" name="menuselection" value="listborrowedbooks">List Borrowed Books <br>


Here we have two radio buttons.
Highlight <input type="radio" name="menuselection" value="listbooks">List Books <br> The first one is to list all the books.
Highlight form action=AdminSection.java. We can see that in adminsection dot jsp we have the form action as equal to AdminSection.
Open AdminSection dot java. Open AdminSection dot java.
Highlight if(menuSelection.equals("listbooks")){ This checks the option that we click on.

We clicked on List Books.

Highlight rs = statement.executeQuery("SELECT * FROM Books"); So this part of code is executed.

We execute query to fetch books from Books table.

Highlight

List<Book> books = new ArrayList<Book>();

Next we create ArrayList to store the details of books
Highlight while(rs.next()){

Book book = new Book();

book.setBookId(rs.getInt("id"));

book.setBookName(rs.getString("bookName"));

book.setAuthorName(rs.getString("authorName"));

book.setISBN(rs.getString("ISBN"));

book.setPublisher(rs.getString("publisher"));

book.setTotalCopies(rs.getInt("totalcopies"));

book.setAvailCopies(rs.getInt("availablecopies"));

books.add(book);

}


Then we iterate through the result set
Highlight Book book = new Book(); We create Book object
Highlight book.setBookId(rs.getInt("id")); We set BookId into the Book object
Highlight book.setBookName(rs.getString("bookName"));

book.setAuthorName(rs.getString("authorName"));

book.setISBN(rs.getString("ISBN"));

book.setPublisher(rs.getString("publisher"));

book.setTotalCopies(rs.getInt("totalcopies"));

book.setAvailCopies(rs.getInt("availablecopies"));

Similarly we set other attributes of book into Book Object
Highlight books.add(book); Then we add book object into the books list.
Highlight req.setAttribute("books", books); Then we set ArrayList books into the request.
Highlight RequestDispatcher requestDispatcher = req.getRequestDispatcher("listBooks.jsp");

requestDispatcher.forward(req, resp);


We forward the request to listBooks.jsp using RequestDispatcher
Now we come to listBooks.jsp
Highlight <p>

This form allows you to view the list of books.

</p>


In this page admin can view list of books.
Highlight

<%

List<Book> books = (ArrayList<Book>)request.getAttribute("books");

%>


Here, first we obtain books from request.
Highlight <table width='100%' border='1'>

<thead align='center'>

<th>Book Id</th>

<th>Book Name</th>

<th>Author Name</th>

<th>ISBN</th>

<th>Publisher</th>

<th>Total Copies</th>

<th>Available Copies</th>

</thead>


This HTML table will display details of books.



Highlight <%

for(Book book:books){

%>

<tr align='center'>

<td><%=book.getBookId()%></td>

<td><%=book.getBookName()%></td>

<td><%=book.getAuthorName()%></td>

<td><%=book.getISBN()%></td>

<td><%=book.getPublisher()%></td>

<td><%=book.getTotalCopies()%></td>

<td><%=book.getAvailCopies() %></td>


</tr>

<%

}

%>


So we will iterate through the book list.
Highlight <td><%=book.getBookId()%></td> Here we display BookId of book.
Highlight <td><%=book.getBookName()%></td>

<td><%=book.getAuthorName()%></td>

<td><%=book.getISBN()%></td>

<td><%=book.getPublisher()%></td>

<td><%=book.getTotalCopies()%></td>

<td><%=book.getAvailCopies() %></td>

Similarly we display other attributes of book.
This is how we display the list of books.
Switch back to the browser. Now, switch back to the browser.
click on List of Borrowed Books. Now, click on List of Borrowed Books.

Click on Submit button.

Point to the list of the books issued. We see a list of all the Books issued.

It has details like Transaction Id, Book Id and Username.

Switch back to the IDE. Now,I will switch back to the IDE.

And show you the code for same.

Go to AdminSection.java. Go to AdminSection.java.
Highlight if(menuSelection.equals("listborrowedbooks")){ We had clicked on List Borrowed Books.

So menuSelection is equal to List Borrowed books.

Highlight rs = statement.executeQuery("SELECT * FROM Checkout order by transaction_Id"); The steps are similar to what we saw for List Books.

We execute the query to fetch borrowed books details from Checkout table.



Highlight List<CheckOut> checkOut = new ArrayList<CheckOut>();

while(rs.next()){

CheckOut checkOutInstance = new CheckOut();

checkOutInstance.setBookId(rs.getInt("book_Id"));

checkOutInstance.setTransactionId(rs.getInt("transaction_Id"));

checkOutInstance.setUserName(rs.getString("username"));

checkOut.add(checkOutInstance);

}

req.setAttribute("checkout", checkOut);


Then we iterate through borrowed books.

And set it into request as checkout attribute.

Now we come to listBooks.jsp
List<CheckOut> checkout = (ArrayList<CheckOut>)request.getAttribute("checkout");


Here we obtain checkout from request.
Highlight

<%

for(CheckOut checkoutInstance:checkout){

%>

<tr align='center'>

<td><%=checkoutInstance.getTransactionId()%></td>

<td><%=checkoutInstance.getBookId()%></td>

<td><%=checkoutInstance.getUserName()%></td>


</tr>

<%

}

%>


We iterate through the Checkout list.

Here we display the attributes of Checkout.

This is how we display Borrowed Books.
Switch back to the browser. Now, switch back to the browser.
Point to the list. In the borrowed books page we also have one more list.

The list of books issued where current date is more than the return date.

Switch back to IDE Switch back to IDE to see the code.
This is done in the same way as we did for Borrowed Books.



Highlight rs = statement.executeQuery("SELECT * FROM Checkout where return_date < now() order by transaction_Id "); The only difference is in the SQL query.

In the query we give the condition, return_date < now() order by transaction_Id.

Now I will show you the interface for a normal user.
Switch back to the browser. So, switch back to the browser.
Come back to the login page. Come back to the login page.
Type the username as mdhusein. I will login as mdhusein.
Type the password as welcome. Type the password as welcome.
Click on Sign In. Click on Sign In.
Point to Success Greeting Page. We get a Success Greeting Page.
Highlight Transaction Id, User Name, Book Id and Return Date. It has the books currently borrowed by the user.

It has details like Transaction Id, User Name, Book Id and Return Date.

Let us come back to the IDE. Now, let us come back to the IDE.
Go to GreetingServlet.java. Now we go to GreetingServlet.java
We display the books issued in same way as we did for admin.


Here the difference will be that we have to display books for the logged in user.



So I get the username from this line.
Highlight stmt = con.prepareStatement("select * from Checkout where username = ? order by return_date");

stmt.setString(1, username);

rs = stmt.executeQuery();

Then we fetch the details of borrowed books.

With the condition username is equal to the logged in user.

Show successGreeting dot jsp So, we get the list of books issued for the corresponding user.

Then in successGreeting dot jsp we will display the list.


This is how your successGreeting dot jsp will look.

Slide 8

Summary

In this tutorial we have learnt to:
  • Modify the login page to redirect to admin page
  • Fetch all the book details
  • Fetch borrowed book details
  • To display the books borrowed by logged in user


Slide 9

About slide


Watch the video available at the following link.
  • It summarizes the Spoken Tutorial Project
  • If you do not have good bandwidth you can download and watch it


Slide 10

About slide

The Spoken Tutorial Project Team
  • Conducts workshops using spoken tutorials
  • Gives certificates for those who pass an online test
  • For more details please write to contact at spoken hyphen tutorial dot org


Slide 11

Acknowledgement

Spoken Tutorial Project is a part of the Talk to a Teacher Project


Slide 12

Contributor Slide

The Library Management System has been contributed by a leading software MNC, through their Corporate Social Responsibility Programme.


They have also validated the content for this spoken tutorial.


This is Arya Ratish from IIT Bombay signing off.


Thank you for joining.

Contributors and Content Editors

Arya Ratish, PoojaMoolya