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