0

I have set up a MySQL database, my connection works, and I've somewhat got a handle on how to retrieve data from the database using ResultSet. The issue I'm currently having is that the assignment calls for certain columns from a few different tables to be printed in CSV format along with replacing some values that meet certain conditions; i.e., if the cost of a plane ticket for a particular flight is under a certain price, replace the cost with "LOW!" in that part of the CSV file. Here's what I've got:

public void generate() throws IOException, SQLException{

    FileWriter fw = new FileWriter("datamine.csv");
    PrintWriter out = new PrintWriter(fw);
    Statement st = conn.createStatement();

    out.println("Airline,FlightNumber,Source,Destination,DayOfWeek,Seats,Daysbefore,Price");

    //ResultStatements here

    out.flush();
    out.close();
    fw.close();

}

I know how to query the DB, and I think I can figure out how to union the columns from tables that I need, but that will just print everything in the column. Is there a way to iterate through the columns, evaluate the value for conditionals, and then append it to the output file?

1 Answer 1

1

You need a while loop, and an if test.

while (rs.next()) {

    String airline = rs.getString(1);
    ...
    BigDecimal price = rs.getBigDecimal(8);
    if (price.compareTo(limit) < 0) {
        csvFile.print("LOW!");
    }
    else {
        csvFile.print(price);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.