Java SQL ResultSet Read getResultArray(ResultSet resultSet, int size, java.util.Date startDate)

Here you can find the source of getResultArray(ResultSet resultSet, int size, java.util.Date startDate)

Description

Pull the data out of the result set and date template it.

License

Open Source License

Parameter

Parameter Description
resultSet The ResultSet from the query.
size size of the data array desired.
startDate the first date of the data array.

Declaration

public static double[] getResultArray(ResultSet resultSet, int size, java.util.Date startDate) 

Method Source Code

//package com.java2s;
/*// ww w . ja  v  a 2s .  co  m
 * Copyright (c) 2013 The Johns Hopkins University/Applied Physics Laboratory
 *                             All rights reserved.
 *
 * This material may be used, modified, or reproduced by or for the U.S.
 * Government pursuant to the rights granted under the clauses at
 * DFARS 252.227-7013/7014 or FAR 52.227-14.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * NO WARRANTY.   THIS MATERIAL IS PROVIDED "AS IS."  JHU/APL DISCLAIMS ALL
 * WARRANTIES IN THE MATERIAL, WHETHER EXPRESS OR IMPLIED, INCLUDING (BUT NOT
 * LIMITED TO) ANY AND ALL IMPLIED WARRANTIES OF PERFORMANCE,
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT OF
 * INTELLECTUAL PROPERTY RIGHTS. ANY USER OF THE MATERIAL ASSUMES THE ENTIRE
 * RISK AND LIABILITY FOR USING THE MATERIAL.  IN NO EVENT SHALL JHU/APL BE
 * LIABLE TO ANY USER OF THE MATERIAL FOR ANY ACTUAL, INDIRECT,
 * CONSEQUENTIAL, SPECIAL OR OTHER DAMAGES ARISING FROM THE USE OF, OR
 * INABILITY TO USE, THE MATERIAL, INCLUDING, BUT NOT LIMITED TO, ANY DAMAGES
 * FOR LOST PROFITS.
 */

import java.sql.ResultSet;
import java.sql.SQLException;

import java.util.Calendar;
import java.util.GregorianCalendar;

public class Main {
    /**
     * Pull the data out of the result set and date template it.
     *
     * @param resultSet The ResultSet from the query.
     * @param size      size of the data array desired.
     * @param startDate the first date of the data array.
     */
    public static double[] getResultArray(ResultSet resultSet, int size, java.util.Date startDate) {
        double[] returnArray = new double[size];

        Calendar cal = new GregorianCalendar();
        if (startDate != null) {
            cal.setTime(startDate);
        } else {
            cal.setTime(new java.util.Date());
            cal.add(Calendar.DATE, -(size - 1));
        }

        Calendar cal2 = new GregorianCalendar();

        try {
            int index = 0;
            while (resultSet.next() && index < size) {
                cal2.setTime(resultSet.getDate("Date"));
                while ((cal.get(Calendar.DATE) != cal2.get(Calendar.DATE))
                        || (cal.get(Calendar.MONTH) != cal2.get(Calendar.MONTH))
                        || (cal.get(Calendar.YEAR) != cal2.get(Calendar.YEAR))) {
                    cal.add(Calendar.DATE, 1);
                    returnArray[index] = 0;
                    index++;
                }
                returnArray[index] = resultSet.getDouble("Count");
                index++;
                cal.add(Calendar.DATE, 1);
            }
            while (index < size) {
                returnArray[index] = 0;
                index++;
            }
        } catch (SQLException sqle) {
            System.err.println("ERROR - [DetectorHelper]: " + sqle.getMessage());
            sqle.printStackTrace();
        }
        return returnArray;
    }
}

Related

  1. getOjbClassName(ResultSet rs)
  2. getOptionalInt(ResultSet rs, String name)
  3. getPath(ResultSet r, String columnName)
  4. getRecordsFromResultSet(ResultSet rs)
  5. getResult(ResultSet rs)
  6. getResultByMap(ResultSet rs)
  7. getResults(ResultSet result)
  8. getResultsAsMap(ResultSet rs)
  9. getResultSet(Connection c, String query)