How can I print column sections from SQL to Java to CSV?

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?

Answers


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

Need Your Help

what is the difference about Unity-iPhone and Unity-iPhone-simulator in xcode

xcode unity3d simulator

when I use the xcode for compiling the game what is built from unity , I notice that if I choose Unity-iPhone-simulator ,there are many errors ,after I choose the Unity-iPhone ,the project runs

How quickly will websocket in the dev versions of Firefox and Chrome be moved to the official releases?

firefox google-chrome websocket

I realize that this is hard/maybe impossible to estimate - but any estimation, even plus or minus 6 months, would be nice.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.