List of usage examples for java.sql ResultSet getBoolean
boolean getBoolean(String columnLabel) throws SQLException;
ResultSet
object as a boolean
in the Java programming language. From source file:com.alibaba.otter.node.etl.common.db.utils.SqlUtils.java
/** * Retrieve a JDBC column value from a ResultSet, using the specified value * type.//from w ww . j a va2 s. co m * <p> * Uses the specifically typed ResultSet accessor methods, falling back to * {@link #getResultSetValue(java.sql.ResultSet, int)} for unknown types. * <p> * Note that the returned value may not be assignable to the specified * required type, in case of an unknown type. Calling code needs to deal * with this case appropriately, e.g. throwing a corresponding exception. * * @param rs is the ResultSet holding the data * @param index is the column index * @param requiredType the required value type (may be <code>null</code>) * @return the value object * @throws SQLException if thrown by the JDBC API */ private static String getResultSetValue(ResultSet rs, int index, Class<?> requiredType) throws SQLException { if (requiredType == null) { return getResultSetValue(rs, index); } Object value = null; boolean wasNullCheck = false; // Explicitly extract typed value, as far as possible. if (String.class.equals(requiredType)) { value = rs.getString(index); } else if (boolean.class.equals(requiredType) || Boolean.class.equals(requiredType)) { value = Boolean.valueOf(rs.getBoolean(index)); wasNullCheck = true; } else if (byte.class.equals(requiredType) || Byte.class.equals(requiredType)) { value = new Byte(rs.getByte(index)); wasNullCheck = true; } else if (short.class.equals(requiredType) || Short.class.equals(requiredType)) { value = new Short(rs.getShort(index)); wasNullCheck = true; } else if (int.class.equals(requiredType) || Integer.class.equals(requiredType)) { value = new Long(rs.getLong(index)); wasNullCheck = true; } else if (long.class.equals(requiredType) || Long.class.equals(requiredType)) { value = rs.getBigDecimal(index); wasNullCheck = true; } else if (float.class.equals(requiredType) || Float.class.equals(requiredType)) { value = new Float(rs.getFloat(index)); wasNullCheck = true; } else if (double.class.equals(requiredType) || Double.class.equals(requiredType) || Number.class.equals(requiredType)) { value = new Double(rs.getDouble(index)); wasNullCheck = true; } else if (java.sql.Time.class.equals(requiredType)) { // try { // value = rs.getTime(index); // } catch (SQLException e) { value = rs.getString(index);// ?string0000Time // if (value == null && !rs.wasNull()) { // value = "00:00:00"; // // mysqlzeroDateTimeBehavior=convertToNull0null // } // } } else if (java.sql.Timestamp.class.equals(requiredType) || java.sql.Date.class.equals(requiredType)) { // try { // value = convertTimestamp(rs.getTimestamp(index)); // } catch (SQLException e) { // ?string0000-00-00 00:00:00Timestamp value = rs.getString(index); // if (value == null && !rs.wasNull()) { // value = "0000:00:00 00:00:00"; // // mysqlzeroDateTimeBehavior=convertToNull0null // } // } } else if (BigDecimal.class.equals(requiredType)) { value = rs.getBigDecimal(index); } else if (BigInteger.class.equals(requiredType)) { value = rs.getBigDecimal(index); } else if (Blob.class.equals(requiredType)) { value = rs.getBlob(index); } else if (Clob.class.equals(requiredType)) { value = rs.getClob(index); } else if (byte[].class.equals(requiredType)) { try { byte[] bytes = rs.getBytes(index); if (bytes == null) { value = null; } else { value = new String(bytes, "ISO-8859-1");// binaryiso-8859-1 } } catch (UnsupportedEncodingException e) { throw new SQLException(e); } } else { // Some unknown type desired -> rely on getObject. value = getResultSetValue(rs, index); } // Perform was-null check if demanded (for results that the // JDBC driver returns as primitives). if (wasNullCheck && (value != null) && rs.wasNull()) { value = null; } return (value == null) ? null : convertUtilsBean.convert(value); }
From source file:CSVWriter.java
private static String getColumnValue(ResultSet rs, int colType, int colIndex) throws SQLException, IOException { String value = ""; /*from w w w . j a v a2s . co m*/ switch (colType) { case Types.BIT: Object bit = rs.getObject(colIndex); if (bit != null) { value = String.valueOf(bit); } break; case Types.BOOLEAN: boolean b = rs.getBoolean(colIndex); if (!rs.wasNull()) { value = Boolean.valueOf(b).toString(); } break; case Types.CLOB: Clob c = rs.getClob(colIndex); if (c != null) { value = read(c); } break; case Types.BIGINT: case Types.DECIMAL: case Types.DOUBLE: case Types.FLOAT: case Types.REAL: case Types.NUMERIC: BigDecimal bd = rs.getBigDecimal(colIndex); if (bd != null) { value = "" + bd.doubleValue(); } break; case Types.INTEGER: case Types.TINYINT: case Types.SMALLINT: int intValue = rs.getInt(colIndex); if (!rs.wasNull()) { value = "" + intValue; } break; case Types.JAVA_OBJECT: Object obj = rs.getObject(colIndex); if (obj != null) { value = String.valueOf(obj); } break; case Types.DATE: java.sql.Date date = rs.getDate(colIndex); if (date != null) { value = DATE_FORMATTER.format(date);; } break; case Types.TIME: Time t = rs.getTime(colIndex); if (t != null) { value = t.toString(); } break; case Types.TIMESTAMP: Timestamp tstamp = rs.getTimestamp(colIndex); if (tstamp != null) { value = TIMESTAMP_FORMATTER.format(tstamp); } break; case Types.LONGVARCHAR: case Types.VARCHAR: case Types.CHAR: value = rs.getString(colIndex); break; default: value = ""; } if (value == null) { value = ""; } return value; }
From source file:com.google.visualization.datasource.util.SqlDataSourceHelper.java
/** * Creates a table cell from the value in the current row of the given result * set and the given column index. The type of the value is determined by the * given value type./*from w w w. j a v a 2s. c o m*/ * * @param rs The result set holding the data from the sql table. The result * points to the current row. * @param valueType The value type of the column that the cell belongs to. * @param column The column index. Indexes are 0-based. * * @return The table cell. * * @throws SQLException Thrown when the connection to the database failed. */ private static TableCell buildTableCell(ResultSet rs, ValueType valueType, int column) throws SQLException { Value value = null; // SQL indexes are 1- based. column = column + 1; switch (valueType) { case BOOLEAN: value = BooleanValue.getInstance(rs.getBoolean(column)); break; case NUMBER: value = new NumberValue(rs.getDouble(column)); break; case DATE: Date date = rs.getDate(column); // If date is null it is handled later. if (date != null) { GregorianCalendar gc = new GregorianCalendar(TimeZone.getTimeZone("GMT")); // Set the year, month and date in the gregorian calendar. // Use the 'set' method with those parameters, and not the 'setTime' // method with the date parameter, since the Date object contains the // current time zone and it's impossible to change it to 'GMT'. gc.set(date.getYear() + 1900, date.getMonth(), date.getDate()); value = new DateValue(gc); } break; case DATETIME: Timestamp timestamp = rs.getTimestamp(column); // If timestamp is null it is handled later. if (timestamp != null) { GregorianCalendar gc = new GregorianCalendar(TimeZone.getTimeZone("GMT")); // Set the year, month, date, hours, minutes and seconds in the // gregorian calendar. Use the 'set' method with those parameters, // and not the 'setTime' method with the timestamp parameter, since // the Timestamp object contains the current time zone and it's // impossible to change it to 'GMT'. gc.set(timestamp.getYear() + 1900, timestamp.getMonth(), timestamp.getDate(), timestamp.getHours(), timestamp.getMinutes(), timestamp.getSeconds()); // Set the milliseconds explicitly, as they are not saved in the // underlying date. gc.set(Calendar.MILLISECOND, timestamp.getNanos() / 1000000); value = new DateTimeValue(gc); } break; case TIMEOFDAY: Time time = rs.getTime(column); // If time is null it is handled later. if (time != null) { GregorianCalendar gc = new GregorianCalendar(TimeZone.getTimeZone("GMT")); // Set the hours, minutes and seconds of the time in the gregorian // calendar. Set the year, month and date to be January 1 1970 like // in the Time object. // Use the 'set' method with those parameters, // and not the 'setTime' method with the time parameter, since // the Time object contains the current time zone and it's // impossible to change it to 'GMT'. gc.set(1970, Calendar.JANUARY, 1, time.getHours(), time.getMinutes(), time.getSeconds()); // Set the milliseconds explicitly, otherwise the milliseconds from // the time the gc was initialized are used. gc.set(GregorianCalendar.MILLISECOND, 0); value = new TimeOfDayValue(gc); } break; default: String colValue = rs.getString(column); if (colValue == null) { value = TextValue.getNullValue(); } else { value = new TextValue(rs.getString(column)); } break; } // Handle null values. if (rs.wasNull()) { return new TableCell(Value.getNullValueFromValueType(valueType)); } else { return new TableCell(value); } }
From source file:com.phideltcmu.recruiter.server.dao.mapper.IsAdminSetExtractor.java
@Override public Object extractData(ResultSet resultSet) throws SQLException, DataAccessException { return resultSet.getBoolean("isAdmin"); }
From source file:com.useekm.indexing.postgis.IndexedStatement.java
private static IndexedStatement convert(ResultSet results) throws SQLException { IndexedStatement result = new IndexedStatement(); int idx = 1;//from ww w. j a v a 2 s .c o m result.objectDate = results.getDate(idx++); result.objectLanguage = results.getString(idx++); result.objectSpatial = (PGgeometry) results.getObject(idx++); result.objectString = results.getString(idx++); result.objectTsVectorConfig = results.getString(idx++); result.objectType = results.getString(idx++); result.objectUri = results.getBoolean(idx++); result.predicate = results.getString(idx++); result.subject = results.getString(idx++); return result; }
From source file:at.alladin.rmbt.db.fields.BooleanField.java
@Override public void setField(final ResultSet rs) throws SQLException { value = rs.getBoolean(dbKey); if (rs.wasNull()) value = null;// w w w . j a va 2 s .c om }
From source file:org.waarp.common.database.data.AbstractDbData.java
/** * Get one value into DbValue from ResultSet * /*from www . j a v a 2s.c o m*/ * @param rs * @param value * @throws WaarpDatabaseSqlException */ static public void getTrueValue(ResultSet rs, DbValue value) throws WaarpDatabaseSqlException { try { switch (value.type) { case Types.VARCHAR: value.value = rs.getString(value.column); break; case Types.LONGVARCHAR: value.value = rs.getString(value.column); break; case Types.BIT: value.value = rs.getBoolean(value.column); break; case Types.TINYINT: value.value = rs.getByte(value.column); break; case Types.SMALLINT: value.value = rs.getShort(value.column); break; case Types.INTEGER: value.value = rs.getInt(value.column); break; case Types.BIGINT: value.value = rs.getLong(value.column); break; case Types.REAL: value.value = rs.getFloat(value.column); break; case Types.DOUBLE: value.value = rs.getDouble(value.column); break; case Types.VARBINARY: value.value = rs.getBytes(value.column); break; case Types.DATE: value.value = rs.getDate(value.column); break; case Types.TIMESTAMP: value.value = rs.getTimestamp(value.column); break; case Types.CLOB: value.value = rs.getClob(value.column).getCharacterStream(); break; case Types.BLOB: value.value = rs.getBlob(value.column).getBinaryStream(); break; default: throw new WaarpDatabaseSqlException("Type not supported: " + value.type + " for " + value.column); } } catch (SQLException e) { DbSession.error(e); throw new WaarpDatabaseSqlException("Getting values in error: " + value.type + " for " + value.column, e); } }
From source file:com.wabacus.system.datatype.BooleanType.java
public Object getColumnValue(ResultSet rs, int iindex, AbsDatabaseType dbtype) throws SQLException { return Boolean.valueOf(rs.getBoolean(iindex)); }
From source file:com.wabacus.system.datatype.BooleanType.java
public Object getColumnValue(ResultSet rs, String column, AbsDatabaseType dbtype) throws SQLException { return Boolean.valueOf(rs.getBoolean(column)); }
From source file:HelloMySQLJDBC.java
public void executeSQL() { try {/*ww w . j av a 2 s . com*/ Statement statement = connection.createStatement(); ResultSet rs = statement.executeQuery("SELECT * FROM bool"); while (rs.next()) { System.out.println(rs.getString("a") + " " + rs.getBoolean("a")); System.out.println(rs.getString("b") + " " + rs.getBoolean("b")); System.out.println(rs.getString("c") + " " + rs.getBoolean("c")); System.out.println(rs.getString("d") + " " + rs.getBoolean("d")); } rs.close(); statement.close(); connection.close(); } catch (SQLException e) { displaySQLErrors(e); } }