retrieve a date value form the resultset at the given column. - Java java.sql

Java examples for java.sql:ResultSet

Description

retrieve a date value form the resultset at the given column.

Demo Code


//package com.java2s;
import java.sql.CallableStatement;

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

import java.util.Calendar;

public class Main {
    /**/*  w  ww.  ja v  a  2  s  .  c o  m*/
     * Attempts to retrieve a date value form the resultset at the given
     * column.  If the value is null, an SQLException is thrown.
     * @param result The resultset from which to get the value.
     * @param columnIndex The index of the column in which the data resides.
     * @return The Calendar object with the date data.
     * @throws SQLException Indicates an error retrieving the data.
     */
    public static Calendar getNonNullableDate(CallableStatement stmt,
            int columnIndex) throws SQLException {
        Calendar cal = getNullableDate(stmt, columnIndex);
        if (cal == null) {
            throw new SQLException("The column " + columnIndex
                    + " does not contain a date value.");
        }
        return cal;
    }

    /**
     * Attempts to retrieve a date value form the resultset at the given
     * column.  If the value is null, an SQLException is thrown.
     * @param result The resultset from which to get the value.
     * @param columnIndex The index of the column in which the data resides.
     * @return The Calendar object with the date data.
     * @throws SQLException Indicates an error retrieving the data.
     */
    public static Calendar getNonNullableDate(ResultSet result,
            int columnIndex) throws SQLException {
        Calendar cal = getNullableDate(result, columnIndex);
        if (cal == null) {
            throw new SQLException("The column " + columnIndex
                    + " does not contain a date value.");
        }
        return cal;
    }

    /**
     * This method retrieves a date value from the resultset at the given
     * column.  If the column's value is null, null is returned, else a new
     * Calendar object containing the date value is returned.
     * @param result The result from which the value is to be retrieved.
     * @param columnIndex The index of the column from which the value is to
     *                    be retrieved.
     * @return If the column's value is null, null; else a new Calendar object
     *         containing the date value in the column.
     * @throws SQLException Indicates an error retrieving the data from the
     *         ResultSet.
     */
    public static Calendar getNullableDate(ResultSet result, int columnIndex)
            throws SQLException {
        java.util.Date data = result.getDate((columnIndex));
        if (result.wasNull()) {
            return null;
        } else {
            Calendar cal = Calendar.getInstance();
            cal.setTime(data);
            return cal;
        }
    }

    /**
     * This method retrieves a date value from the callable statement at the
     * given parameter.  If the parameter's value is null, null is returned,
     * else a new Calendar object containing the date value is returned.
     * @param stmt The callable statement from which the value is to be
     *             retrieved.
     * @param paramIndex The index of the parameter from which the value is to
     *                   be retrieved.
     * @return If the parameter's value is null, null; else a new Calendar
     *         object containing the date value in the parameter.
     * @throws SQLException Indicates an error retrieving the data from the
     *         CallableStatement.
     */
    public static Calendar getNullableDate(CallableStatement stmt,
            int paramIndex) throws SQLException {
        java.util.Date date = stmt.getDate(paramIndex);
        if (stmt.wasNull()) {
            return null;
        } else {
            Calendar cal = Calendar.getInstance();
            cal.setTime(date);
            return cal;
        }
    }
}

Related Tutorials