Java examples for java.sql:ResultSet
retrieve a 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 .ja va2s . c om * Attempts to retrieve a 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 getNonNullableTime(ResultSet result, int columnIndex) throws SQLException { Calendar cal = getNullableTime(result, columnIndex); if (cal == null) { throw new SQLException("The column " + columnIndex + " does not contain a date value."); } return cal; } /** * This method retrieves a 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 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 getNullableTime(ResultSet result, int columnIndex) throws SQLException { java.util.Date data = result.getTime((columnIndex)); if (result.wasNull()) { return null; } else { Calendar cal = Calendar.getInstance(); cal.setTime(data); return cal; } } /** * This method retrieves a 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 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 getNullableTime(CallableStatement stmt, int paramIndex) throws SQLException { java.util.Date date = stmt.getTime(paramIndex); if (stmt.wasNull()) { return null; } else { Calendar cal = Calendar.getInstance(); cal.setTime(date); return cal; } } }