List of usage examples for java.sql ResultSet getTime
java.sql.Time getTime(String columnLabel) throws SQLException;
ResultSet
object as a java.sql.Time
object in the Java programming language. From source file:org.apache.ddlutils.platform.PlatformImplBase.java
/** * This is the core method to retrieve a value for a column from a result set. Its primary * purpose is to call the appropriate method on the result set, and to provide an extension * point where database-specific implementations can change this behavior. * /*from w w w . j ava2 s .c om*/ * @param resultSet The result set to extract the value from * @param columnName The name of the column; can be <code>null</code> in which case the * <code>columnIdx</code> will be used instead * @param columnIdx The index of the column's value in the result set; is only used if * <code>columnName</code> is <code>null</code> * @param jdbcType The jdbc type to extract * @return The value * @throws SQLException If an error occurred while accessing the result set */ protected Object extractColumnValue(ResultSet resultSet, String columnName, int columnIdx, int jdbcType) throws SQLException { boolean useIdx = (columnName == null); Object value; switch (jdbcType) { case Types.CHAR: case Types.VARCHAR: case Types.LONGVARCHAR: value = useIdx ? resultSet.getString(columnIdx) : resultSet.getString(columnName); break; case Types.NUMERIC: case Types.DECIMAL: value = useIdx ? resultSet.getBigDecimal(columnIdx) : resultSet.getBigDecimal(columnName); break; case Types.BIT: case Types.BOOLEAN: value = new Boolean(useIdx ? resultSet.getBoolean(columnIdx) : resultSet.getBoolean(columnName)); break; case Types.TINYINT: case Types.SMALLINT: case Types.INTEGER: value = new Integer(useIdx ? resultSet.getInt(columnIdx) : resultSet.getInt(columnName)); break; case Types.BIGINT: value = new Long(useIdx ? resultSet.getLong(columnIdx) : resultSet.getLong(columnName)); break; case Types.REAL: value = new Float(useIdx ? resultSet.getFloat(columnIdx) : resultSet.getFloat(columnName)); break; case Types.FLOAT: case Types.DOUBLE: value = new Double(useIdx ? resultSet.getDouble(columnIdx) : resultSet.getDouble(columnName)); break; case Types.BINARY: case Types.VARBINARY: case Types.LONGVARBINARY: value = useIdx ? resultSet.getBytes(columnIdx) : resultSet.getBytes(columnName); break; case Types.DATE: value = useIdx ? resultSet.getDate(columnIdx) : resultSet.getDate(columnName); break; case Types.TIME: value = useIdx ? resultSet.getTime(columnIdx) : resultSet.getTime(columnName); break; case Types.TIMESTAMP: value = useIdx ? resultSet.getTimestamp(columnIdx) : resultSet.getTimestamp(columnName); break; case Types.CLOB: Clob clob = useIdx ? resultSet.getClob(columnIdx) : resultSet.getClob(columnName); if (clob == null) { value = null; } else { long length = clob.length(); if (length > Integer.MAX_VALUE) { value = clob; } else if (length == 0) { // the javadoc is not clear about whether Clob.getSubString // can be used with a substring length of 0 // thus we do the safe thing and handle it ourselves value = ""; } else { value = clob.getSubString(1l, (int) length); } } break; case Types.BLOB: Blob blob = useIdx ? resultSet.getBlob(columnIdx) : resultSet.getBlob(columnName); if (blob == null) { value = null; } else { long length = blob.length(); if (length > Integer.MAX_VALUE) { value = blob; } else if (length == 0) { // the javadoc is not clear about whether Blob.getBytes // can be used with for 0 bytes to be copied // thus we do the safe thing and handle it ourselves value = new byte[0]; } else { value = blob.getBytes(1l, (int) length); } } break; case Types.ARRAY: value = useIdx ? resultSet.getArray(columnIdx) : resultSet.getArray(columnName); break; case Types.REF: value = useIdx ? resultSet.getRef(columnIdx) : resultSet.getRef(columnName); break; default: value = useIdx ? resultSet.getObject(columnIdx) : resultSet.getObject(columnName); break; } return resultSet.wasNull() ? null : value; }
From source file:fll.scheduler.TournamentSchedule.java
/** * Load a tournament from the database.// w w w . ja va 2 s . c om * * @param connection * @param tournamentID * @throws SQLException */ public TournamentSchedule(final Connection connection, final int tournamentID) throws SQLException { final Tournament currentTournament = Tournament.findTournamentByID(connection, tournamentID); name = currentTournament.getName(); PreparedStatement getSched = null; ResultSet sched = null; PreparedStatement getPerfRounds = null; ResultSet perfRounds = null; PreparedStatement getNumRounds = null; ResultSet numRounds = null; PreparedStatement getSubjective = null; ResultSet subjective = null; PreparedStatement getSubjectiveStations = null; ResultSet stations = null; try { getSubjectiveStations = connection .prepareStatement("SELECT DISTINCT name from sched_subjective WHERE tournament = ?"); getSubjectiveStations.setInt(1, tournamentID); stations = getSubjectiveStations.executeQuery(); while (stations.next()) { final String name = stations.getString(1); subjectiveStations.add(name); } SQLFunctions.close(stations); stations = null; SQLFunctions.close(getSubjectiveStations); getSubjectiveStations = null; getNumRounds = connection .prepareStatement("SELECT MAX(round) FROM sched_perf_rounds WHERE tournament = ?"); getNumRounds.setInt(1, tournamentID); numRounds = getNumRounds.executeQuery(); if (numRounds.next()) { this.numRounds = numRounds.getInt(1) + 1; } else { throw new RuntimeException("No rounds found for tournament: " + tournamentID); } getSched = connection.prepareStatement("SELECT team_number, judging_station" + " FROM schedule"// + " WHERE tournament = ?"); getSched.setInt(1, tournamentID); getPerfRounds = connection.prepareStatement("SELECT round, perf_time, table_color, table_side" // + " FROM sched_perf_rounds" // + " WHERE tournament = ? AND team_number = ?" // + " ORDER BY round ASC"); getPerfRounds.setInt(1, tournamentID); getSubjective = connection.prepareStatement("SELECT name, subj_time" // + " FROM sched_subjective" // + " WHERE tournament = ? AND team_number = ?"); getSubjective.setInt(1, tournamentID); sched = getSched.executeQuery(); while (sched.next()) { final int teamNumber = sched.getInt(1); final String judgingStation = sched.getString(2); final TeamScheduleInfo ti = new TeamScheduleInfo(this.numRounds, teamNumber); ti.setJudgingGroup(judgingStation); getSubjective.setInt(2, teamNumber); subjective = getSubjective.executeQuery(); while (subjective.next()) { final String name = subjective.getString(1); final Time subjTime = subjective.getTime(2); ti.addSubjectiveTime(new SubjectiveTime(name, subjTime.toLocalTime())); } getPerfRounds.setInt(2, teamNumber); perfRounds = getPerfRounds.executeQuery(); int prevRound = -1; while (perfRounds.next()) { final int round = perfRounds.getInt(1); if (round != prevRound + 1) { throw new RuntimeException( "Rounds must be consecutive and start at 1. Tournament: " + tournamentID + " team: " + teamNumber + " round: " + (round + 1) + " prevRound: " + (prevRound + 1)); } final LocalTime perfTime = perfRounds.getTime(2).toLocalTime(); final String tableColor = perfRounds.getString(3); final int tableSide = perfRounds.getInt(4); if (tableSide != 1 && tableSide != 2) { throw new RuntimeException("Tables sides must be 1 or 2. Tournament: " + tournamentID + " team: " + teamNumber); } final PerformanceTime performance = new PerformanceTime(perfTime, tableColor, tableSide); ti.setPerf(round, performance); prevRound = round; } final String eventDivision = Queries.getEventDivision(connection, teamNumber, tournamentID); ti.setDivision(eventDivision); final Team team = Team.getTeamFromDatabase(connection, teamNumber); ti.setOrganization(team.getOrganization()); ti.setTeamName(team.getTeamName()); cacheTeamScheduleInformation(ti); } } finally { SQLFunctions.close(stations); stations = null; SQLFunctions.close(getSubjectiveStations); getSubjectiveStations = null; SQLFunctions.close(sched); sched = null; SQLFunctions.close(getSched); getSched = null; SQLFunctions.close(perfRounds); perfRounds = null; SQLFunctions.close(getPerfRounds); getPerfRounds = null; SQLFunctions.close(numRounds); numRounds = null; SQLFunctions.close(getNumRounds); getNumRounds = null; SQLFunctions.close(subjective); subjective = null; SQLFunctions.close(getSubjective); getSubjective = null; } }
From source file:org.wso2.carbon.dataservices.core.description.query.SQLQuery.java
private DataEntry getDataEntryFromRS(ResultSet rs) throws SQLException { DataEntry dataEntry = new DataEntry(); ResultSetMetaData metaData = rs.getMetaData(); int columnCount = metaData.getColumnCount(); int columnType; String value;/* ww w . ja v a 2 s . c o m*/ ParamValue paramValue; Time sqlTime; Date sqlDate; Timestamp sqlTimestamp; Blob sqlBlob; BigDecimal bigDecimal; InputStream binInStream; boolean useColumnNumbers = this.isUsingColumnNumbers(); for (int i = 1; i <= columnCount; i++) { /* retrieve values according to the column type */ columnType = metaData.getColumnType(i); switch (columnType) { /* handle string types */ case Types.VARCHAR: /* fall through */ case Types.LONGVARCHAR: /* fall through */ case Types.CHAR: /* fall through */ case Types.CLOB: /* fall through */ case Types.NCHAR: /* fall through */ case Types.NCLOB: /* fall through */ case Types.NVARCHAR: /* fall through */ case Types.LONGNVARCHAR: value = rs.getString(i); paramValue = new ParamValue(value); break; /* handle numbers */ case Types.INTEGER: /* fall through */ case Types.TINYINT: /* fall through */ case Types.SMALLINT: value = ConverterUtil.convertToString(rs.getInt(i)); paramValue = new ParamValue(rs.wasNull() ? null : value); break; case Types.DOUBLE: value = ConverterUtil.convertToString(rs.getDouble(i)); paramValue = new ParamValue(rs.wasNull() ? null : value); break; case Types.FLOAT: value = ConverterUtil.convertToString(rs.getFloat(i)); paramValue = new ParamValue(rs.wasNull() ? null : value); break; case Types.BOOLEAN: /* fall through */ case Types.BIT: value = ConverterUtil.convertToString(rs.getBoolean(i)); paramValue = new ParamValue(rs.wasNull() ? null : value); break; case Types.DECIMAL: bigDecimal = rs.getBigDecimal(i); if (bigDecimal != null) { value = ConverterUtil.convertToString(bigDecimal); } else { value = null; } paramValue = new ParamValue(value); break; /* handle data/time values */ case Types.TIME: /* handle time data type */ sqlTime = rs.getTime(i); if (sqlTime != null) { value = this.convertToTimeString(sqlTime); } else { value = null; } paramValue = new ParamValue(value); break; case Types.DATE: /* handle date data type */ sqlDate = rs.getDate(i); if (sqlDate != null) { value = ConverterUtil.convertToString(sqlDate); } else { value = null; } paramValue = new ParamValue(value); break; case Types.TIMESTAMP: sqlTimestamp = rs.getTimestamp(i, calendar); if (sqlTimestamp != null) { value = this.convertToTimestampString(sqlTimestamp); } else { value = null; } paramValue = new ParamValue(value); break; /* handle binary types */ case Types.BLOB: sqlBlob = rs.getBlob(i); if (sqlBlob != null) { value = this.getBase64StringFromInputStream(sqlBlob.getBinaryStream()); } else { value = null; } paramValue = new ParamValue(value); break; case Types.BINARY: /* fall through */ case Types.LONGVARBINARY: /* fall through */ case Types.VARBINARY: binInStream = rs.getBinaryStream(i); if (binInStream != null) { value = this.getBase64StringFromInputStream(binInStream); } else { value = null; } paramValue = new ParamValue(value); break; /* handling User Defined Types */ case Types.STRUCT: Struct udt = (Struct) rs.getObject(i); paramValue = new ParamValue(udt); break; case Types.ARRAY: paramValue = new ParamValue(ParamValue.PARAM_VALUE_ARRAY); Array dataArray = (Array) rs.getObject(i); if (dataArray == null) { break; } paramValue = this.processSQLArray(dataArray, paramValue); break; case Types.NUMERIC: bigDecimal = rs.getBigDecimal(i); if (bigDecimal != null) { value = ConverterUtil.convertToString(bigDecimal); } else { value = null; } paramValue = new ParamValue(value); break; case Types.BIGINT: value = ConverterUtil.convertToString(rs.getLong(i)); paramValue = new ParamValue(rs.wasNull() ? null : value); break; /* handle all other types as strings */ default: value = rs.getString(i); paramValue = new ParamValue(value); break; } dataEntry.addValue(useColumnNumbers ? Integer.toString(i) : metaData.getColumnLabel(i), paramValue); } return dataEntry; }
From source file:org.apache.openjpa.jdbc.sql.DBDictionary.java
/** * Convert the specified column of the SQL ResultSet to the proper * java type.// w ww. j ava2 s .c o m */ public Time getTime(ResultSet rs, int column, Calendar cal) throws SQLException { if (cal == null) return rs.getTime(column); return rs.getTime(column, cal); }
From source file:com.github.woonsan.jdbc.jcr.impl.JcrJdbcResultSetTest.java
@SuppressWarnings("deprecation") private void assertNonExistingColumn(final ResultSet rs) throws Exception { int nonExistingColIndex = Integer.MAX_VALUE; String nonExistingColName = "col" + nonExistingColIndex; try {/*from w w w . j a va 2 s .c o m*/ rs.getString(nonExistingColIndex); fail(); } catch (SQLException ignore) { } try { rs.getString(nonExistingColName); fail(); } catch (SQLException ignore) { } try { rs.getBoolean(nonExistingColIndex); fail(); } catch (SQLException ignore) { } try { rs.getBoolean(nonExistingColName); fail(); } catch (SQLException ignore) { } try { rs.getByte(nonExistingColIndex); fail(); } catch (SQLException ignore) { } try { rs.getByte(nonExistingColName); fail(); } catch (SQLException ignore) { } try { rs.getShort(nonExistingColIndex); fail(); } catch (SQLException ignore) { } try { rs.getShort(nonExistingColName); fail(); } catch (SQLException ignore) { } try { rs.getInt(nonExistingColIndex); fail(); } catch (SQLException ignore) { } try { rs.getInt(nonExistingColName); fail(); } catch (SQLException ignore) { } try { rs.getLong(nonExistingColIndex); fail(); } catch (SQLException ignore) { } try { rs.getLong(nonExistingColName); fail(); } catch (SQLException ignore) { } try { rs.getFloat(nonExistingColIndex); fail(); } catch (SQLException ignore) { } try { rs.getFloat(nonExistingColName); fail(); } catch (SQLException ignore) { } try { rs.getDouble(nonExistingColIndex); fail(); } catch (SQLException ignore) { } try { rs.getDouble(nonExistingColName); fail(); } catch (SQLException ignore) { } try { rs.getBigDecimal(nonExistingColIndex); fail(); } catch (SQLException ignore) { } try { rs.getBigDecimal(nonExistingColName); fail(); } catch (SQLException ignore) { } try { rs.getBigDecimal(nonExistingColIndex, 1); fail(); } catch (SQLException ignore) { } try { rs.getBigDecimal(nonExistingColName, 1); fail(); } catch (SQLException ignore) { } try { rs.getBytes(nonExistingColIndex); fail(); } catch (SQLException ignore) { } try { rs.getBytes(nonExistingColName); fail(); } catch (SQLException ignore) { } try { rs.getDate(nonExistingColIndex); fail(); } catch (SQLException ignore) { } try { rs.getDate(nonExistingColName); fail(); } catch (SQLException ignore) { } try { rs.getDate(nonExistingColIndex, null); fail(); } catch (SQLException ignore) { } try { rs.getDate(nonExistingColName, null); fail(); } catch (SQLException ignore) { } try { rs.getTime(nonExistingColIndex); fail(); } catch (SQLException ignore) { } try { rs.getTime(nonExistingColName); fail(); } catch (SQLException ignore) { } try { rs.getTime(nonExistingColIndex, null); fail(); } catch (SQLException ignore) { } try { rs.getTime(nonExistingColName, null); fail(); } catch (SQLException ignore) { } try { rs.getTimestamp(nonExistingColIndex); fail(); } catch (SQLException ignore) { } try { rs.getTimestamp(nonExistingColName); fail(); } catch (SQLException ignore) { } try { rs.getTimestamp(nonExistingColIndex, null); fail(); } catch (SQLException ignore) { } try { rs.getTimestamp(nonExistingColName, null); fail(); } catch (SQLException ignore) { } try { rs.getAsciiStream(nonExistingColIndex); fail(); } catch (SQLException ignore) { } try { rs.getAsciiStream(nonExistingColName); fail(); } catch (SQLException ignore) { } try { rs.getUnicodeStream(nonExistingColIndex); fail(); } catch (SQLException ignore) { } try { rs.getUnicodeStream(nonExistingColName); fail(); } catch (SQLException ignore) { } try { rs.getBinaryStream(nonExistingColIndex); fail(); } catch (SQLException ignore) { } try { rs.getBinaryStream(nonExistingColName); fail(); } catch (SQLException ignore) { } try { rs.getCharacterStream(nonExistingColIndex); fail(); } catch (SQLException ignore) { } try { rs.getCharacterStream(nonExistingColName); fail(); } catch (SQLException ignore) { } }
From source file:com.github.woonsan.jdbc.jcr.impl.JcrJdbcResultSetTest.java
@SuppressWarnings("deprecation") @Test//from w w w . j av a 2 s . c o m public void testResultSetWhenClosed() throws Exception { Statement statement = getConnection().createStatement(); ResultSet rs = statement.executeQuery(SQL_EMPS); rs.close(); try { rs.isBeforeFirst(); fail(); } catch (SQLException ignore) { } try { rs.isAfterLast(); fail(); } catch (SQLException ignore) { } try { rs.isFirst(); fail(); } catch (SQLException ignore) { } try { rs.isLast(); fail(); } catch (SQLException ignore) { } try { rs.beforeFirst(); fail(); } catch (SQLException ignore) { } try { rs.afterLast(); fail(); } catch (SQLException ignore) { } try { rs.first(); fail(); } catch (SQLException ignore) { } try { rs.last(); fail(); } catch (SQLException ignore) { } try { rs.next(); fail(); } catch (SQLException ignore) { } try { rs.getRow(); fail(); } catch (SQLException ignore) { } try { rs.getType(); fail(); } catch (SQLException ignore) { } try { rs.getConcurrency(); fail(); } catch (SQLException ignore) { } try { rs.rowUpdated(); fail(); } catch (SQLException ignore) { } try { rs.rowDeleted(); fail(); } catch (SQLException ignore) { } try { rs.rowInserted(); fail(); } catch (SQLException ignore) { } try { rs.getStatement(); fail(); } catch (SQLException ignore) { } try { rs.wasNull(); fail(); } catch (SQLException ignore) { } try { rs.getString(1); fail(); } catch (SQLException ignore) { } try { rs.getString("col1"); fail(); } catch (SQLException ignore) { } try { rs.getBoolean(1); fail(); } catch (SQLException ignore) { } try { rs.getBoolean("col1"); fail(); } catch (SQLException ignore) { } try { rs.getByte(1); fail(); } catch (SQLException ignore) { } try { rs.getByte("col1"); fail(); } catch (SQLException ignore) { } try { rs.getShort(1); fail(); } catch (SQLException ignore) { } try { rs.getShort("col1"); fail(); } catch (SQLException ignore) { } try { rs.getInt(1); fail(); } catch (SQLException ignore) { } try { rs.getInt("col1"); fail(); } catch (SQLException ignore) { } try { rs.getLong(1); fail(); } catch (SQLException ignore) { } try { rs.getLong("col1"); fail(); } catch (SQLException ignore) { } try { rs.getFloat(1); fail(); } catch (SQLException ignore) { } try { rs.getFloat("col1"); fail(); } catch (SQLException ignore) { } try { rs.getDouble(1); fail(); } catch (SQLException ignore) { } try { rs.getDouble("col1"); fail(); } catch (SQLException ignore) { } try { rs.getBigDecimal(1); fail(); } catch (SQLException ignore) { } try { rs.getBigDecimal("col1"); fail(); } catch (SQLException ignore) { } try { rs.getBytes(1); fail(); } catch (SQLException ignore) { } try { rs.getBytes("col1"); fail(); } catch (SQLException ignore) { } try { rs.getDate(1); fail(); } catch (SQLException ignore) { } try { rs.getDate(1, null); fail(); } catch (SQLException ignore) { } try { rs.getDate("col1"); fail(); } catch (SQLException ignore) { } try { rs.getDate("col1", null); fail(); } catch (SQLException ignore) { } try { rs.getTime(1); fail(); } catch (SQLException ignore) { } try { rs.getTime(1, null); fail(); } catch (SQLException ignore) { } try { rs.getTime("col1"); fail(); } catch (SQLException ignore) { } try { rs.getTime("col1", null); fail(); } catch (SQLException ignore) { } try { rs.getTimestamp(1); fail(); } catch (SQLException ignore) { } try { rs.getTimestamp(1, null); fail(); } catch (SQLException ignore) { } try { rs.getTimestamp("col1"); fail(); } catch (SQLException ignore) { } try { rs.getTimestamp("col1", null); fail(); } catch (SQLException ignore) { } try { rs.getAsciiStream(1); fail(); } catch (SQLException ignore) { } try { rs.getAsciiStream("col1"); fail(); } catch (SQLException ignore) { } try { rs.getUnicodeStream(1); fail(); } catch (SQLException ignore) { } try { rs.getUnicodeStream("col1"); fail(); } catch (SQLException ignore) { } try { rs.getBinaryStream(1); fail(); } catch (SQLException ignore) { } try { rs.getBinaryStream("col1"); fail(); } catch (SQLException ignore) { } try { rs.getCharacterStream(1); fail(); } catch (SQLException ignore) { } try { rs.getCharacterStream("col1"); fail(); } catch (SQLException ignore) { } try { rs.getMetaData(); fail(); } catch (SQLException ignore) { } try { rs.setFetchDirection(1); fail(); } catch (SQLException ignore) { } try { rs.getFetchDirection(); fail(); } catch (SQLException ignore) { } try { rs.setFetchSize(100); fail(); } catch (SQLException ignore) { } try { rs.getFetchSize(); fail(); } catch (SQLException ignore) { } try { rs.getHoldability(); fail(); } catch (SQLException ignore) { } statement.close(); }