Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 1 of 1

Thread: [URGENT] Unable to display data retrieved from the database in table format in JSP page

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation [URGENT] Unable to display data retrieved from the database in table format in JSP page

    Hi, I'm a second year student. I'm doing a project on java servlet. However, i encountered a problem along the way. I have tried to search online and also ask my friends who are better in programming but they are not able to identified the problem. I would really appreciated that you could point out where i had gone, and helped me solved the problem that i had encountered. (This is just a small part of the project)


    For this part, the JSP page is supposed to retrieved the data from the database and display the retrieved data into the table that is to be display on the JSP page.The problem i encountered is that the JSP is unable to retrieved the data and displayed them in the table in the JSP page. Sorry, for pasting the code like this. It has been a long time since i last used this forum. For those who want to have a better view of the code, I have attach my project file. Here are the following code:


    HTML Code:
    ViewBookingsAdmin.jsp
    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>View Bookings</title>
    </head>
    <style>
    #box-table-a
    {
    font-family: "Lucida Sans Unicode", "Lucida Grande", Sans-Serif;
    font-size: 12px;
    margin: 45px;
    width: 480px;
    text-align: left;
    border-collapse: collapse;
    }
    #box-table-a th
    {
    font-size: 13px;
    font-weight: normal;
    padding: 8px;
    background: #b9c9fe;
    border-top: 4px solid #aabcfe;
    border-bottom: 1px solid #fff;
    color: #039;
    }
    #box-table-a td
    {
    padding: 8px;
    background: #e8edff;
    border-bottom: 1px solid #fff;
    color: #669;
    border-top: 1px solid transparent;
    }
    #box-table-a tr:hover td
    {
    background: #d0dafd;
    color: #339;
    }
    
    </style>
    <body>
    <%@ page import = "database.DBController, java.util.*" %>
    <%@ page import = "entity.BookingDetails" %>
    <% DBController dbc = new DBController();
    List bookings = dbc.getBookings();
    %>
    
    
    <p>
    <table id = 'box-table-a' class="entity.BookingDetails">
    
    <thread>
    <tr>
    <th>Name</th>
    <th>Address</th>
    <th>Contact</th>
    <th>Start Date</th>
    <th>NoOfMonthsOrdering</th>
    <th>NoOfppl</th>
    <th>mealplan</th>
    </tr>
    </thread>
    
    
    <% for (int i = 0; i<bookings.size(); i++ ) {
    
    	 BookingDetails bd = (BookingDetails)bookings.get(i);
    	
    %>
    
    <tbody>
    
    <tr>
    
    <td><%= bd.getName() %></td>
    <td><%= bd.getAddress() %></td>
    <td><%= bd.getContact() %></td>
    <td><%= bd.getStartDate() %></td>
    <td><%= bd.getNoOfMthsOrdering() %></td>
    <td><%= bd.getNoOfppl() %></td>
    <td><%= bd.getMealplan() %></td>
    
    </tr>
    </tbody>
    
    <%
    }
    %>
    
    </table>
    </p>
    
    </body>
    </html>
    ////////////////////////////////////////////////////////////////////////////////////////
    BookingTableServlet1.java
    
    package servlets;
    import java.io.IOException;
    import java.util.List;
    import javax.servlet.RequestDispatcher;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import database.DBController;
    import entity.BookingDetails;
    /**
    * Servlet implementation class BookingTableServlet1
    */
    public class BookingTableServlet1 extends HttpServlet {
    private static final long serialVersionUID = 1L;
    	
    /**
    	 * @see HttpServlet#HttpServlet()
    	 */
    public BookingTableServlet1() {
    	 super();
    	 // TODO Auto-generated constructor stub
    }
    /**
    * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
    */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    
    //List<bookings> bookinglist = null;
    List<BookingDetails> bookinglist = null;
    DBController dbc;
    try{
    dbc = new DBController();
    bookinglist = dbc.getBookings();
    
    System.out.println("bookinglist:"+ bookinglist);
    String destination = "ViewBookingsAdmin.jsp";
    request.setAttribute("bookinglist", bookinglist);
    
    RequestDispatcher rd = request.getRequestDispatcher(destination);
    rd.forward(request, response);
    
    }
    
    catch(Exception e){
    e.printStackTrace();
    }
    }
    /**
    * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
    */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    String screen = request.getServletPath() + ".jsp";
    
    try {
    			 request.getRequestDispatcher(screen)
    				 .forward(request, response);
    		 } catch (Exception ex) {
    			 ex.printStackTrace();
    		 }
    
    
    
    
    }
    }
    
    ///////////////////////////////////////////////////////////////////////////////////////////
    BookingDetails.java
    package entity;
    import java.util.List;
    import database.DBController;
    import exception.BookingsNotFoundException;
    public class BookingDetails {
    
    public String Name;
    public String Address;
    public String Contact;
    public String startDate;
    public String NoOfMthsOrdering;
    public String NoOfppl;
    public String mealplan;
    
    
    private DBController database = null;
    
    public DBController getDatabase() {
    return database;
    }
    public void setDatabase(DBController database) {
    this.database = database;
    }
    public List getBookings() throws BookingsNotFoundException {
    return database.getBookings();
    
    }
    public BookingDetails() {
    
    }
    
    
    
    public BookingDetails(String Name, String Address, String Contact,String startDate,
    String NoOfMthsOrdering,String NoOfppl, String mealplan)
    {
    this.Name = Name;
    this.Address = Address;
    this.Contact = Contact;
    this.startDate = startDate;
    this.NoOfMthsOrdering = NoOfMthsOrdering;
    this.NoOfppl = NoOfppl;
    this.mealplan = mealplan;
    
    }
    
    
    public String getName() {
    return Name;
    }
    public void setName(String name) {
    Name = name;
    }
    public String getAddress() {
    return Address;
    }
    public void setAddress(String address) {
    Address = address;
    }
    public String getContact() {
    return Contact;
    }
    public void setContact(String contact) {
    Contact = contact;
    }
    public String getStartDate() {
    return startDate;
    }
    public void setStartDate(String startDate) {
    this.startDate = startDate;
    }
    public String getNoOfMthsOrdering() {
    return NoOfMthsOrdering;
    }
    public void setNoOfMthsOrdering(String noOfMthsOrdering) {
    NoOfMthsOrdering = noOfMthsOrdering;
    }
    public String getNoOfppl() {
    return NoOfppl;
    }
    public void setNoOfppl(String noOfppl) {
    NoOfppl = noOfppl;
    }
    public String getMealplan() {
    return mealplan;
    }
    public void setMealplan(String mealplan) {
    this.mealplan = mealplan;
    }
    
    //public boolean createBooking(String Name, String Address, String Contact,String startDate,
    //String NoOfMthsOrdering,String NoOfppl, String mealplan ){
    	
    	 /*
    	 String SQLQuery = "";
    	
    //DBController dbc = new DBController();
    
    	 boolean status = false;
    	
    	
    	
    	 SQLQuery = "INSERT INTO booking(Name, Address, Contact, startDate, NoOfMthsOrdering, NoOfppl, mealplan) ";
    	 SQLQuery += "VALUES ('" + Name + "','" + Address + "','" + Contact + "','" + startDate + "','" + NoOfMthsOrdering + "','" + NoOfppl
    	 + "','" + mealplan + "')" ;
    	
    	 System.out.println(SQLQuery);
    	 if(DBController.updateRequest(SQLQuery)==1){
    	
    	 status = true;
    	
    	
    	 DBController.remove();
    return status;
    
    	
    }
    return status; */
    	
    	
    	
    //@Override
    //public int compareTo(Object arg0) {
    //TODO Auto-generated method stub
    //return 0;
    //}
    
    
    }
    ////////////////////////////////////////////////////////////////////////////////////////////////
    DBController.java
    package database;
    
    import java.sql.*;
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.List;
    import entity.BookingDetails;
    import exception.BookingsNotFoundException;
    //Database connection for Trial website
    
    public class DBController {
    
    
    private ArrayList bookings;
    static Connection con;
    
    
    // Database configuration
    public static String url = "jdbc:mysql://localhost/trial";
    public static String dbdriver = "com.mysql.jdbc.Driver";
    public static String username = "root";
    public static String password = "root";
    
    
    
    public DBController() throws Exception {
    	 try {
    		 Class.forName(dbdriver);
    		 con = DriverManager.getConnection(url, username, password);
    		
    	 }
    	 catch (Exception ex) {
    		 System.out.println("Exception in BookingsDBAO: " + ex);
    		 throw new Exception("Couldn't open connection to database: " +
    				 ex.getMessage());
    	 }
    }
    
    
    
    
    
    public Connection getConnection() {
    	 return con;
    }
    
    
    
    
    
    
    public static void remove(){
    	
    	 try {
    		 if (con!=null) con.close();
    	 }
    	 catch (SQLException ex) {
    		 System.out.println(ex.getMessage());
    	 }
    
    }
    
    
    
    
    
    //public void createBooking( ){
    	
    //}
    
    
    public static int updateRequest(String dbQuery){
    	
    	 int count = 0;
    	 System.out.println("DB Query: " + dbQuery);
    	 try{
    	 Statement stmt = con.createStatement();
    count = stmt.executeUpdate(dbQuery);
    	 }
    	 catch (Exception e) {
    e.printStackTrace();
    }
    	 return count;
    }
    
    
    
    public ResultSet readRequest(String dbQuery){
    	
    	 ResultSet rs = null;
    	 System.out.println("DBQuery: " + dbQuery);
    	 try{
    	 Statement stmt = con.createStatement();
    	 rs = stmt.executeQuery(dbQuery);
    	 }
    	 catch (Exception e){
    	 e.printStackTrace();
    	 }
    return rs;
    	
    }
    
    
    
    
    
    public boolean createBooking(String Name, String Address, String Contact,
    	 String startDate, String NoOfMthsOrdering, String NoOfppl, String mealplan){
    	
    	
    	 String SQLQuery = "";
    	 boolean status = false;
    	
    	 SQLQuery = "Insert into booking(Name, Address, Contact, startDate, NoOfMthsOrdering, NoOfppl, mealplan)";
    	 SQLQuery += "VALUES ('" + Name + "','" + Address + "','" + Contact + "','" + startDate + "','" + NoOfMthsOrdering + "','" + NoOfppl + "','" + mealplan + "')" ;
    	 System.out.println(SQLQuery);
    	 if(DBController.updateRequest(SQLQuery)==1){
    	
    	 status = true;
    	
    	
    	 DBController.remove();
    //return status;
    
    	
    }
    	
    return status;
    	
    }
    
    
    
    
    
    
    
    public List getBookings() throws BookingsNotFoundException {
    
    bookings = new ArrayList();
    String SQLQuery = "";
    //DBController dbc = new DBController();
    //dbc.getConnection();
    
    //SQLQuery = "SELECT * FROM booking";
    //ResultSet rs = null;
    //rs = dbc.readRequest(SQLQuery);
    
    
    
    try{
    
    
    SQLQuery = "select * from booking";
    PreparedStatement prepStmt = con.prepareStatement(SQLQuery);
    ResultSet rs = prepStmt.executeQuery();
    
    
    while (rs.next()) {
    
    BookingDetails bd = new BookingDetails (rs.getString(1), rs.getString(2), rs.getString(3),
    	 rs.getString(4), rs.getString(5), rs.getString(6), rs.getString(7));
    
    //if (rs.getInt(7)>0){
    //bookings.add(bd);
    //}
    
    }
    prepStmt.close();
    }
    
    catch (SQLException ex){
    
    throw new BookingsNotFoundException(ex.getMessage());
    
    }
    
    	 Collections.sort(bookings);
    	 return bookings;
    
    	 }
    
    
    }
    Attached Files Attached Files


Similar Threads

  1. Display data from list on same jsp page
    By reubenmk in forum JavaServer Pages: JSP & JSTL
    Replies: 7
    Last Post: February 13th, 2012, 03:54 AM
  2. Unable to view jsp pages through same login page
    By Rajiv in forum Web Frameworks
    Replies: 1
    Last Post: August 17th, 2011, 11:43 AM
  3. how can i display my data on jsp page in struts2
    By shekhar16 in forum Web Frameworks
    Replies: 0
    Last Post: March 4th, 2011, 06:26 AM
  4. Display Servlet Output into JSP Page
    By DanBrown in forum JavaServer Pages: JSP & JSTL
    Replies: 3
    Last Post: February 28th, 2011, 10:34 AM
  5. Replies: 0
    Last Post: April 15th, 2010, 05:13 AM

Tags for this Thread