0

I have created an sql database with a table containing information about Airplanes, I want to be able to take this information and insert it into an ArrayList of type Aircraft(object) although the different info in the sql table are different primitive types........can this be done?`package uk.ac.qub.sqldbflights;

This is the Aircraft object with all the attributes which are the

public class Aircraft {

/**
 * private String containing the airline name of the aircraft
 *                  -Can't be blank
 */
private int aircraft_number;

/**
 * private String containing the flight number belonging to the aircraft
 *                  -Can't be blank
 */
private String airline_company;

/**
 * private String containing the aircrafts city of origin
 *                  -Can't be blank
 */
private String departure_airport;

/**
 * private int which holds the aircrafts fuel level
 *                  -Must be over 0 and less than 100
 */
private float passenger_number;

/**
 * private int containing the number of passengers aboard the aircraft
 *                  -Must be over 0 and less than 300
 */
private float fuel_percentage;

/**
 * private Boolean indicting wether the aircraft is in the landing queue or not
 */
private int flight_time_remaining;

/**
 * private Boolean indicating wether the aircraft is landed or not
 */
private boolean in_queue;

private boolean is_landed;

public Aircraft() {
}

/**
 * Song creation
 * @param name -not null
 * @param artist -not null
 * @param album -not null
 * @param genre- not null and one of Pop, Dance, Rock
 * @throws IllegalArgumentException
 */
public Aircraft(int aircraft_number, String airline_company, String departure_airport, int passenger_number, float fuel_percentage, int flight_time_remaining, boolean in_queue, boolean is_landed)
        throws IllegalArgumentException {
    try {
        // set name
        this.setAircraft_number(aircraft_number);
        this.setAirline_company(airline_company);
        this.setDeparture_airport(departure_airport);
        this.setPassenger_number(passenger_number);
        this.setFuel_percentage(fuel_percentage);
        this.setFlight_time_remaining(flight_time_remaining);
        this.setIn_queue(in_queue);
        this.setIs_landed(is_landed);




    } catch (IllegalArgumentException ex) {
            System.out.println("Unable to create song due to arguments passed");
            throw ex;
    }}


public int getAircraft_number() {
    return aircraft_number;
}

public void setAircraft_number(int aircraft_number) {
    this.aircraft_number = aircraft_number;
}

public String getAirline_company() {
    return airline_company;
}

public void setAirline_company(String airline_company) {
    this.airline_company = airline_company;
}

public String getDeparture_airport() {
    return departure_airport;
}

public void setDeparture_airport(String departure_airport) {
    this.departure_airport = departure_airport;
}

public float getPassenger_number() {
    return passenger_number;
}

public void setPassenger_number(float passenger_number) {
    this.passenger_number = passenger_number;
}

public float getFuel_percentage() {
    return fuel_percentage;
}

public void setFuel_percentage(float fuel_percentage) {
    this.fuel_percentage = fuel_percentage;
}

public int isFlight_time_remaining() {
    return flight_time_remaining;
}

public void setFlight_time_remaining(int flight_time_remaining) {
    this.flight_time_remaining = flight_time_remaining;
}

public boolean isIn_queue() {
    return in_queue;
}

public void setIn_queue(boolean in_queue) {
    this.in_queue = in_queue;
}

public boolean isIs_landed() {
    return is_landed;
}

public void setIs_landed(boolean is_landed) {
    this.is_landed = is_landed;
}

}

This is the code which makes the connection to the sql DB and trys to add the info to an arraylist...

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.SQLException;

    import java.sql.Statement;
    import java.util.ArrayList;


    public class FlightsDbtoArrayList {
ArrayList<Aircraft> allFlights = new ArrayList<Aircraft>();

public static void main(String[] args) {
    // Entering username to verify connection to SQL Server
    String url = "jdbc:mysql://"; 
    Connection con;

    Statement statement1;



    try {
        Class.forName("com.mysql.jdbc.Driver");
        //Catching any errors and printing a message to the user
    } catch(java.lang.ClassNotFoundException e) {
        System.err.print("ClassNotFoundException: "); 
        System.err.println(e.getMessage());
    }

    try {
        // Entering username and password to verify connection to SQL Server
        con = DriverManager.getConnection(url);





        //Creating platform for a SQL query Statement
        statement1 = con.createStatement();
        //Creating and executing the designed SQL query statement
        ResultSet results1 = statement1.executeQuery("SELECT aircraft_number, airline_company, departure_airport, passenger_number, fuel_Percentage, flight_time_remaining, in_queue, is_landed FROM flights");
        //Displaying the results of the query to screen
        printResults(results1);


        con.close();
        statement1.close();


    } catch(SQLException ex) {
        System.err.println("SQLException: " + ex.getMessage());
    }
}
/**
 * Method to display the results of the three SQL queries
 * @param results
 * @throws SQLException
 * 
 * 
 */
public ArrayList<Aircraft> allFlights() {
    return allFlights;
}

/**
 * set  songs on the system
 * @param allSongs
 */
public  void setSongs(ArrayList<Aircraft> allSongs) {
    allSongs = this.allFlights;
}
private static void printResults(ResultSet results) throws SQLException {


    while (results.next()) {
        int aircraft_number = results.getInt("aircraft_number");
        String airline_company = results.getString("airline_company");
        String departure_airport = results.getString("departure_airport");
        int passenger_number = results.getInt("passenger_number");
        float fuel_percentage = results.getFloat("fuel_percentage");
        int flight_time_remaining = results.getInt("flight_time_remaining");
        boolean in_queue = results.getBoolean("in_queue");
        boolean is_landed = results.getBoolean("is_landed");


        Aircraft a1;


        ArrayList<Aircraft> allFlights = new ArrayList<Aircraft>();



        a1 = new Aircraft(aircraft_number, airline_company, departure_airport, passenger_number, fuel_percentage, flight_time_remaining, in_queue, is_landed);

        allFlights.add(a1);
        System.out.println(allFlights);
    }
}
}
`
Matteo Italia
  • 123,740
  • 17
  • 206
  • 299

1 Answers1

0

Yes it is definitely possible to have list of objects(Aircraft) derived from database. But in this case your while loop inside the PrintResult has some errors.

You are creating list allFights within the while loop which will create new list in each iteration and add a1 to it so in the end you will only have a list with one aircraft details in it.

System.out.println(allFlights) will not give much desired output (or it might), but I would advice you should override toString() method in your AirCraft class.

Try to change your PrintResult method as following.

private static void printResults(ResultSet results) throws SQLException {

ArrayList<Aircraft> allFlights = new ArrayList<Aircraft>();

Aircraft a1;

while (results.next()) {
    int aircraft_number = results.getInt("aircraft_number");
    String airline_company = results.getString("airline_company");
    String departure_airport = results.getString("departure_airport");
    int passenger_number = results.getInt("passenger_number");
    float fuel_percentage = results.getFloat("fuel_percentage");
    int flight_time_remaining = results.getInt("flight_time_remaining");
    boolean in_queue = results.getBoolean("in_queue");
    boolean is_landed = results.getBoolean("is_landed");
    a1 = new Aircraft(aircraft_number, airline_company, departure_airport, passenger_number, fuel_percentage, flight_time_remaining, in_queue, is_landed);
    allFlights.add(a1);

    //Instead of this line System.out.println(allFlights);

    //write following code
    For(AirCraft aircraft : allFlights){
        System.out.println(aircraft.toString());
    }
}
}

Edit 2: Write the following method in your AirCraft class.

@Override 
Public String toString(){
    String string;
    //Write some code here so that you can represent you object using this method
    //for example I am adding just the aircraft_number
    string = getAircraft_number()+"";
    return string;
}

Important: The toString() method I wrote is just an example you need to learn how to correctly write toString() method for any of your class. this and this are good starting point to learn that. And stop worrying about your your list of allFlights because as per this code it is getting created but you can not print it the way you are trying to.

Community
  • 1
  • 1
madteapot
  • 2,208
  • 2
  • 19
  • 32
  • [uk.ac.qub.sqldbflights.Aircraft@47eaec].....it just outputs this lots of times instead of the database info, I want it to create an Aircraft object for each aircraft in the database, then add them to an ArrayList so they can be used elsewhere in the programme....any ideas? thnks for helping btw! –  May 06 '14 at 23:24
  • yes your arraylist is getting created but the way you are printing is not correct(it's correct but will output the reference to the object and not String). Please check the edit part in my code. – madteapot May 06 '14 at 23:27
  • I cant find a toString method in Aircraft class....Sorry man im a real newb, Not sure how to make a seperate Aircraft object in the arrayList...so like a1 will have the first planes details, a2 the second....etc –  May 06 '14 at 23:43
  • @user3610099 Does this solve your problem? It would be nice if you reply what was the outcome. – madteapot May 08 '14 at 11:46