Hands On - JDBC
Hands On - JDBC
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class FlightManagementSystem {
public boolean addFlight(Flight flight ){
try{
Connection connection = DB.getConnection();
String query ="insert into flight
(flightId,source,destination,noOfSeats,flightFare) values (?,?,?,?,?);";
PreparedStatement preparedStatement =
connection.prepareStatement(query);
preparedStatement.setInt(1, flight.getFlightId());
preparedStatement.setString(2, flight.getSource());
preparedStatement.setString(3, flight.getDestination());
preparedStatement.setInt(4, flight.getNoOfSeats());
preparedStatement.setDouble(5, flight.getFlightFare());
preparedStatement.execute();
return true;
}
catch(ClassNotFoundException | SQLException e){
e.printStackTrace();
}
return false;
}
}
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
/*if(isFLightAdded)
System.out.println("Flight details added successfully");
else
System.out.println("Addition not done");*/
if (flightManagementSystem . addFlight(flight))
System.out.println("Flight details added successfully");
else
System.out.println("Addition not done");
}
}
-----------------------------------------------------------------------------------
--------------------------------------
Retrieve Flight Using Flight ID
public class Flight {
import java.util.*;
import java.sql.*;
public class FlightManagementSystem{
while(rs.next()){
x.setFlightId(rs.getInt(1));
x.setSource(rs.getString(2));
x.setDestination(rs.getString(3));
x.setNoOfSeats(rs.getInt(4));
x.setFlightFare(rs.getDouble(5));
//Flight x = new
Flight(rs.getInt(1),rs.getString(2),rs.getString(3),rs.getInt(4),rs.getDouble(5));
}
con.close();
}
catch(SQLException e){
System.out.println("SQL Error. Contact Administrator");
}
catch(Exception e) {
System.out.println(e);
}
System.out.println(x);
return x;
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int id = sc.nextInt();
FlightManagementSystem fl = new FlightManagementSystem();
-----------------------------------------------------------------------------------
-----------------------
Retrieve Flights Based on Source and Destination
import java.util.*;
import java.sql.*;
ResultSet rs = st.executeQuery(sql);
while(rs.next()){
Flight x= new
Flight(rs.getInt(1),rs.getString(2),rs.getString(3),rs.getInt(4),rs.getDouble(5));
list.add(x);
}
con.close();
}
catch(SQLException e){
System.out.println("SQL Error. Contact Admisitrator");
}
catch(Exception e){
System.out.println("Exception.Contact Admisitrator.");
}
return list;
}
}
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
System.out.println("Enter the source");
String source=sc.nextLine();
System.out.println("Enter the destination");
String dest = sc.nextLine();
if(res!=null)
for(Flight fl : res){
System.out.println("Flight ID : "+fl.getFlightId());
System.out.println("Source : "+fl.getSource());
System.out.println("Destination : "+fl.getDestination());
System.out.println("NO of seats : "+fl.getNoOfSeats());
System.out.println("Flight Fare : "+fl.getFlightFare());
}
else
System.out.println("No flights available for the given source and
destination");
}
}
-----------------------------------------------------------------------------------
----------------------
Update Flight Fare for a Flight
import java.util.*;
import java.sql.*;
try{
Connection con = db.getConnection();
Statement st = con.createStatement();
int rs = st.executeUpdate(sql);
if(rs>0)
out = true;
else
out= false;
con.close();
}
catch(SQLException e){
System.out.println(e);
}
catch(Exception ex){
System.out.println(ex);
}
return out;
}
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
System.out.println("Enter the flight id");
int id= sc.nextInt();
System.out.println("Enter the Updated flight fare");
double fare = sc.nextDouble();
FlightManagementSystem obj = new FlightManagementSystem();
if(res = true)
System.out.println("FLight Fare Updated successfully");
else
System.out.println("FLight fare not updated");
}
}
-----------------------------------------------------
*----------------------------------------------------------------