Java Airline Reservation System – Travel Smarter, Not Harder.pdf
1. Java Airline Reservation System
– Travel Smarter, Not Harder
Welcome to this project on developing an Airline Reservation System using Java
and SQLite! This system will consist of four panels – Reservations, Customers,
Flights, and Airports – each of which will have a JTable to display data from the
database and an input panel to take input from the user and store it into the
database.
In this project, we will cover the step-by-step process of designing and implementing
this system, including setting up the database, creating the user interface, and
integrating the functionality to perform various operations such as adding new
reservations, customers, flights, and airports, as well as updating and deleting
existing data.
International flights are those that operate between two or more countries, typically
crossing international boundaries. These flights usually involve longer distances. On
the other hand, domestic flights operate within a single country. Domestic flights are
typically shorter in duration and do not require passengers to go through customs or
immigration procedures.
For this we will be creating a toggle button which will allow the users to switch
between international and domestic flights in the Reservations panel. This button will
update our flight choices to reflect the option we selected using the toggle button.
By the end of this project, you will have a working Airline Reservation System that
can efficiently manage and store customer information, flight schedules, and
2. reservations, making it an invaluable tool for any airline or travel agency. So let’s get
started!
About Java Airline Reservation System
The objective of this project is to provide a comprehensive guide to developing an
Airline Reservation System using Java and SQLite.
By the end of this project, you will be able to design and implement a system that
can efficiently manage and store customer information, flight schedules, and
reservations. Specifically, you will learn how to set up the database, create the user
interface with JTables, and integrate the functionality to perform various operations
such as adding, updating, and deleting data.
This project aims to equip you with the skills and knowledge necessary to develop a
fully-functional Airline Reservation System.
Prerequisites for Airline Reservation
System using Java
Before starting with this tutorial, you should have the following prerequisites:
■ Basic knowledge of Java programming language, including
object-oriented programming concepts.
■ Understanding of SQL and database concepts.
■ Experience with Eclipse or any other Java Integrated Development
Environment (IDE).
■ Basic knowledge of Swing GUI toolkit for building user interfaces.
■ Familiarity with SQLite database management system.
3. Download Java Airline Reservation
System Project
Please download the source code of Java Airline Reservation System Project from
the following link: Java Airline Reservation System Project Code
Steps to Create Airline Reservation
System using Java
Following are the steps for developing the Java Airline Reservation System Project:
Step 1: Setting Up the Classes in Eclipse
1. Launch Eclipse and go to “File” > “New” > “Java Project”.
2. Assign a name, such as “Airline Reservation System”.
3. Right-click on the project and select “New” > “Class”.
4. Name the first class, for example, “AirlineReservation”.
5. Repeat steps 3 and 4 to create another class, such as “Database”.
Step 2: Adding SQLite to the Project in Eclipse
Note: Before moving forward, make sure to have the SQLite JAR file downloaded
and saved on your computer. This file can be found on MavenRepository
1. Go to the Project Explorer in Eclipse and right-click on your project.
2. Select “Properties” from the menu.
3. In the Properties window, navigate to “Java Build Path” and click the
“Libraries” tab.
4. Click the “Add External JARs” button and find the SQLite JAR file.
5. Choose the JAR file and press “Open”.
6. Click “OK” to close the Properties window.
4. Incorporating SQLite into your Eclipse project is now complete after following the
steps outlined above.
Step 3: Implementing the Database class with the
required methods
Below is the complete code for the Database class :
package school.FirstCode;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JComboBox;
import javax.swing.table.DefaultTableModel;
import org.sqlite.SQLiteDataSource;
public class Database {
// declaring connection and datasource variables
static Connection conn;
static SQLiteDataSource ds;
// initialize method to initialize the database with all the tables
public static void dbInit() {
ds = new SQLiteDataSource();
try {
5. ds = new SQLiteDataSource();
ds.setUrl("jdbc:sqlite:AirlineDB.db");
} catch ( Exception e ) {
e.printStackTrace();
System.exit(0);
}
try {
conn = ds.getConnection();
Statement statement = conn.createStatement();
// Create the airports table
statement.executeUpdate("CREATE TABLE IF NOT EXISTS airports (n"
+ " id INTEGER PRIMARY KEY,n"
+ " name TEXT NOT NULL,n"
+ " city TEXT NOT NULL,n"
+ " country TEXT NOT NULLn"
+ ");");
// Create the flights table
statement.executeUpdate("CREATE TABLE IF NOT EXISTS flights (n"
+ " id INTEGER PRIMARY KEY,n"
+ " departure_airport_id INTEGER NOT NULL,n"
+ " arrival_airport_id INTEGER NOT NULL,n"
+ " departure_time TEXT NOT NULL,n"
+ " arrival_time TEXT NOT NULL,n"
6. + " price DECIMAL(10, 2) NOT NULL,n"
+ " domestic TEXT NOT NULL CHECK (domestic IN ('yes', 'no')),n"
+ " CONSTRAINT fk_departure_airportn"
+ " FOREIGN KEY (departure_airport_id)n"
+ " REFERENCES airports (id),n"
+ " CONSTRAINT fk_arrival_airportn"
+ " FOREIGN KEY (arrival_airport_id)n"
+ " REFERENCES airports (id)n"
+ " );n");
// Create the customers table
statement.executeUpdate("CREATE TABLE IF NOT EXISTS customers (n"
+ " id INTEGER PRIMARY KEY,n"
+ " first_name TEXT NOT NULL,n"
+ " last_name TEXT NOT NULL,n"
+ " email TEXT NOT NULL,n"
+ " phone_number TEXTn"
+ ");");
// Create the customers table
statement.executeUpdate("CREATE TABLE IF NOT EXISTS reservations (n"
+ " id INTEGER PRIMARY KEY,n"
+ " customer_id INTEGER NOT NULL,n"
+ " flight_id INTEGER NOT NULL,n"
+ " seat_number TEXT NOT NULL,n"
8. }
// Method to get update the reservation table with the data from the database
public static void updateReservations(DefaultTableModel model) throws SQLException {
model.setRowCount(0);
conn = ds.getConnection();
String query = "SELECT * From reservations";
PreparedStatement ps = conn.prepareStatement(query);
ResultSet rs = ps.executeQuery();
// Add data to the table model
while (rs.next()) {
int id = rs.getInt("id");
String customer_id = rs.getString("customer_id");
String flight_id = rs.getString("flight_id");
String seat_number = rs.getString("seat_number");
Object[] row = {id, customer_id, flight_id,seat_number};
model.addRow(row);
}
rs.close();
ps.close();
conn.close();
}
// Method to get update the Flights table with the data from the database
public static void updateFlights(DefaultTableModel model) throws SQLException {
9. model.setRowCount(0);
conn = ds.getConnection();
String query = "SELECT * From flights";
PreparedStatement ps = conn.prepareStatement(query);
ResultSet rs = ps.executeQuery();
// Add data to the table model
while (rs.next()) {
int id = rs.getInt("id");
int departure_airport = rs.getInt("departure_airport_id");
int arrival_airport = rs.getInt("arrival_airport_id");
String departure_time = rs.getString("departure_time");
String arrival_time = rs.getString("arrival_time");
Float price = rs.getFloat("price");
String domestic = rs.getString("domestic");
Object[] row = {id, departure_airport,
arrival_airport,departure_time,arrival_time,price,domestic};
model.addRow(row);
}
rs.close();
ps.close();
conn.close();
}
// Method to get update the Customers table with the data from the database
10. public static void updateCustomers(DefaultTableModel model) throws SQLException {
model.setRowCount(0);
conn = ds.getConnection();
String query = "SELECT * From customers";
PreparedStatement ps = conn.prepareStatement(query);
ResultSet rs = ps.executeQuery();
// Add data to the table model
while (rs.next()) {
int id = rs.getInt("id");
String first_name = rs.getString("first_name");
String last_name = rs.getString("last_name");
String email = rs.getString("email");
String phone_number = rs.getString("phone_number");
Object[] row = {id, first_name, last_name,email,phone_number};
model.addRow(row);
}
rs.close();
ps.close();
conn.close();
}
// Method to get update the Airports table with the data from the database
public static void updateAirports(DefaultTableModel model) throws SQLException {
model.setRowCount(0);
11. conn = ds.getConnection();
String query = "SELECT * From airports";
PreparedStatement ps = conn.prepareStatement(query);
ResultSet rs = ps.executeQuery();
// Add data to the table model
while (rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
String city = rs.getString("city");
String country = rs.getString("country");
Object[] row = {id, name, city,country};
model.addRow(row);
}
rs.close();
ps.close();
conn.close();
}
// Method to delete an specific item from a specific table
public static void delete(String tableName,String id) throws SQLException {
conn = ds.getConnection();
String query = "DELETE FROM "+tableName+" WHERE id = "+id+";";
Statement stmt = conn.createStatement();
stmt.execute(query);
12. stmt.close();
conn.close();
}
// Method to update jcomboBoxes with the required field values
public static void updateBox(JComboBox<String> cbx,String tableName,String
domestic_status) throws SQLException {
cbx.removeAllItems();
conn = ds.getConnection();
String query = "SELECT id FROM "+tableName+" WHERE domestic = '"+domestic_status+"';";
PreparedStatement ps = conn.prepareStatement(query);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
cbx.addItem(rs.getString("id"));
}
ps.close();
conn.close();
}
public static void updateBox(JComboBox<String> cbx,String tableName) throws
SQLException {
cbx.removeAllItems();
conn = ds.getConnection();
String query = "SELECT id FROM "+tableName+";";
PreparedStatement ps = conn.prepareStatement(query);
ResultSet rs = ps.executeQuery();
13. while (rs.next()) {
cbx.addItem(rs.getString("id"));
}
ps.close();
conn.close();
}
// Method to add the reservations data into the reservations table
public static void addReservation(int id,int custID,int flightID,String seatNum)
throws SQLException {
conn = ds.getConnection();
PreparedStatement ps =conn.prepareStatement("INSERT INTO "
+ "reservations(id,customer_id,flight_id,seat_number) "
+ "VALUES(?,?,?,?)");
ps.setInt(1, id);
ps.setInt(2, flightID);
ps.setInt(3, custID);
ps.setString(4, seatNum);
ps.executeUpdate();
ps.close();
conn.close();
}
// Method to add the Customer data into the Customers table
public static void addCustomer(int id,String first_name,String last_name,String
email,String phone) throws SQLException {
14. conn = ds.getConnection();
PreparedStatement ps =conn.prepareStatement("INSERT INTO "
+ "customers(id,first_name,last_name,email,phone_number) "
+ "VALUES(?,?,?,?,?)");
ps.setInt(1, id);
ps.setString(2, first_name);
ps.setString(3, last_name);
ps.setString(4, email);
ps.setString(5, phone);
ps.executeUpdate();
ps.close();
conn.close();
}
// Method to add the Flight data into the Flights table
public static void addFlight(int id,int departure_ap_id,int arrival_ap_id,String
departure_time,String arrival_time,float price,String domestic) throws SQLException {
conn = ds.getConnection();
PreparedStatement ps =conn.prepareStatement("INSERT INTO "
+
"flights(id,departure_airport_id,arrival_airport_id,departure_time,arrival_time,price,
domestic) "
+ "VALUES(?,?,?,?,?,?,?)");
ps.setInt(1, id);
ps.setInt(2, departure_ap_id);
15. ps.setInt(3, arrival_ap_id);
ps.setString(4, departure_time);
ps.setString(5, arrival_time);
ps.setFloat(6, price);
ps.setString(7,domestic);
ps.executeUpdate();
ps.close();
conn.close();
}
// Method to add the Airport data into the Airports table
public static void addAirport(int id,String name,String city,String country ) throws
SQLException {
conn = ds.getConnection();
PreparedStatement ps =conn.prepareStatement("INSERT INTO "
+ "airports(id,name,city,country) "
+ "VALUES(?,?,?,?)");
ps.setInt(1, id);
ps.setString(2, name);
ps.setString(3, city);
ps.setString(4, country);
ps.executeUpdate();
ps.close();
conn.close();
16. }
}
■ dbInit() – This function initializes the database by creating tables for
airports, flights, customers, and reservations using SQLite queries.
■ updateReservations(DefaultTableModel model) – This function updates
the reservations table from the database and sets the data in the given
table model. It retrieves all the data from the reservations table and adds
them to the table model.
■ updateFlights(DefaultTableModel model) – This function updates the
flights table from the database and sets the data in the given table model.
It retrieves all the data from the flights table and adds them to the table
model.
■ updateCustomers(DefaultTableModel model) – This function updates the
customers table from the database and sets the data in the given table
model. It retrieves all the data from the customers table and adds them to
the table model.
■ updateAirports(DefaultTableModel model) – This function updates the
airports table from the database and sets the data in the given model.It
retrieves all the data from the Airport table and adds them to the table
model.
Similarly, there are functions to add data to the database like
addFlight,addCustomer, etc. Then there’s a delete function that delete the data from
the database by using the id field and the table’s name and lastly, the updateBox
function updates the combo boxes with relevant data.
All these functions connect to the database using the ds (SQLiteDataSource) object
and execute SQL queries to fetch data from the tables. They also use the
PreparedStatement object to execute SQL queries with parameters. Once the data
is fetched from the tables, it is added to the DefaultTableModel object that is passed
as a parameter to these functions. Finally, the connection to the database is closed
after executing the queries.
Step 3: Implementing the AirlineReservation
39. }
}
Summary
This project demonstrates the use of SQLite and JDBC to create and manage a
database for an airline reservation system. The project covers the creation of a
database, creating tables and establishing relationships between them, adding data
to the database, and querying the database to retrieve data.
The code includes methods to update tables in the database and retrieve data from
them, which are demonstrated through the creation of a GUI that allows the user to
view flights, airports, customers, and reservations. The GUI also allows the user to
add new flights and customers to the database, as well as create and delete
reservations.
Overall, this project provides a practical example of using JDBC and SQL to create a
database and perform basic CRUD (Create, Read, Update, Delete) operations on it.
It also demonstrates the use of a graphical user interface (GUI) to interact with the
database, which can make it more accessible to end-users.