[go: up one dir, main page]

0% found this document useful (0 votes)
439 views9 pages

Hands On - JDBC

The document describes how to retrieve flight data from a database using JDBC in Java. It defines a Flight class with fields for flight details and getter/setter methods. A FlightManagementSystem class contains methods to add a flight to the database by inserting a Flight object, retrieve a flight by ID by querying the database, and retrieve flights by source and destination by querying the database and returning an ArrayList of Flight objects. Main classes demonstrate using the methods by taking user input and displaying flight details.

Uploaded by

Vamsi Gembali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
439 views9 pages

Hands On - JDBC

The document describes how to retrieve flight data from a database using JDBC in Java. It defines a Flight class with fields for flight details and getter/setter methods. A FlightManagementSystem class contains methods to add a flight to the database by inserting a Flight object, retrieve a flight by ID by querying the database, and retrieve flights by source and destination by querying the database and returning an ArrayList of Flight objects. Main classes demonstrate using the methods by taking user input and displaying flight details.

Uploaded by

Vamsi Gembali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 9

Add Flight using JDBC

public class Flight {

private int flightId;


private String source;
private String destination;
private int noOfSeats;
private double flightFare;
public int getFlightId() {
return flightId;
}
public void setFlightId(int flightId) {
this.flightId = flightId;
}
public String getSource() {
return source;
}
public void setSource(String source) {
this.source = source;
}
public String getDestination() {
return destination;
}
public void setDestination(String destination) {
this.destination = destination;
}
public int getNoOfSeats() {
return noOfSeats;
}
public void setNoOfSeats(int noOfSeats) {
this.noOfSeats = noOfSeats;
}
public double getFlightFare() {
return flightFare;
}
public void setFlightFare(double flightFare) {
this.flightFare = flightFare;
}
public Flight(int flightId, String source, String destination,
int noOfSeats, double flightFare) {
super();
this.flightId = flightId;
this.source = source;
this.destination = destination;
this.noOfSeats = noOfSeats;
this.flightFare = flightFare;
}

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);

int flightId = sc.nextInt();


String source = sc.next();
String destination = sc.next();
int noOfSeats = sc.nextInt();
double flightFare = sc.nextDouble();

Flight flight = new Flight(flightId, source, destination, noOfSeats,


flightFare);

FlightManagementSystem flightManagementSystem =new


FlightManagementSystem();

//boolean isFLightAdded = flightManagementSystem.addFlight(flight);

/*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 {

private int flightId;


private String source;
private String destination;
private int noOfSeats;
private double flightFare;
public int getFlightId() {
return flightId;
}
public void setFlightId(int flightId) {
this.flightId = flightId;
}
public String getSource() {
return source;
}
public void setSource(String source) {
this.source = source;
}
public String getDestination() {
return destination;
}
public void setDestination(String destination) {
this.destination = destination;
}
public int getNoOfSeats() {
return noOfSeats;
}
public void setNoOfSeats(int noOfSeats) {
this.noOfSeats = noOfSeats;
}
public double getFlightFare() {
return flightFare;
}
public void setFlightFare(double flightFare) {
this.flightFare = flightFare;
}
public Flight(int flightId, String source, String destination,
int noOfSeats, double flightFare) {
super();
this.flightId = flightId;
this.source = source;
this.destination = destination;
this.noOfSeats = noOfSeats;
this.flightFare = flightFare;
}
public Flight(){

import java.util.*;
import java.sql.*;
public class FlightManagementSystem{

public Flight viewFlightById(int flightId){


Flight x = new Flight();
try {
DB db = new DB();

Connection con = db.getConnection();


Statement st = con.createStatement();
String sql = "select * from Flight where flightId="+flightId;
System.out.println(sql);
ResultSet rs = st.executeQuery(sql);

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();

Flight obj = fl.viewFlightById(id);


if(obj!= null){
System.out.println("Flight ID : "+obj.getFlightId());
System.out.println("Source : "+obj.getSource());
System.out.println("Destination : "+obj.getDestination());
System.out.println("No of seats : "+obj.getNoOfSeats());
System.out.println("Flight Fare : "+obj.getFlightFare());
}
else{
System.out.println("Invalid Flight ID");
}
}

-----------------------------------------------------------------------------------
-----------------------
Retrieve Flights Based on Source and Destination

public class Flight {

private int flightId;


private String source;
private String destination;
private int noOfSeats;
private double flightFare;
public int getFlightId() {
return flightId;
}
public void setFlightId(int flightId) {
this.flightId = flightId;
}
public String getSource() {
return source;
}
public void setSource(String source) {
this.source = source;
}
public String getDestination() {
return destination;
}
public void setDestination(String destination) {
this.destination = destination;
}
public int getNoOfSeats() {
return noOfSeats;
}
public void setNoOfSeats(int noOfSeats) {
this.noOfSeats = noOfSeats;
}
public double getFlightFare() {
return flightFare;
}
public void setFlightFare(double flightFare) {
this.flightFare = flightFare;
}
public Flight(int flightId, String source, String destination,
int noOfSeats, double flightFare) {
super();
this.flightId = flightId;
this.source = source;
this.destination = destination;
this.noOfSeats = noOfSeats;
this.flightFare = flightFare;
}
}

import java.util.*;
import java.sql.*;

public class FlightManagementSystem{

public ArrayList<Flight> viewFlightsBySourceDestination(String source, String


destination){
DB db = new DB();

ArrayList<Flight> list = new ArrayList<Flight>();


try{
int f=0;

Connection con = db.getConnection();


Statement st = con.createStatement();

String sql = "select*from Flight where source='"+source+"'and


destination='"+destination+"'";

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();

FlightManagementSystem obj = new FlightManagementSystem();


System.out.println(source);
System.out.println(dest);
ArrayList<Flight>res=obj.viewFlightsBySourceDestination(source,dest);

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

public class Flight {

private int flightId;


private String source;
private String destination;
private int noOfSeats;
private double flightFare;
public int getFlightId() {
return flightId;
}
public void setFlightId(int flightId) {
this.flightId = flightId;
}
public String getSource() {
return source;
}
public void setSource(String source) {
this.source = source;
}
public String getDestination() {
return destination;
}
public void setDestination(String destination) {
this.destination = destination;
}
public int getNoOfSeats() {
return noOfSeats;
}
public void setNoOfSeats(int noOfSeats) {
this.noOfSeats = noOfSeats;
}
public double getFlightFare() {
return flightFare;
}
public void setFlightFare(double flightFare) {
this.flightFare = flightFare;
}
public Flight(int flightId, String source, String destination,
int noOfSeats, double flightFare) {
this.flightId = flightId;
this.source = source;
this.destination = destination;
this.noOfSeats = noOfSeats;
this.flightFare = flightFare;
}

import java.util.*;
import java.sql.*;

public class FlightManagementSystem{


public boolean updateFlightFare(int flightId,double flightFare){
DB db = new DB();
boolean out = false;

try{
Connection con = db.getConnection();
Statement st = con.createStatement();

String sql = "Update Flight set flightFare="+flightFare+"WHERE


flightId="+flightId;

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();

boolean res = obj.updateFlightFare(id,fare);

if(res = true)
System.out.println("FLight Fare Updated successfully");
else
System.out.println("FLight fare not updated");
}
}

-----------------------------------------------------
*----------------------------------------------------------------

You might also like