retrieves a date value from the resultset at the given column. - Java java.sql

Java examples for java.sql:ResultSet

Description

retrieves a date value from 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 {
    /**//from w  w  w.  j a  v  a  2 s  .c om
     * 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