import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;

public class PushDown 
{
  public static void main(String[] args) 
  {
  String familyName;
  String firstName;
    Connection connection;
    DatabaseConnection databaseConnection;
    Statement statement;    
    ResultSet resultSet;
    Date startDate;
    Date endDate;
    long startTime;
    long endTime;
    
    
    familyName = new String(args[0]);
    firstName = new String(args[1]);
    
    databaseConnection = new DatabaseConnection();
    connection = databaseConnection.get();

    startDate = new Date();
    startTime = startDate.getTime();
    
    try 
    {
      statement = connection.createStatement();
      resultSet = statement.executeQuery
      (
        "select " +
          "a.family_name, " +
          "a.first_name, " +
          "d.description as claim_head, " +
          "c.amount " +
        "from " +
          "person a " +
          "join " +
          "claimant b " +
          "on a.id = b.person_id " +
          "join " +
          "transaction c " +
          "on b.id = c.claimant_id " +
          "join " +
          "claim_head d " +
          "on c.claim_head_id = d.id " +
        "where " +
          "a.family_name = '" + familyName + "' and " +
          "a.first_name = '" + firstName + "' " +
        "order by " +
          "claimant_id, " +
          "claim_head_id;"
      );
      while (resultSet.next())
      {
        System.out.println
        (
          "Family name: " + resultSet.getString("family_name") + ", " +
          "First name: " + resultSet.getString("first_name") + ", " +
          "Claim head: " + resultSet.getString("claim_head") + ", " +
          "Amount: " + resultSet.getBigDecimal("amount")
        );
      }
    } 
    catch (SQLException e) 
    {
      e.printStackTrace();
    }
    
    endDate = new Date();
    endTime = endDate.getTime();    
    System.out.println("Elapsed time: " + (endTime - startTime));
  }
}
