Java Airline Reservation System – Travel Smarter, Not Harder.pdf

SudhanshiBakre1 265 views 39 slides Aug 19, 2023
Slide 1
Slide 1 of 39
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39

About This Presentation

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


Slide Content

JavaAirlineReservationSystem
–TravelSmarter,NotHarder
WelcometothisprojectondevelopinganAirlineReservationSystemusingJava
andSQLite!Thissystemwillconsistoffourpanels–Reservations,Customers,
Flights,andAirports–eachofwhichwillhaveaJTabletodisplaydatafromthe
databaseandaninputpaneltotakeinputfromtheuserandstoreitintothe
database.
Inthisproject,wewillcoverthestep-by-stepprocessofdesigningandimplementing
thissystem,includingsettingupthedatabase,creatingtheuserinterface,and
integratingthefunctionalitytoperformvariousoperationssuchasaddingnew
reservations,customers,flights,andairports,aswellasupdatinganddeleting
existingdata.
Internationalflightsarethosethatoperatebetweentwoormorecountries,typically
crossinginternationalboundaries.Theseflightsusuallyinvolvelongerdistances.On
theotherhand,domesticflightsoperatewithinasinglecountry.Domesticflightsare
typicallyshorterindurationanddonotrequirepassengerstogothroughcustomsor
immigrationprocedures.
Forthiswewillbecreatingatogglebuttonwhichwillallowtheuserstoswitch
betweeninternationalanddomesticflightsintheReservationspanel.Thisbuttonwill
updateourflightchoicestoreflecttheoptionweselectedusingthetogglebutton.
Bytheendofthisproject,youwillhaveaworkingAirlineReservationSystemthat
canefficientlymanageandstorecustomerinformation,flightschedules,and

reservations,makingitaninvaluabletoolforanyairlineortravelagency.Solet’sget
started!
AboutJavaAirlineReservationSystem
Theobjectiveofthisprojectistoprovideacomprehensiveguidetodevelopingan
AirlineReservationSystemusingJavaandSQLite.
Bytheendofthisproject,youwillbeabletodesignandimplementasystemthat
canefficientlymanageandstorecustomerinformation,flightschedules,and
reservations.Specifically,youwilllearnhowtosetupthedatabase,createtheuser
interfacewithJTables,andintegratethefunctionalitytoperformvariousoperations
suchasadding,updating,anddeletingdata.
Thisprojectaimstoequipyouwiththeskillsandknowledgenecessarytodevelopa
fully-functionalAirlineReservationSystem.
PrerequisitesforAirlineReservation
SystemusingJava
Beforestartingwiththistutorial,youshouldhavethefollowingprerequisites:
■BasicknowledgeofJavaprogramminglanguage,including
object-orientedprogrammingconcepts.
■UnderstandingofSQLanddatabaseconcepts.
■ExperiencewithEclipseoranyotherJavaIntegratedDevelopment
Environment(IDE).
■BasicknowledgeofSwingGUItoolkitforbuildinguserinterfaces.
■FamiliaritywithSQLitedatabasemanagementsystem.

DownloadJavaAirlineReservation
SystemProject
PleasedownloadthesourcecodeofJavaAirlineReservationSystemProjectfrom
thefollowinglink:JavaAirlineReservationSystemProjectCode
StepstoCreateAirlineReservation
SystemusingJava
FollowingarethestepsfordevelopingtheJavaAirlineReservationSystemProject:
Step1:SettingUptheClassesinEclipse
1.LaunchEclipseandgoto“File”>“New”>“JavaProject”.
2.Assignaname,suchas“AirlineReservationSystem”.
3.Right-clickontheprojectandselect“New”>“Class”.
4.Namethefirstclass,forexample,“AirlineReservation”.
5.Repeatsteps3and4tocreateanotherclass,suchas“Database”.
Step2:AddingSQLitetotheProjectinEclipse
Note:Beforemovingforward,makesuretohavetheSQLiteJARfiledownloaded
andsavedonyourcomputer.ThisfilecanbefoundonMavenRepository
1.GototheProjectExplorerinEclipseandright-clickonyourproject.
2.Select“Properties”fromthemenu.
3.InthePropertieswindow,navigateto“JavaBuildPath”andclickthe
“Libraries”tab.
4.Clickthe“AddExternalJARs”buttonandfindtheSQLiteJARfile.
5.ChoosetheJARfileandpress“Open”.
6.Click“OK”toclosethePropertieswindow.

IncorporatingSQLiteintoyourEclipseprojectisnowcompleteafterfollowingthe
stepsoutlinedabove.
Step3:ImplementingtheDatabaseclasswiththe
requiredmethods
BelowisthecompletecodefortheDatabaseclass:
packageschool.FirstCode;
importjava.sql.Connection;
importjava.sql.PreparedStatement;
importjava.sql.ResultSet;
importjava.sql.SQLException;
importjava.sql.Statement;
importjavax.swing.JComboBox;
importjavax.swing.table.DefaultTableModel;
importorg.sqlite.SQLiteDataSource;
publicclassDatabase{
//declaringconnectionanddatasourcevariables
staticConnectionconn;
staticSQLiteDataSource ds;
//initializemethodtoinitializethedatabasewithallthetables
publicstaticvoiddbInit(){
ds=newSQLiteDataSource();
try{

ds=newSQLiteDataSource();
ds.setUrl("jdbc:sqlite:AirlineDB.db" );
}catch(Exceptione){
e.printStackTrace();
System.exit(0);
}
try{
conn=ds.getConnection();
Statementstatement=conn.createStatement();
//Createtheairportstable
statement.executeUpdate("CREATETABLEIFNOTEXISTSairports(\n"
+"idINTEGERPRIMARYKEY,\n"
+"nameTEXTNOTNULL,\n"
+"cityTEXTNOTNULL,\n"
+"countryTEXTNOTNULL\n"
+");");
//Createtheflightstable
statement.executeUpdate("CREATETABLEIFNOTEXISTSflights(\n"
+"idINTEGERPRIMARYKEY,\n"
+"departure_airport_id INTEGERNOTNULL,\n"
+"arrival_airport_id INTEGERNOTNULL,\n"
+"departure_time TEXTNOTNULL,\n"
+"arrival_timeTEXTNOTNULL,\n"

+"priceDECIMAL(10,2)NOTNULL,\n"
+"domesticTEXTNOTNULLCHECK(domesticIN('yes','no')),\n"
+"CONSTRAINTfk_departure_airport\n"
+"FOREIGNKEY(departure_airport_id)\n"
+"REFERENCESairports(id),\n"
+"CONSTRAINTfk_arrival_airport\n"
+"FOREIGNKEY(arrival_airport_id)\n"
+"REFERENCESairports(id)\n"
+");\n");
//Createthecustomerstable
statement.executeUpdate("CREATETABLEIFNOTEXISTScustomers(\n"
+"idINTEGERPRIMARYKEY,\n"
+"first_nameTEXTNOTNULL,\n"
+"last_nameTEXTNOTNULL,\n"
+"emailTEXTNOTNULL,\n"
+"phone_numberTEXT\n"
+");");
//Createthecustomerstable
statement.executeUpdate("CREATETABLEIFNOTEXISTSreservations(\n"
+"idINTEGERPRIMARYKEY,\n"
+"customer_idINTEGERNOTNULL,\n"
+"flight_idINTEGERNOTNULL,\n"
+"seat_numberTEXTNOTNULL,\n"

+"CONSTRAINTfk_customer\n"
+"FOREIGNKEY(customer_id)\n"
+"REFERENCEScustomers(id),\n"
+"CONSTRAINTfk_flight\n"
+"FOREIGNKEY(flight_id)\n"
+"REFERENCESflights(id)\n"
+");");
//Closingstatementandconnection
statement.close();
conn.close();
}catch(SQLExceptione){
e.printStackTrace();
System.exit(0);
}
finally{
try{
if(conn!=null){
conn.close();
}
}catch(SQLExceptione){
System.err.println(e);
}
}

}
//Methodtogetupdatethereservationtablewiththedatafromthedatabase
publicstaticvoidupdateReservations(DefaultTableModel model)throwsSQLException{
model.setRowCount(0);
conn=ds.getConnection();
Stringquery="SELECT*Fromreservations";
PreparedStatement ps=conn.prepareStatement(query);
ResultSetrs=ps.executeQuery();
//Adddatatothetablemodel
while(rs.next()){
intid=rs.getInt("id");
Stringcustomer_id=rs.getString("customer_id");
Stringflight_id=rs.getString("flight_id");
Stringseat_number=rs.getString("seat_number");
Object[]row={id,customer_id,flight_id,seat_number };
model.addRow(row);
}
rs.close();
ps.close();
conn.close();
}
//MethodtogetupdatetheFlightstablewiththedatafromthedatabase
publicstaticvoidupdateFlights(DefaultTableModel model)throwsSQLException{

model.setRowCount(0);
conn=ds.getConnection();
Stringquery="SELECT*Fromflights";
PreparedStatement ps=conn.prepareStatement(query);
ResultSetrs=ps.executeQuery();
//Adddatatothetablemodel
while(rs.next()){
intid=rs.getInt("id");
intdeparture_airport =rs.getInt("departure_airport_id" );
intarrival_airport =rs.getInt("arrival_airport_id" );
Stringdeparture_time =rs.getString("departure_time");
Stringarrival_time=rs.getString("arrival_time");
Floatprice=rs.getFloat("price");
Stringdomestic=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();
}
//MethodtogetupdatetheCustomerstablewiththedatafromthedatabase

publicstaticvoidupdateCustomers(DefaultTableModel model)throwsSQLException{
model.setRowCount(0);
conn=ds.getConnection();
Stringquery="SELECT*Fromcustomers";
PreparedStatement ps=conn.prepareStatement(query);
ResultSetrs=ps.executeQuery();
//Adddatatothetablemodel
while(rs.next()){
intid=rs.getInt("id");
Stringfirst_name=rs.getString("first_name");
Stringlast_name=rs.getString("last_name");
Stringemail=rs.getString("email");
Stringphone_number=rs.getString("phone_number");
Object[]row={id,first_name,last_name,email,phone_number };
model.addRow(row);
}
rs.close();
ps.close();
conn.close();
}
//MethodtogetupdatetheAirportstablewiththedatafromthedatabase
publicstaticvoidupdateAirports(DefaultTableModel model)throwsSQLException{
model.setRowCount(0);

conn=ds.getConnection();
Stringquery="SELECT*Fromairports";
PreparedStatement ps=conn.prepareStatement(query);
ResultSetrs=ps.executeQuery();
//Adddatatothetablemodel
while(rs.next()){
intid=rs.getInt("id");
Stringname=rs.getString("name");
Stringcity=rs.getString("city");
Stringcountry=rs.getString("country");
Object[]row={id,name,city,country};
model.addRow(row);
}
rs.close();
ps.close();
conn.close();
}
//Methodtodeleteanspecificitemfromaspecifictable
publicstaticvoiddelete(StringtableName,String id)throwsSQLException{
conn=ds.getConnection();
Stringquery="DELETEFROM"+tableName+"WHEREid="+id+";";
Statementstmt=conn.createStatement();
stmt.execute(query);

stmt.close();
conn.close();
}
//MethodtoupdatejcomboBoxeswiththerequiredfieldvalues
publicstaticvoidupdateBox(JComboBox<String>cbx,StringtableName,String
domestic_status)throwsSQLException{
cbx.removeAllItems();
conn=ds.getConnection();
Stringquery="SELECTidFROM"+tableName+"WHEREdomestic='"+domestic_status+"';";
PreparedStatement ps=conn.prepareStatement(query);
ResultSetrs=ps.executeQuery();
while(rs.next()){
cbx.addItem(rs.getString("id"));
}
ps.close();
conn.close();
}
publicstaticvoidupdateBox(JComboBox<String>cbx,StringtableName)throws
SQLException{
cbx.removeAllItems();
conn=ds.getConnection();
Stringquery="SELECTidFROM"+tableName+";";
PreparedStatement ps=conn.prepareStatement(query);
ResultSetrs=ps.executeQuery();

while(rs.next()){
cbx.addItem(rs.getString("id"));
}
ps.close();
conn.close();
}
//Methodtoaddthereservationsdataintothereservationstable
publicstaticvoidaddReservation(intid,intcustID,intflightID,String seatNum)
throwsSQLException{
conn=ds.getConnection();
PreparedStatement ps=conn.prepareStatement("INSERTINTO"
+"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();
}
//MethodtoaddtheCustomerdataintotheCustomerstable
publicstaticvoidaddCustomer(intid,Stringfirst_name,String last_name,String
email,Stringphone)throwsSQLException{

conn=ds.getConnection();
PreparedStatement ps=conn.prepareStatement("INSERTINTO"
+"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();
}
//MethodtoaddtheFlightdataintotheFlightstable
publicstaticvoidaddFlight(intid,intdeparture_ap_id,int arrival_ap_id,String
departure_time,String arrival_time,float price,Stringdomestic)throwsSQLException{
conn=ds.getConnection();
PreparedStatement ps=conn.prepareStatement("INSERTINTO"
+
"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);

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();
}
//MethodtoaddtheAirportdataintotheAirportstable
publicstaticvoidaddAirport(intid,Stringname,Stringcity,Stringcountry)throws
SQLException{
conn=ds.getConnection();
PreparedStatement ps=conn.prepareStatement("INSERTINTO"
+"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();

}
}
■dbInit()–Thisfunctioninitializesthedatabasebycreatingtablesfor
airports,flights,customers,andreservationsusingSQLitequeries.
■updateReservations(DefaultTableModelmodel)–Thisfunctionupdates
thereservationstablefromthedatabaseandsetsthedatainthegiven
tablemodel.Itretrievesallthedatafromthereservationstableandadds
themtothetablemodel.
■updateFlights(DefaultTableModelmodel)–Thisfunctionupdatesthe
flightstablefromthedatabaseandsetsthedatainthegiventablemodel.
Itretrievesallthedatafromtheflightstableandaddsthemtothetable
model.
■updateCustomers(DefaultTableModelmodel)–Thisfunctionupdatesthe
customerstablefromthedatabaseandsetsthedatainthegiventable
model.Itretrievesallthedatafromthecustomerstableandaddsthemto
thetablemodel.
■updateAirports(DefaultTableModelmodel)–Thisfunctionupdatesthe
airportstablefromthedatabaseandsetsthedatainthegivenmodel.It
retrievesallthedatafromtheAirporttableandaddsthemtothetable
model.
Similarly,therearefunctionstoadddatatothedatabaselike
addFlight,addCustomer,etc.Thenthere’sadeletefunctionthatdeletethedatafrom
thedatabasebyusingtheidfieldandthetable’snameandlastly,theupdateBox
functionupdatesthecomboboxeswithrelevantdata.
Allthesefunctionsconnecttothedatabaseusingtheds(SQLiteDataSource)object
andexecuteSQLqueriestofetchdatafromthetables.Theyalsousethe
PreparedStatementobjecttoexecuteSQLquerieswithparameters.Oncethedata
isfetchedfromthetables,itisaddedtotheDefaultTableModelobjectthatispassed
asaparametertothesefunctions.Finally,theconnectiontothedatabaseisclosed
afterexecutingthequeries.
Step3:ImplementingtheAirlineReservation

BelowisthecompletecodefortheAirlineReservationclass:
packageschool.FirstCode;
importjava.awt.EventQueue;
importjavax.swing.JFrame;
importjavax.swing.JTabbedPane;
importjava.awt.BorderLayout;
importjavax.swing.JPanel;
importjava.awt.Color;
importjavax.swing.border.TitledBorder;
importjavax.swing.table.DefaultTableModel;
importjavax.swing.border.LineBorder;
importjava.awt.GridLayout;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.sql.SQLException;
importjavax.swing.JLabel;
importjavax.swing.JOptionPane;
importjavax.swing.JTextField;
importjavax.swing.JComboBox;
importjavax.swing.JButton;
importjavax.swing.JScrollPane;
importjavax.swing.JTable;

importjavax.swing.JToggleButton;
publicclassAirlineReservation {
privateJFramefrmAirlineReservatoinSystem;
privateJTextFieldresIDField;
privateJTextFieldseatField;
privateJTablereservationsTable;
privateJTextFieldcustIDField;
privateJTextFieldfirstNamefield;
privateJTablecustomerTable;
privateJTextFieldlastNameField;
privateJTextFieldemailField;
privateJTextFieldtextField_1;
privateJTextFieldairportIDField;
privateJTextFieldairportNameField;
privateJTextFieldcityField;
privateJTextFieldcountryField;
privateJTableairportsTable;
privateJTextFieldflightIDField;
privateJTextFielddepartureTimeField;
privateJTextFieldarrivalTimeField;
privateJTextFieldpriceField;
privateJTableflightTable;
/**

*Launchtheapplication.
*/
publicstaticvoidmain(String[]args){
EventQueue.invokeLater(newRunnable(){
publicvoidrun(){
try{
Database.dbInit();
AirlineReservation window=newAirlineReservation();
window.frmAirlineReservatoinSystem .setVisible(true);
}catch(Exceptione){
e.printStackTrace();
}
}
});
}
/**
*Createtheapplication.
*/
publicAirlineReservation(){
initialize();
}
/**
*Initializethecontentsoftheframe.

*/
privatevoidinitialize(){
frmAirlineReservatoinSystem =newJFrame();
frmAirlineReservatoinSystem. setTitle("AirlineReservationSystem");
frmAirlineReservatoinSystem. setBounds(100,100,800,500);
frmAirlineReservatoinSystem. setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frmAirlineReservatoinSystem. getContentPane().setLayout(newBorderLayout(0,0));
frmAirlineReservatoinSystem. setResizable(false);
JTabbedPanetabbedPane=newJTabbedPane(JTabbedPane.TOP);
frmAirlineReservatoinSystem. getContentPane().add(tabbedPane);
JPanelreservationsPanel =newJPanel();
tabbedPane.addTab("Reservations",null,reservationsPanel, null);
reservationsPanel.setLayout(null);
JPanelresInputPanel=newJPanel();
resInputPanel.setBounds(5,53,281,349);
resInputPanel.setBorder(newTitledBorder(newLineBorder(newColor(0,0,0),5),"Add
Reservation",TitledBorder.LEADING,TitledBorder.TOP,null,newColor(0,0,0)));
resInputPanel.setBackground(newColor(192,191,188));
reservationsPanel.add(resInputPanel);
resInputPanel.setLayout(newGridLayout(0,2,0,10));
JLabellblID=newJLabel("ID");
resInputPanel.add(lblID);
resIDField=newJTextField();

resIDField.setColumns(10);
resInputPanel.add(resIDField);
JLabellblSeatNo=newJLabel("SeatNumber");
resInputPanel.add(lblSeatNo);
seatField=newJTextField();
seatField.setColumns(10);
resInputPanel.add(seatField);
JLabellblflightID=newJLabel("FlightID");
resInputPanel.add(lblflightID);
JComboBox<String>flightComboBox =newJComboBox<String>();
resInputPanel.add(flightComboBox);
JLabellblCustID=newJLabel("CustomerID");
resInputPanel.add(lblCustID);
JComboBox<String>custComboBox=newJComboBox<String>();
resInputPanel.add(custComboBox);
JScrollPanereservationscrollPane =newJScrollPane();
reservationscrollPane. setBounds(295,53,500,349);
reservationsPanel.add(reservationscrollPane );
String[]reservationColumsNames ={"ID","CustomerID","FlightID","SeatNo."};
DefaultTableModel reservationsModel =newDefaultTableModel(reservationColumsNames, 0);
reservationsTable =newJTable();
reservationsTable.setModel(reservationsModel);
reservationscrollPane. setViewportView(reservationsTable);

JButtonbtnLoadReservations =newJButton("Load");
btnLoadReservations. setBackground(newColor(188,108,37));
btnLoadReservations. setBounds(295,25,100,30);
btnLoadReservations. addActionListener(newActionListener(){
@Override
publicvoidactionPerformed(ActionEvente){
try{
Database.updateReservations(reservationsModel);
}catch(SQLExceptione1){
e1.printStackTrace();
}
}
});
reservationsPanel.add(btnLoadReservations);
JToggleButtontglbtnDomesticFlights =newJToggleButton("DomesticFlights",true);
tglbtnDomesticFlights. setBackground(newColor(188,108,37));
tglbtnDomesticFlights. setBounds(400,25,200,30);
tglbtnDomesticFlights. addActionListener(newActionListener(){
@Override
publicvoidactionPerformed(ActionEvente){
if(tglbtnDomesticFlights. isSelected()){
tglbtnDomesticFlights. setText("DomesticFlights");
try{

Database.updateBox(flightComboBox,"flights","yes");
}catch(SQLExceptione1){
//TODOAuto-generated catchblock
e1.printStackTrace();
}
}else{
tglbtnDomesticFlights. setText("International Flights");
try{
Database.updateBox(flightComboBox,"flights","no");
}catch(SQLExceptione1){
//TODOAuto-generated catchblock
e1.printStackTrace();
}
}
}
});
reservationsPanel.add(tglbtnDomesticFlights );
JButtonbtnaddReservation =newJButton("Add");
btnaddReservation.setBackground(newColor(188,108,37));
btnaddReservation.addActionListener(newActionListener(){
@Override
publicvoidactionPerformed(ActionEvente){
try{

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, "EnterValidID","Invalid
ID",JOptionPane.INFORMATION_MESSAGE);
e1.printStackTrace();
}catch(SQLExceptione1){
JOptionPane.showMessageDialog(btnaddReservation, "Can't
Add","Error",JOptionPane.ERROR_MESSAGE);
e1.printStackTrace();
}
}
});
resInputPanel.add(btnaddReservation);
JButtonbtnRemoveReservation =newJButton("Remove");
btnRemoveReservation. setBackground(newColor(188,108,37));
btnRemoveReservation. addActionListener(newActionListener(){
@Override
publicvoidactionPerformed(ActionEvente){

try{
Database.delete("reservations",resIDField.getText());
Database.updateReservations(reservationsModel);
JOptionPane.showMessageDialog(btnRemoveReservation, "Deleted","success"
,JOptionPane.INFORMATION_MESSAGE);
}catch(SQLExceptione1){
JOptionPane.showMessageDialog(btnRemoveReservation, "Can'tDelete","Error"
,JOptionPane.ERROR_MESSAGE);
e1.printStackTrace();
}
}
});
resInputPanel.add(btnRemoveReservation );
JPanelcustomerPanel=newJPanel();
tabbedPane.addTab("Customers",null,customerPanel, null);
customerPanel.setLayout(null);
JPanelcustInputPanel =newJPanel();
custInputPanel.setBorder(newTitledBorder(newLineBorder(newColor(0,0,0),5),"Add
Customers",TitledBorder.LEADING,TitledBorder.TOP,null,newColor(0,0,0)));
custInputPanel.setBackground(newColor(192,191,188));
custInputPanel.setBounds(12,36,281,349);
customerPanel.add(custInputPanel);
custInputPanel.setLayout(newGridLayout(0,2,0,10));
JLabellblID_1=newJLabel("ID");

custInputPanel.add(lblID_1);
custIDField=newJTextField();
custIDField.setColumns(10);
custInputPanel.add(custIDField);
JLabellblFirstName=newJLabel("FirstName");
custInputPanel.add(lblFirstName);
firstNamefield =newJTextField();
firstNamefield.setColumns(10);
custInputPanel.add(firstNamefield);
JLabellblLastName=newJLabel("LastName");
custInputPanel.add(lblLastName);
lastNameField=newJTextField();
lastNameField.setColumns(10);
custInputPanel.add(lastNameField);
JLabellblEmail=newJLabel("Email");
custInputPanel.add(lblEmail);
emailField=newJTextField();
emailField.setColumns(10);
custInputPanel.add(emailField);
JLabelphoneNumField=newJLabel("Phoneno.");
custInputPanel.add(phoneNumField);
textField_1=newJTextField();
textField_1.setColumns(10);

custInputPanel.add(textField_1);
JScrollPanecustomerscrollPane =newJScrollPane();
customerscrollPane.setBounds(305,35,500,349);
customerPanel.add(customerscrollPane);
String[]custColumnName ={"ID","FirstName","LastName","Email","Phone"};
DefaultTableModel customerModel=newDefaultTableModel(custColumnName,0);
customerTable=newJTable();
customerTable.setModel(customerModel);
customerscrollPane.setViewportView(customerTable);
JButtonbtnaddCustomer =newJButton("Add");
btnaddCustomer.setBackground(newColor(188,108,37));
btnaddCustomer.addActionListener(newActionListener(){
@Override
publicvoidactionPerformed(ActionEvente){
try{
Database.addCustomer(Integer.valueOf(custIDField.getText()),
firstNamefield.getText(),
lastNameField.getText(),
emailField.getText(),
phoneNumField.getText());
Database.updateCustomers(customerModel);
JOptionPane.showMessageDialog(btnaddCustomer, "AddedSuccessfully","success"
,JOptionPane.INFORMATION_MESSAGE);

}catch(NumberFormatException e1){
JOptionPane.showMessageDialog(btnaddCustomer, "EnterValidID","InvalidID"
,JOptionPane.ERROR_MESSAGE);
e1.printStackTrace();
}catch(SQLExceptione1){
JOptionPane.showMessageDialog(btnaddCustomer, "Can'tadd","Error"
,JOptionPane.INFORMATION_MESSAGE);
e1.printStackTrace();
}
}
});
custInputPanel.add(btnaddCustomer);
JButtonbtnRemoveCustomer =newJButton("Remove");
btnRemoveCustomer.setBackground(newColor(188,108,37));
btnRemoveCustomer.addActionListener(newActionListener(){
@Override
publicvoidactionPerformed(ActionEvente){
try{
Database.delete("customers",custIDField.getText());
Database.updateCustomers(customerModel);
JOptionPane.showMessageDialog(btnRemoveCustomer, "Deleted","success"
,JOptionPane.INFORMATION_MESSAGE);
}catch(SQLExceptione1){
JOptionPane.showMessageDialog(btnRemoveCustomer, "Can'tDelete","Error"
,JOptionPane.ERROR_MESSAGE);

e1.printStackTrace();
}
}
});
custInputPanel.add(btnRemoveCustomer);
JButtonbtnLoadCustomers =newJButton("LoadAll");
btnLoadCustomers.setBackground(newColor(188,108,37));
btnLoadCustomers.setBounds(305,10,100,30);
btnLoadCustomers.addActionListener(newActionListener(){
@Override
publicvoidactionPerformed(ActionEvente){
try{
Database.updateCustomers(customerModel);
}catch(SQLExceptione1){
e1.printStackTrace();
}
}
});
customerPanel.add(btnLoadCustomers);
JPanelflightsPanel=newJPanel();
tabbedPane.addTab("Flights",null,flightsPanel,null);
flightsPanel.setLayout(null);
JPanelflightsInputPanel =newJPanel();

flightsInputPanel.setBorder(newTitledBorder(newLineBorder(newColor(0,0,0),5),
"AddFlights",TitledBorder.LEADING,TitledBorder.TOP,null,newColor(0,0,0)));
flightsInputPanel.setBackground(newColor(192,191,188));
flightsInputPanel.setBounds(0,43,281,349);
flightsPanel.add(flightsInputPanel);
flightsInputPanel.setLayout(newGridLayout(0,2,0,10));
JLabellblID_1_1=newJLabel("ID");
flightsInputPanel.add(lblID_1_1);
flightIDField=newJTextField();
flightIDField.setColumns(10);
flightsInputPanel.add(flightIDField);
JLabeldeparture=newJLabel("DepartureTime");
flightsInputPanel.add(departure);
departureTimeField =newJTextField();
departureTimeField.setColumns(10);
flightsInputPanel.add(departureTimeField);
JLabellblArrival=newJLabel("ArrivalTime");
flightsInputPanel.add(lblArrival);
arrivalTimeField =newJTextField();
arrivalTimeField.setColumns(10);
flightsInputPanel.add(arrivalTimeField);
JLabellblPrice=newJLabel("Price");
flightsInputPanel.add(lblPrice);

priceField=newJTextField();
priceField.setColumns(10);
flightsInputPanel.add(priceField);
JLabellbldepAirportID =newJLabel("DepAirportID");
flightsInputPanel.add(lbldepAirportID);
JComboBox<String>depAirportIDComboBox =newJComboBox<String>();
flightsInputPanel.add(depAirportIDComboBox );
JLabellblArrAirportID =newJLabel("ArrAirportID");
flightsInputPanel.add(lblArrAirportID);
JComboBox<String>arrAirportIDComboBox =newJComboBox<String>();
flightsInputPanel.add(arrAirportIDComboBox );
JScrollPaneflightscrollPane =newJScrollPane();
flightscrollPane.setBounds(290,43,500,349);
flightsPanel.add(flightscrollPane);
String[]flightColumns={"Id","Dp.Ap.ID","Arr.Ap.ID","DpTime","Arr
Time","Price","Domestic"};
DefaultTableModel flightModel=newDefaultTableModel(flightColumns,0);
flightTable=newJTable();
flightTable.setModel(flightModel);
flightscrollPane.setViewportView(flightTable);
JLabellblDomestic=newJLabel("Domestic");
flightsInputPanel.add(lblDomestic);
JComboBox<String>arrDomesticBox =newJComboBox<String>();

arrDomesticBox.addItem("Yes");
arrDomesticBox.addItem("No");
flightsInputPanel.add(arrDomesticBox);
JButtonbtnaddFlight=newJButton("Add");
btnaddFlight.setBackground(newColor(188,108,37));
btnaddFlight.addActionListener(newActionListener(){
@Override
publicvoidactionPerformed(ActionEvente){
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,"AddedSuccessfully","Success"
,JOptionPane.INFORMATION_MESSAGE);
}catch(NumberFormatException e1){
JOptionPane.showMessageDialog(btnaddFlight,"EnterValidID/price","InvalidID/price"
,JOptionPane.ERROR_MESSAGE);
e1.printStackTrace();
}catch(SQLExceptione1){

JOptionPane.showMessageDialog(btnaddFlight,"Can'tAdd","Error"
,JOptionPane.ERROR_MESSAGE);
e1.printStackTrace();
}
}
});
flightsInputPanel.add(btnaddFlight);
JButtonbtnRemoveFlight =newJButton("Remove");
btnRemoveFlight.setBackground(newColor(188,108,37));
btnRemoveFlight.addActionListener(newActionListener(){
@Override
publicvoidactionPerformed(ActionEvente){
try{
Database.delete("flights",flightIDField.getText());
Database.updateFlights(flightModel);
JOptionPane.showMessageDialog(btnRemoveFlight, "DeletedSuccessfully","Success"
,JOptionPane.INFORMATION_MESSAGE);
}catch(SQLExceptione1){
JOptionPane.showMessageDialog(btnRemoveFlight, "Can'tDelete","Error"
,JOptionPane.ERROR_MESSAGE);
e1.printStackTrace();
}
}
});

flightsInputPanel.add(btnRemoveFlight);
JButtonbtnLoadFlights =newJButton("LoadAll");
btnLoadFlights.setBackground(newColor(188,108,37));
btnLoadFlights.setBounds(290,12,100,30);
btnLoadFlights.addActionListener(newActionListener(){
@Override
publicvoidactionPerformed(ActionEvente){
try{
Database.updateFlights(flightModel);
}catch(SQLExceptione1){
e1.printStackTrace();
}
}
});
flightsPanel.add(btnLoadFlights);
JPanelairportPanel=newJPanel();
tabbedPane.addTab("Airport",null,airportPanel,null);
airportPanel.setLayout(null);
JPanelairportInputPanel =newJPanel();
airportInputPanel.setBorder(newTitledBorder(newLineBorder(newColor(0,0,0),5),
"AddAirport",TitledBorder.LEADING,TitledBorder.TOP,null,newColor(0,0,0)));
airportInputPanel.setBackground(newColor(192,191,188));
airportInputPanel.setBounds(0,40,281,349);

airportPanel.add(airportInputPanel);
airportInputPanel.setLayout(newGridLayout(0,2,0,10));
JLabellblID_2=newJLabel("ID");
airportInputPanel.add(lblID_2);
airportIDField =newJTextField();
airportIDField.setColumns(10);
airportInputPanel.add(airportIDField);
JLabellblairportName =newJLabel("Name");
airportInputPanel.add(lblairportName);
airportNameField =newJTextField();
airportNameField.setColumns(10);
airportInputPanel.add(airportNameField);
JLabellblCity=newJLabel("City");
airportInputPanel.add(lblCity);
cityField=newJTextField();
cityField.setColumns(10);
airportInputPanel.add(cityField);
JLabellblCountry=newJLabel("Country");
airportInputPanel.add(lblCountry);
countryField=newJTextField();
countryField.setColumns(10);
airportInputPanel.add(countryField);
JScrollPaneairportscrollPane =newJScrollPane();

airportscrollPane.setBounds(290,40,500,349);
airportPanel.add(airportscrollPane);
String[]apModelColumns ={"ID","Name","City","Country"};
DefaultTableModel airportTableModel =newDefaultTableModel(apModelColumns,0);
airportsTable=newJTable();
airportscrollPane.setViewportView(airportsTable);
airportsTable.setModel(airportTableModel);
JButtonbtnaddAirport=newJButton("Add");
btnaddAirport.setBackground(newColor(188,108,37));
btnaddAirport.addActionListener(newActionListener(){
@Override
publicvoidactionPerformed(ActionEvente){
try{
Database.addAirport(Integer.valueOf(airportIDField.getText()),
airportNameField.getText(),
cityField.getText(),
countryField.getText());
Database.updateAirports(airportTableModel);
JOptionPane.showMessageDialog(btnaddAirport, "AddedSuccessfully","Success"
,JOptionPane.INFORMATION_MESSAGE);
}catch(NumberFormatException e1){
JOptionPane.showMessageDialog(btnaddAirport, "EnterValidID","InvalidID"
,JOptionPane.ERROR_MESSAGE);
e1.printStackTrace();

}catch(SQLExceptione1){
//TODOAuto-generated catchblock
e1.printStackTrace();
}
}
});
airportInputPanel.add(btnaddAirport);
JButtonbtnRemoveAirport =newJButton("Remove");
btnRemoveAirport.setBackground(newColor(188,108,37));
btnRemoveAirport.addActionListener(newActionListener(){
@Override
publicvoidactionPerformed(ActionEvente){
try{
Database.delete("airports",airportIDField.getText());
Database.updateAirports(airportTableModel);
JOptionPane.showMessageDialog(btnRemoveAirport, "Successfully
Deleted","Success",JOptionPane.INFORMATION_MESSAGE);
}catch(SQLExceptione1){
JOptionPane.showMessageDialog(btnRemoveAirport, "Can't
Delete","Error",JOptionPane.ERROR_MESSAGE);
e1.printStackTrace();
}
}
});

airportInputPanel.add(btnRemoveAirport);
JButtonbtnLoadAirports =newJButton("LoadAll");
btnLoadAirports.setBackground(newColor(188,108,37));
btnLoadAirports.setBounds(289,12,100,30);
btnLoadAirports.addActionListener(newActionListener(){
@Override
publicvoidactionPerformed(ActionEvente){
try{
Database.updateAirports(airportTableModel);
}catch(SQLExceptione1){
e1.printStackTrace();
}
}
});
airportPanel.add(btnLoadAirports);
try{
Database.updateBox(arrAirportIDComboBox, "airports");
Database.updateBox(depAirportIDComboBox, "airports");
Database.updateBox(flightComboBox, "flights","yes");
Database.updateBox(custComboBox,"customers");
}catch(SQLExceptione1){
e1.printStackTrace();
}

}
}
Summary
ThisprojectdemonstratestheuseofSQLiteandJDBCtocreateandmanagea
databaseforanairlinereservationsystem.Theprojectcoversthecreationofa
database,creatingtablesandestablishingrelationshipsbetweenthem,addingdata
tothedatabase,andqueryingthedatabasetoretrievedata.
Thecodeincludesmethodstoupdatetablesinthedatabaseandretrievedatafrom
them,whicharedemonstratedthroughthecreationofaGUIthatallowstheuserto
viewflights,airports,customers,andreservations.TheGUIalsoallowstheuserto
addnewflightsandcustomerstothedatabase,aswellascreateanddelete
reservations.
Overall,thisprojectprovidesapracticalexampleofusingJDBCandSQLtocreatea
databaseandperformbasicCRUD(Create,Read,Update,Delete)operationsonit.
Italsodemonstratestheuseofagraphicaluserinterface(GUI)tointeractwiththe
database,whichcanmakeitmoreaccessibletoend-users.
Tags