Java examples for java.sql:ResultSet
retrieve a date time value form the resultset at the given column.
//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 .jav a 2 s .c o m*/ * Attempts to retrieve a date time 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 getNonNullableTimestamp(ResultSet result, int columnIndex) throws SQLException { Calendar cal = getNullableTimestamp(result, columnIndex); if (cal == null) { throw new SQLException("The column " + columnIndex + " does not contain a date value."); } return cal; } /** * This method retrieves a date and time 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 and time 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 getNullableTimestamp(ResultSet result, int columnIndex) throws SQLException { java.util.Date data = result.getTimestamp((columnIndex)); if (result.wasNull()) { return null; } else { Calendar cal = Calendar.getInstance(); cal.setTime(data); return cal; } } /** * This method retrieves a date and time 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 and time 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 getNullableTimestamp(CallableStatement stmt, int paramIndex) throws SQLException { java.util.Date date = stmt.getTimestamp(paramIndex); if (stmt.wasNull()) { return null; } else { Calendar cal = Calendar.getInstance(); cal.setTime(date); return cal; } } }