1 / 39

Java Airline Reservation System – Travel Smarter, Not Harder

https://firstcode.school/java-airline-reservation-system/

ayushii12
Download Presentation

Java Airline Reservation System – Travel Smarter, Not Harder

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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 NULL\n" + ");"); // 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_airport\n" + " FOREIGN KEY (departure_airport_id)\n" + " REFERENCES airports (id),\n" + " CONSTRAINT fk_arrival_airport\n" + " 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 TEXT\n" + ");"); // 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"

  7. + " CONSTRAINT fk_customer\n" + " FOREIGN KEY (customer_id)\n" + " REFERENCES customers (id),\n" + " CONSTRAINT fk_flight\n" + " FOREIGN KEY (flight_id)\n" + " REFERENCES flights (id)\n" + ");"); // Closing statement and connection statement.close(); conn.close(); }catch ( SQLException e ) { e.printStackTrace(); System.exit( 0 ); } finally { try { if (conn != null) { conn.close(); } }catch (SQLException e) { System.err.println(e); } }

  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

  17. Below is the complete code for the AirlineReservation class : package school.FirstCode; import java.awt.EventQueue; import javax.swing.JFrame; import javax.swing.JTabbedPane; import java.awt.BorderLayout; import javax.swing.JPanel; import java.awt.Color; import javax.swing.border.TitledBorder; import javax.swing.table.DefaultTableModel; import javax.swing.border.LineBorder; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.sql.SQLException; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JTextField; import javax.swing.JComboBox; import javax.swing.JButton; import javax.swing.JScrollPane; import javax.swing.JTable;

  18. import javax.swing.JToggleButton; public class AirlineReservation { private JFrame frmAirlineReservatoinSystem; private JTextField resIDField; private JTextField seatField; private JTable reservationsTable; private JTextField custIDField; private JTextField firstNamefield; private JTable customerTable; private JTextField lastNameField; private JTextField emailField; private JTextField textField_1; private JTextField airportIDField; private JTextField airportNameField; private JTextField cityField; private JTextField countryField; private JTable airportsTable; private JTextField flightIDField; private JTextField departureTimeField; private JTextField arrivalTimeField; private JTextField priceField; private JTable flightTable; /**

  19. * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { Database.dbInit(); AirlineReservation window = new AirlineReservation(); window.frmAirlineReservatoinSystem.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the application. */ public AirlineReservation() { initialize(); } /** * Initialize the contents of the frame.

  20. */ private void initialize() { frmAirlineReservatoinSystem = new JFrame(); frmAirlineReservatoinSystem.setTitle("Airline Reservation System"); frmAirlineReservatoinSystem.setBounds(100, 100, 800, 500); frmAirlineReservatoinSystem.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frmAirlineReservatoinSystem.getContentPane().setLayout(new BorderLayout(0, 0)); frmAirlineReservatoinSystem.setResizable(false); JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP); frmAirlineReservatoinSystem.getContentPane().add(tabbedPane); JPanel reservationsPanel = new JPanel(); tabbedPane.addTab("Reservations", null, reservationsPanel, null); reservationsPanel.setLayout(null); JPanel resInputPanel = new JPanel(); resInputPanel.setBounds(5, 53, 281, 349); resInputPanel.setBorder(new TitledBorder(new LineBorder(new Color(0, 0, 0), 5), "Add Reservation", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 0, 0))); resInputPanel.setBackground(new Color(192, 191, 188)); reservationsPanel.add(resInputPanel); resInputPanel.setLayout(new GridLayout(0, 2, 0, 10)); JLabel lblID = new JLabel("ID"); resInputPanel.add(lblID); resIDField = new JTextField();

  21. resIDField.setColumns(10); resInputPanel.add(resIDField); JLabel lblSeatNo = new JLabel("Seat Number"); resInputPanel.add(lblSeatNo); seatField = new JTextField(); seatField.setColumns(10); resInputPanel.add(seatField); JLabel lblflightID = new JLabel("Flight ID"); resInputPanel.add(lblflightID); JComboBox<String> flightComboBox = new JComboBox<String>(); resInputPanel.add(flightComboBox); JLabel lblCustID = new JLabel("Customer ID"); resInputPanel.add(lblCustID); JComboBox<String> custComboBox = new JComboBox<String>(); resInputPanel.add(custComboBox); JScrollPane reservationscrollPane = new JScrollPane(); reservationscrollPane.setBounds(295, 53, 500, 349); reservationsPanel.add(reservationscrollPane); String[] reservationColumsNames = {"ID","Customer ID","Flight ID","Seat No."}; DefaultTableModel reservationsModel = new DefaultTableModel(reservationColumsNames,0); reservationsTable = new JTable(); reservationsTable.setModel(reservationsModel); reservationscrollPane.setViewportView(reservationsTable);

  22. JButton btnLoadReservations = new JButton("Load"); btnLoadReservations.setBackground(new Color(188,108,37)); btnLoadReservations.setBounds(295, 25, 100, 30); btnLoadReservations.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { try { Database.updateReservations(reservationsModel); } catch (SQLException e1) { e1.printStackTrace(); } } }); reservationsPanel.add(btnLoadReservations); JToggleButton tglbtnDomesticFlights = new JToggleButton("Domestic Flights",true); tglbtnDomesticFlights.setBackground(new Color(188,108,37)); tglbtnDomesticFlights.setBounds(400, 25, 200, 30); tglbtnDomesticFlights.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if (tglbtnDomesticFlights.isSelected()) { tglbtnDomesticFlights.setText("Domestic Flights"); try {

  23. Database.updateBox(flightComboBox,"flights", "yes"); } catch (SQLException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } else { tglbtnDomesticFlights.setText("International Flights"); try { Database.updateBox(flightComboBox,"flights", "no"); } catch (SQLException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } } }); reservationsPanel.add(tglbtnDomesticFlights); JButton btnaddReservation = new JButton("Add"); btnaddReservation.setBackground(new Color(188, 108, 37)); btnaddReservation.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { try {

  24. Database.addReservation(Integer.valueOf(resIDField.getText()),Database.addReservation(Integer.valueOf(resIDField.getText()), Integer.valueOf(custComboBox.getSelectedItem().toString()), Integer.valueOf(flightComboBox.getSelectedItem().toString()), seatField.getText()); Database.updateReservations(reservationsModel); JOptionPane.showMessageDialog(btnaddReservation, "Added successfully","Success",JOptionPane.INFORMATION_MESSAGE); } catch (NumberFormatException e1) { JOptionPane.showMessageDialog(btnaddReservation, "Enter Valid ID","Invalid ID",JOptionPane.INFORMATION_MESSAGE); e1.printStackTrace(); } catch (SQLException e1) { JOptionPane.showMessageDialog(btnaddReservation, "Can't Add","Error",JOptionPane.ERROR_MESSAGE); e1.printStackTrace(); } } }); resInputPanel.add(btnaddReservation); JButton btnRemoveReservation = new JButton("Remove"); btnRemoveReservation.setBackground(new Color(188, 108, 37)); btnRemoveReservation.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) {

  25. try { Database.delete("reservations",resIDField.getText()); Database.updateReservations(reservationsModel); JOptionPane.showMessageDialog(btnRemoveReservation, "Deleted","success" ,JOptionPane.INFORMATION_MESSAGE); } catch (SQLException e1) { JOptionPane.showMessageDialog(btnRemoveReservation, "Can't Delete","Error" ,JOptionPane.ERROR_MESSAGE); e1.printStackTrace(); } } }); resInputPanel.add(btnRemoveReservation); JPanel customerPanel = new JPanel(); tabbedPane.addTab("Customers", null, customerPanel, null); customerPanel.setLayout(null); JPanel custInputPanel = new JPanel(); custInputPanel.setBorder(new TitledBorder(new LineBorder(new Color(0, 0, 0), 5), "Add Customers", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 0, 0))); custInputPanel.setBackground(new Color(192, 191, 188)); custInputPanel.setBounds(12, 36, 281, 349); customerPanel.add(custInputPanel); custInputPanel.setLayout(new GridLayout(0, 2, 0, 10)); JLabel lblID_1 = new JLabel("ID");

  26. custInputPanel.add(lblID_1); custIDField = new JTextField(); custIDField.setColumns(10); custInputPanel.add(custIDField); JLabel lblFirstName = new JLabel("First Name"); custInputPanel.add(lblFirstName); firstNamefield = new JTextField(); firstNamefield.setColumns(10); custInputPanel.add(firstNamefield); JLabel lblLastName = new JLabel("Last Name"); custInputPanel.add(lblLastName); lastNameField = new JTextField(); lastNameField.setColumns(10); custInputPanel.add(lastNameField); JLabel lblEmail = new JLabel("Email"); custInputPanel.add(lblEmail); emailField = new JTextField(); emailField.setColumns(10); custInputPanel.add(emailField); JLabel phoneNumField = new JLabel("Phone no."); custInputPanel.add(phoneNumField); textField_1 = new JTextField(); textField_1.setColumns(10);

  27. custInputPanel.add(textField_1); JScrollPane customerscrollPane = new JScrollPane(); customerscrollPane.setBounds(305, 35, 500, 349); customerPanel.add(customerscrollPane); String[] custColumnName = {"ID","First Name","Last Name","Email","Phone"}; DefaultTableModel customerModel = new DefaultTableModel(custColumnName,0); customerTable = new JTable(); customerTable.setModel(customerModel); customerscrollPane.setViewportView(customerTable); JButton btnaddCustomer = new JButton("Add"); btnaddCustomer.setBackground(new Color(188, 108, 37)); btnaddCustomer.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { try { Database.addCustomer(Integer.valueOf(custIDField.getText()), firstNamefield.getText(), lastNameField.getText(), emailField.getText(), phoneNumField.getText()); Database.updateCustomers(customerModel); JOptionPane.showMessageDialog(btnaddCustomer, "Added Successfully","success" ,JOptionPane.INFORMATION_MESSAGE);

  28. } catch (NumberFormatException e1) { JOptionPane.showMessageDialog(btnaddCustomer, "Enter Valid ID","Invalid ID" ,JOptionPane.ERROR_MESSAGE); e1.printStackTrace(); } catch (SQLException e1) { JOptionPane.showMessageDialog(btnaddCustomer, "Can't add","Error" ,JOptionPane.INFORMATION_MESSAGE); e1.printStackTrace(); } } }); custInputPanel.add(btnaddCustomer); JButton btnRemoveCustomer = new JButton("Remove"); btnRemoveCustomer.setBackground(new Color(188, 108, 37)); btnRemoveCustomer.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { try { Database.delete("customers",custIDField.getText()); Database.updateCustomers(customerModel); JOptionPane.showMessageDialog(btnRemoveCustomer, "Deleted","success" ,JOptionPane.INFORMATION_MESSAGE); } catch (SQLException e1) { JOptionPane.showMessageDialog(btnRemoveCustomer, "Can't Delete","Error" ,JOptionPane.ERROR_MESSAGE);

  29. e1.printStackTrace(); } } }); custInputPanel.add(btnRemoveCustomer); JButton btnLoadCustomers = new JButton("Load All"); btnLoadCustomers.setBackground(new Color(188, 108, 37)); btnLoadCustomers.setBounds(305, 10, 100, 30); btnLoadCustomers.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { try { Database.updateCustomers(customerModel); } catch (SQLException e1) { e1.printStackTrace(); } } }); customerPanel.add(btnLoadCustomers); JPanel flightsPanel = new JPanel(); tabbedPane.addTab("Flights", null, flightsPanel, null); flightsPanel.setLayout(null); JPanel flightsInputPanel = new JPanel();

  30. flightsInputPanel.setBorder(new TitledBorder(new LineBorder(new Color(0, 0, 0), 5), "Add Flights", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 0, 0))); flightsInputPanel.setBackground(new Color(192, 191, 188)); flightsInputPanel.setBounds(0, 43, 281, 349); flightsPanel.add(flightsInputPanel); flightsInputPanel.setLayout(new GridLayout(0, 2, 0, 10)); JLabel lblID_1_1 = new JLabel("ID"); flightsInputPanel.add(lblID_1_1); flightIDField = new JTextField(); flightIDField.setColumns(10); flightsInputPanel.add(flightIDField); JLabel departure = new JLabel("Departure Time"); flightsInputPanel.add(departure); departureTimeField = new JTextField(); departureTimeField.setColumns(10); flightsInputPanel.add(departureTimeField); JLabel lblArrival = new JLabel("Arrival Time"); flightsInputPanel.add(lblArrival); arrivalTimeField = new JTextField(); arrivalTimeField.setColumns(10); flightsInputPanel.add(arrivalTimeField); JLabel lblPrice = new JLabel("Price"); flightsInputPanel.add(lblPrice);

  31. priceField = new JTextField(); priceField.setColumns(10); flightsInputPanel.add(priceField); JLabel lbldepAirportID = new JLabel("Dep Airport ID"); flightsInputPanel.add(lbldepAirportID); JComboBox<String> depAirportIDComboBox = new JComboBox<String>(); flightsInputPanel.add(depAirportIDComboBox); JLabel lblArrAirportID = new JLabel("Arr Airport ID"); flightsInputPanel.add(lblArrAirportID); JComboBox<String> arrAirportIDComboBox = new JComboBox<String>(); flightsInputPanel.add(arrAirportIDComboBox); JScrollPane flightscrollPane = new JScrollPane(); flightscrollPane.setBounds(290, 43, 500, 349); flightsPanel.add(flightscrollPane); String[] flightColumns = {"Id","Dp. Ap. ID","Arr. Ap. ID","Dp Time","Arr Time","Price","Domestic"}; DefaultTableModel flightModel = new DefaultTableModel(flightColumns,0); flightTable = new JTable(); flightTable.setModel(flightModel); flightscrollPane.setViewportView(flightTable); JLabel lblDomestic = new JLabel("Domestic"); flightsInputPanel.add(lblDomestic); JComboBox<String> arrDomesticBox = new JComboBox<String>();

  32. arrDomesticBox.addItem("Yes"); arrDomesticBox.addItem("No"); flightsInputPanel.add(arrDomesticBox); JButton btnaddFlight = new JButton("Add"); btnaddFlight.setBackground(new Color(188, 108, 37)); btnaddFlight.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { try { Database.addFlight( Integer.valueOf(flightIDField.getText()), Integer.valueOf(depAirportIDComboBox.getSelectedItem().toString()), Integer.valueOf(arrAirportIDComboBox.getSelectedItem().toString()), departureTimeField.getText(), arrivalTimeField.getText(), Float.valueOf(priceField.getText()), (String)arrDomesticBox.getSelectedItem()); Database.updateFlights(flightModel); JOptionPane.showMessageDialog(btnaddFlight, "Added Successfully","Success" ,JOptionPane.INFORMATION_MESSAGE); } catch (NumberFormatException e1) { JOptionPane.showMessageDialog(btnaddFlight, "Enter Valid ID/price","Invalid ID/price" ,JOptionPane.ERROR_MESSAGE); e1.printStackTrace(); } catch (SQLException e1) {

  33. JOptionPane.showMessageDialog(btnaddFlight, "Can't Add","Error" ,JOptionPane.ERROR_MESSAGE); e1.printStackTrace(); } } }); flightsInputPanel.add(btnaddFlight); JButton btnRemoveFlight = new JButton("Remove"); btnRemoveFlight.setBackground(new Color(188, 108, 37)); btnRemoveFlight.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { try { Database.delete("flights", flightIDField.getText()); Database.updateFlights(flightModel); JOptionPane.showMessageDialog(btnRemoveFlight, "Deleted Successfully","Success" ,JOptionPane.INFORMATION_MESSAGE); } catch (SQLException e1) { JOptionPane.showMessageDialog(btnRemoveFlight, "Can't Delete","Error" ,JOptionPane.ERROR_MESSAGE); e1.printStackTrace(); } } });

  34. flightsInputPanel.add(btnRemoveFlight); JButton btnLoadFlights = new JButton("Load All"); btnLoadFlights.setBackground(new Color(188, 108, 37)); btnLoadFlights.setBounds(290, 12, 100, 30); btnLoadFlights.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { try { Database.updateFlights(flightModel); } catch (SQLException e1) { e1.printStackTrace(); } } }); flightsPanel.add(btnLoadFlights); JPanel airportPanel = new JPanel(); tabbedPane.addTab("Airport", null, airportPanel, null); airportPanel.setLayout(null); JPanel airportInputPanel = new JPanel(); airportInputPanel.setBorder(new TitledBorder(new LineBorder(new Color(0, 0, 0), 5), "Add Airport", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 0, 0))); airportInputPanel.setBackground(new Color(192, 191, 188)); airportInputPanel.setBounds(0, 40, 281, 349);

  35. airportPanel.add(airportInputPanel); airportInputPanel.setLayout(new GridLayout(0, 2, 0, 10)); JLabel lblID_2 = new JLabel("ID"); airportInputPanel.add(lblID_2); airportIDField = new JTextField(); airportIDField.setColumns(10); airportInputPanel.add(airportIDField); JLabel lblairportName = new JLabel("Name"); airportInputPanel.add(lblairportName); airportNameField = new JTextField(); airportNameField.setColumns(10); airportInputPanel.add(airportNameField); JLabel lblCity = new JLabel("City"); airportInputPanel.add(lblCity); cityField = new JTextField(); cityField.setColumns(10); airportInputPanel.add(cityField); JLabel lblCountry = new JLabel("Country"); airportInputPanel.add(lblCountry); countryField = new JTextField(); countryField.setColumns(10); airportInputPanel.add(countryField); JScrollPane airportscrollPane = new JScrollPane();

  36. airportscrollPane.setBounds(290, 40, 500, 349); airportPanel.add(airportscrollPane); String[] apModelColumns = {"ID","Name","City","Country"}; DefaultTableModel airportTableModel = new DefaultTableModel(apModelColumns,0); airportsTable = new JTable(); airportscrollPane.setViewportView(airportsTable); airportsTable.setModel(airportTableModel); JButton btnaddAirport = new JButton("Add"); btnaddAirport.setBackground(new Color(188, 108, 37)); btnaddAirport.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { try { Database.addAirport(Integer.valueOf(airportIDField.getText()), airportNameField.getText(), cityField.getText(), countryField.getText()); Database.updateAirports(airportTableModel); JOptionPane.showMessageDialog(btnaddAirport, "Added Successfully","Success" ,JOptionPane.INFORMATION_MESSAGE); } catch (NumberFormatException e1) { JOptionPane.showMessageDialog(btnaddAirport, "Enter Valid ID","Invalid ID" ,JOptionPane.ERROR_MESSAGE); e1.printStackTrace();

  37. } catch (SQLException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } }); airportInputPanel.add(btnaddAirport); JButton btnRemoveAirport = new JButton("Remove"); btnRemoveAirport.setBackground(new Color(188, 108, 37)); btnRemoveAirport.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { try { Database.delete("airports", airportIDField.getText()); Database.updateAirports(airportTableModel); JOptionPane.showMessageDialog(btnRemoveAirport, "Successfully Deleted","Success",JOptionPane.INFORMATION_MESSAGE); } catch (SQLException e1) { JOptionPane.showMessageDialog(btnRemoveAirport, "Can't Delete","Error",JOptionPane.ERROR_MESSAGE); e1.printStackTrace(); } } });

  38. airportInputPanel.add(btnRemoveAirport); JButton btnLoadAirports = new JButton("Load All"); btnLoadAirports.setBackground(new Color(188, 108, 37)); btnLoadAirports.setBounds(289, 12, 100, 30); btnLoadAirports.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { try { Database.updateAirports(airportTableModel); } catch (SQLException e1) { e1.printStackTrace(); } } }); airportPanel.add(btnLoadAirports); try { Database.updateBox(arrAirportIDComboBox, "airports"); Database.updateBox(depAirportIDComboBox, "airports"); Database.updateBox(flightComboBox, "flights","yes"); Database.updateBox(custComboBox, "customers"); } catch (SQLException e1) { e1.printStackTrace(); }

  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.

More Related