Airline Ticket Reservation Website (Java and mySQL)


--------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------
Create 3D Animation and Video In 3 Clicks...know more

Installation and Testing of MySQL DB from Java

1. Download:
Common URL    : http://dev.mysql.com/downloads/mysql/5.1.html#downloads
MacOSX (10.6)  : http://dev.mysql.com/downloads/mirror.php?id=406393#mirrors
(mysql-5.1.61-osx10.6-x86.tar.gz)
Windows (32 bit): http://dev.mysql.com/downloads/mirror.php?id=406810#mirrors
(mysql-noinstall-5.1.61-win32.zip)
2. Install:
Unzip the zip to proper location, for example
/usr/local/mysql5
3. Configure:
i) Start mysql using below command to start it with root user
/usr/local/mysql5/bin/mysql -u root
ii) Create new Database 
mysql>CREATE DATABASE airlinedb;
iii) setup mySQL JDBC driver
a) Download JDBC Driver (Platform independent) 
b) Add mySQL jdbc driver to your project classpath 
mysql-connector-java-5.1.18-bin.jar
4.Testing:
i) Use below Java test to confirm the working   
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException; 
public class TestMySQLDB {
       public static void main(String[] argv) {
       System.out.println("Trying to connect mySQL database…");
              try { 
                     Class.forName("com.mysql.jdbc.Driver");
              } catch (ClassNotFoundException e) { 
                     System.out.println("Missing mySQL Driver?");
                     e.printStackTrace();
                     return;
              } 
              System.out.println("Registered mySQL JDBC driver successfully.");
              Connection connection = null;
              try {
                     connection = DriverManager
                                   .getConnection("jdbc:mysql://127.0.0.1:3306/airlinedb",
                                                 "root", "");

              } catch (SQLException e) {
                     System.out.println("Failed to Connect !");
                     e.printStackTrace();
                     return;
              }
               if (connection != null) {
                     System.out.println(" Connected Successfully !!!");
              } else {
                     System.out.println("Failed to Connect !");
              }
       }
}
5. Output:
Trying to connect mySQL database…

Registered mySQL JDBC driver successfully.
 Connected Successfully !!!

Airline Ticket Reservation Sample Website

1) Introduction:
Airline Ticket Reservation is a sample web application which could be use to search flights based on the specified airports (departure airport, arrival airport) and dates (departure date and return date).
2) References: 
- For basic DB preparations please refer to notes titled "Installation and Testing of Hypersonic DB"
- For basic Java EE setup for Airline Ticket Reservation Project refer to notes titled "Build and Deployment of Java EE Web Application (Airline Ticket Reservation)"
3) Database Preparations:
Create 2 tables as shown below:
i) Table to store Sector information as described below

CREATE TABLE SECTOR (
DEPARTURE_AP VARCHAR(100),
ARRIVAL_AP VARCHAR(100),
DEPARTURE_DT DATE,
ARRIVAL_DT DATE,
DEPARTURE_TM DECIMAL(4,2),
ARRIVAL_TM DECIMAL(4,2) ,
SID INTEGER,
PID INTEGER,
PRIMARY KEY (SID)
)
Insert 4 sample records: 
INSERT INTO SECTOR (DEPARTURE_AP, ARRIVAL_AP, DEPARTURE_DT,ARRIVAL_DT, DEPARTURE_TM, ARRIVAL_TM, SID, PID ) values ('NRT','LAX',DATE '2012-05-15',DATE '2012-05-15',10.05,19.01,1, 1)
INSERT INTO SECTOR (DEPARTURE_AP, ARRIVAL_AP, DEPARTURE_DT,ARRIVAL_DT, DEPARTURE_TM, ARRIVAL_TM, SID, PID ) values ('NRT','LAX',DATE '2012-05-15',DATE '2012-05-16',23.00,06.15,2, 2) 
INSERT INTO SECTOR (DEPARTURE_AP, ARRIVAL_AP, DEPARTURE_DT,ARRIVAL_DT, DEPARTURE_TM, ARRIVAL_TM, SID, PID ) values ('LAX','NRT',DATE '2012-05-25',DATE '2012-05-25',12.05,21.00,3, 1)
INSERT INTO SECTOR (DEPARTURE_AP, ARRIVAL_AP, DEPARTURE_DT,ARRIVAL_DT, DEPARTURE_TM, ARRIVAL_TM, SID, PID ) values ('LAX','NRT',DATE '2012-05-27',DATE '2012-05-27',11.05,20.00,4, 2)

ii) Table to store Price information as described below

CREATE TABLE PRICE (
PID INTEGER,
BASIC DECIMAL(10,2),
SURCHARGE DECIMAL(10,2),
TAX DECIMAL(10,2),
DISCOUNT DECIMAL(10,2),
CURR VARCHAR(3),
PRIMARY KEY (PID)
)
Insert 2 sample records: 

INSERT INTO PRICE (PID,BASIC,SURCHARGE,TAX,DISCOUNT,CURR) VALUES (1,1200,110,50,75,'USD')
INSERT INTO PRICE (PID,BASIC,SURCHARGE,TAX,DISCOUNT,CURR) VALUES (2,1800,200,145,105,'USD')









4) Java Source Preparations:
- Add hsqldb.jar to [/WEB-INF/lib] of "AirlineTicketReservation" Project in Eclipse

Installation and Testing of Hypersonic DB from Java

1. Download:
http://sourceforge.net/projects/hsqldb/files/hsqldb/hsqldb_2_2/hsqldb-2.2.8.zip/download
2. Install:
Unzip the hsqldb-2.2.8.zip to proper location, for example
/usr/local/hsqldb
3. Configure:
i) Open Configuration GUI using below command,
/usr/local/hsqldb/lib/java -jar hsqldb.jar
ii) Provide location for file based database, for example 
/Users/santoshlg/testdb

iii) To use hsqldb from the Eclipse project, add hsqldb.jar in the Eclipse Project Library reference,
4.Testing:
i) Connect to db GUI Console using above configuration to get below Database Manager GUI
ii) Create sample table CUSTOMER by using GUI Editor 
(Editor appears after successful connection as per above step i) 
CREATE TABLE CUSTOMER (
            LAST_NAME VARCHAR(60),
            FIRST_NAME VARCHAR(60),
            ACCOUNT_ID VARCHAR(10)
)

iii) Insert 2 test records
INSERT INTO CUSTOMER
            (LAST_NAME, FIRST_NAME,ACCOUNT_ID)
            VALUES
            ('HONDA','YUKI','A-1001');
INSERT INTO CUSTOMER
            (LAST_NAME, FIRST_NAME,ACCOUNT_ID)
            VALUES
            ('SUZUKI','ICHIRO','A-1002');

iv) Use below Java test to confirm the working 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class TestHSQLDB {
public static void main(String[] args) throws SQLException {
 
Connection conn = null;
PreparedStatement prepStmt = null;
ResultSet rs = null;
 
 try {
  Class.forName("org.hsqldb.jdbc.JDBCDriver");
  conn = 
  DriverManager.getConnection("jdbc:hsqldb:file:/Users/santoshlg/testdb", "SA", "");

            String selectStatement =
                "select * from Customer where ACCOUNT_ID = ?";
            prepStmt = conn.prepareStatement(selectStatement);
            prepStmt.setString(1, "A-1001");
            rs = prepStmt.executeQuery();

            while (rs.next()) {
                String title = rs.getString("LAST_NAME");
                System.out.println(title);
            }

  } catch (Exception e) {
      System.err.println("ERROR: failed to load HSQLDB JDBC driver.");
      e.printStackTrace();
  } finally {
            rs.close();
            prepStmt.close();
  conn.close();
  }
}
}

5. Output:
HONDA

Build and Deployment of Java EE Web Application (Airline Ticket Reservation)

Background: 
To develop Java EE web application using standard Eclipse Java IDE has many limitations, to overcome those limitations the Eclipse project has already created "Java EE Developers". Below are the steps to demonstrate, Building and Deployment of Web Application on JBoss Application server using "Java EE Developer".

Steps:
1) Download and Install "Eclipse IDE for Java EE Developers"
http://www.eclipse.org/downloads/?osType=win32

2) Create new project by selecting "Dynamic Web Project"
    [File][New][Project...][Web]

3) Set Project name : AirlineTicketReservation
   (Keep other values to default)

4) Add reference to the external Servlet library so that we can use Servlet API in our application
[jboss-5.0.1.GA/common/lib/servlet-api.jar]
/AirlineTicketReservation/WebContent/WEB-INF/web.xml
/AirlineTicketReservation/WebContent/searchView.html
/AirlineTicketReservation/src/controller/SearchController.java
/AirlineTicketReservation/WebContent/image/airline.png

6) Build the project
7) Export as a WAR (Web Archive) file :  AirlineTicketReservation.war
8) Deploy the WAR file to application server (Such as JBoss)
9) Access web application using URL with the web application context AirlineTicketReservation
10) Confirm log information
11) Source reference:
i) /AirlineTicketReservation/WebContent/WEB-INF/web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>

<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
  version="2.4">

  <welcome-file-list>
     <welcome-file>searchView.html</welcome-file>
  </welcome-file-list>
  
  <servlet>
    <servlet-name>searchController</servlet-name>
    <servlet-class>controller.SearchController</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>searchController</servlet-name>
    <url-pattern>/searchController.do</url-pattern>
  </servlet-mapping>
    
</web-app>


ii) /AirlineTicketReservation/WebContent/searchView.html
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Search Flight</title>
</head>
<body>

<form action="searchController.do" method="post">
<table border = "1" align="center">
  <tr>
<td align ="center" valign="bottom" bgcolor="#000080">
<font size="4" color="#FFFFFF"><b>Airline Ticket Reservation</b></font>
</td>
</tr>
<tr>
<td>
          <img src="image/airline.png" alt="Airline" width=100% height=100%>
</td>
</tr>
<tr>
<td>
<b>Airport</b> <br/>
From <input type="text" name="fromAirport" value="NRT">
To <input type="text" name="toAirport" value="LAX">
</td>
</tr>
<tr>
<td align="left">
<b>Date</b>[yyyy-mm-dd]<br/>
Depart <input type="text" name="fromYear" size="4">-<input type="text" name="fromMonth" size="2">-<input type="text" name="fromDay" size="2">
Return*   <input type="text" name="toYear" size="4"  >-<input type="text" name="toMonth"  size="2" >-<input type="text" name="toDay" size="2">
<br/>
<font size="2" color="#808080">*For one-way flight keep "Return" date blank</font>
</td>
</tr>
<tr>
<td align="right">
<input type="submit" value="Search">
</td>
</tr>
</table>
</form>

</body>
</html>

iii) /AirlineTicketReservation/src/controller/SearchController.java
package controller;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class SearchController extends HttpServlet {

@Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {
System.out.println("Calling doPost...");
String fromAirport = req.getParameter("fromAirport");
String toAirport = req.getParameter("toAirport");
System.out.println("From airport =" + fromAirport);
System.out.println("To   airport =" + toAirport);
}
}

iv) /AirlineTicketReservation/WebContent/image/airline.png

How to configure Eclipse IDE Code Completion key binding on Mac OSX

Follow below steps to assign [Ctrl + Space] combination for code completion in Eclipse for Java IDE.

Steps:
i) Open Eclipse Preferences
ii) Under [General][Keys] check for [Command] column
iii) Change [Content Assist] Binding to [^Space]
(Make sure the Binding is not already in use by any other Software including another Eclipse IDE)
iv) Apply changes

JavaEE Pragmatic Introduction

1) Background:
Below steps demonstrate about creation of workable web application based on Java EE architecture. This application will be deployed on "Application Server" [JBoss].


2) Application Server: JBoss 
http://sourceforge.net/projects/jboss/files/JBoss/JBoss-5.0.1.GA/jboss-5.0.1.GA-jdk6.zip/download

- Getting Started:
i) Unzip jboss-5.0.1.GA-jdk6.zip to appropriate location (/user/local/ or c:/)
ii) check for run.bat (windows) or run.sh (unix/linux like systems) and execute it
[use -b option to run it with IP address or hostname, so that it can be accessed using IP or Hostname instead of localhost always]
./run.sh -b 0.0.0.0
iii) check for the runtime logs to see if the server started without any errors


3) UML Tools:
http://astah.net/download
http://argouml.tigris.org/


4) Basic Application Structure:
i) Create new Java project in eclipse [JavaEE_Sample]
        
ii) Add new [Source Folder] named : web
                 

iii) Add new [Folder] named : WEB-INF under web source folder [web/WEB-INF]
iv) Add new file web.xml under WEB-INF folder [web/WEB-INF/web.xml]
This is a application descriptor for web application which maintains operational relationship of web application.
[web.xml]

<?xml version="1.0" encoding="ISO-8859-1"?>

<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
  version="2.4">

  <welcome-file-list>
     <welcome-file>index.html</welcome-file>
  </welcome-file-list>
    
</web-app>

v) Add new index.html file under web folder [web/index.html]

[index.html]
<html>
<body>
This is a sample web application. 
</body>
</html>


5) Deployment:
i) Save the project as war (web archive) file : myjavaee.war
(The war file is a zip file named as war so that Application Server can understand it as a web application)
ii) Copy the war file to the deploy folder of JBoss, which will deploy the web application
/JBossInstallation/server/default/deploy/myjavaee.war

iii) Start JBoss using run command 
/JBossInstallation/bin/run.sh (run.bat for windows)
iv) Confirm deployment by checking log file


6) Test confirmation:
i) Access the deployed web application using below context
http://localhost:8080/myjavaee
(8080 is default port where JBoss runs)
ii) Check output to see below message
=========================================================