List of usage examples for java.sql ResultSet wasNull
boolean wasNull() throws SQLException;
NULL
. From source file:com.nabla.wapp.server.database.Database.java
/** * Get java <b>Integer</b> value from ResultSet (and not <b>int</b> value as it is the default!) * @param rs - query result//www . ja va2s .c o m * @param column - name of column * @return null if it was NULL, value otherwise * @throws SQLException */ public static Integer getInteger(final ResultSet rs, final String column) throws SQLException { Assert.argumentNotNull(rs); int value = rs.getInt(column); return rs.wasNull() ? null : value; }
From source file:com.flexive.core.LifeCycleInfoImpl.java
/** * Helper function for a less error prone and faster loading from the database * * @param rs ResultSet containing all the required info * @param creatorColumn column index of the create user reference * @param creationTimeColumn column index of the create timestamp * @param modificatorColumns column index of the modified by user reference * @param modificationTimeColumn column index of the modified by timestamp * @return LifeCycleInfo with the relevant data gathered from the ResultSet * @throws java.sql.SQLException if a column could not be read *//*from w w w . jav a2 s . co m*/ public static LifeCycleInfo load(ResultSet rs, int creatorColumn, int creationTimeColumn, int modificatorColumns, int modificationTimeColumn) throws SQLException { if (rs == null) { throw new IllegalArgumentException("Can not read from a null ResultSet!"); } int cid; long ct; int mid; long mt; long dTmp; cid = rs.getInt(creatorColumn); if (rs.wasNull()) { cid = NOT_DEFINED; } dTmp = rs.getLong(creationTimeColumn); ct = (rs.wasNull() ? NOT_DEFINED : dTmp); if (modificatorColumns < 0) { mid = NOT_DEFINED; } else { mid = rs.getInt(modificatorColumns); if (rs.wasNull()) mid = NOT_DEFINED; } if (modificationTimeColumn < 0) { mt = NOT_DEFINED; } else { dTmp = rs.getLong(modificationTimeColumn); mt = (rs.wasNull() ? NOT_DEFINED : dTmp); } return new LifeCycleInfoImpl(cid, ct, mid, mt); }
From source file:com.datamoin.tajo.tpcds.TpcDSTestUtil.java
public static String resultSetToString(ResultSet resultSet) throws SQLException { StringBuilder sb = new StringBuilder(); ResultSetMetaData rsmd = resultSet.getMetaData(); int numOfColumns = rsmd.getColumnCount(); for (int i = 1; i <= numOfColumns; i++) { if (i > 1) sb.append(","); String columnName = rsmd.getColumnName(i); sb.append(columnName);//from w w w . j av a2s . c o m } sb.append("\n-------------------------------\n"); while (resultSet.next()) { for (int i = 1; i <= numOfColumns; i++) { if (i > 1) sb.append(","); String columnValue = resultSet.getString(i); if (resultSet.wasNull()) { columnValue = "null"; } sb.append(columnValue); } sb.append("\n"); } return sb.toString(); }
From source file:com.act.lcms.db.model.CuratedChemical.java
protected static List<CuratedChemical> fromResultSet(ResultSet resultSet) throws SQLException { List<CuratedChemical> results = new ArrayList<>(); while (resultSet.next()) { Integer id = resultSet.getInt(DB_FIELD.ID.getOffset()); String name = resultSet.getString(DB_FIELD.NAME.getOffset()); String inchi = resultSet.getString(DB_FIELD.INCHI.getOffset()); Double mass = resultSet.getDouble(DB_FIELD.MASS.getOffset()); Integer expectedCollisionVoltage = resultSet.getInt(DB_FIELD.EXPECTED_COLLISION_VOLTAGE.getOffset()); if (resultSet.wasNull()) { expectedCollisionVoltage = null; }// w w w. ja v a2s.c o m String referenceUrl = resultSet.getString(DB_FIELD.REFERENCE_URL.getOffset()); results.add(new CuratedChemical(id, name, inchi, mass, expectedCollisionVoltage, referenceUrl)); } return results; }
From source file:com.sqewd.open.dal.core.persistence.db.EntityHelper.java
@SuppressWarnings({ "unchecked", "rawtypes" }) public static void setColumnValue(final ResultSet rs, final StructAttributeReflect attr, final AbstractEntity entity, final AbstractJoinGraph gr, final Stack<KeyValuePair<Class<?>>> path) throws Exception { KeyValuePair<String> alias = gr.getAliasFor(path, attr.Column, 0); String tabprefix = alias.getKey(); if (EnumPrimitives.isPrimitiveType(attr.Field.getType())) { EnumPrimitives prim = EnumPrimitives.type(attr.Field.getType()); switch (prim) { case ECharacter: String sv = rs.getString(tabprefix + "." + attr.Column); if (!rs.wasNull()) { PropertyUtils.setSimpleProperty(entity, attr.Field.getName(), sv.charAt(0)); }//from w w w .j a va 2 s. com break; case EShort: short shv = rs.getShort(tabprefix + "." + attr.Column); if (!rs.wasNull()) { PropertyUtils.setSimpleProperty(entity, attr.Field.getName(), shv); } break; case EInteger: int iv = rs.getInt(tabprefix + "." + attr.Column); if (!rs.wasNull()) { PropertyUtils.setSimpleProperty(entity, attr.Field.getName(), iv); } break; case ELong: long lv = rs.getLong(tabprefix + "." + attr.Column); if (!rs.wasNull()) { PropertyUtils.setSimpleProperty(entity, attr.Field.getName(), lv); } break; case EFloat: float fv = rs.getFloat(tabprefix + "." + attr.Column); if (!rs.wasNull()) { PropertyUtils.setSimpleProperty(entity, attr.Field.getName(), fv); } break; case EDouble: double dv = rs.getDouble(tabprefix + "." + attr.Column); if (!rs.wasNull()) { PropertyUtils.setSimpleProperty(entity, attr.Field.getName(), dv); } break; default: throw new Exception("Unsupported Data type [" + prim.name() + "]"); } } else if (attr.Convertor != null) { String value = rs.getString(tabprefix + "." + attr.Column); if (!rs.wasNull()) { attr.Convertor.load(entity, attr.Column, value); } } else if (attr.Field.getType().equals(String.class)) { String value = rs.getString(tabprefix + "." + attr.Column); if (!rs.wasNull()) { PropertyUtils.setSimpleProperty(entity, attr.Field.getName(), value); } } else if (attr.Field.getType().equals(Date.class)) { long value = rs.getLong(tabprefix + "." + attr.Column); if (!rs.wasNull()) { Date dt = new Date(value); PropertyUtils.setSimpleProperty(entity, attr.Field.getName(), dt); } } else if (attr.Field.getType().isEnum()) { String value = rs.getString(tabprefix + "." + attr.Column); if (!rs.wasNull()) { Class ecls = attr.Field.getType(); Object evalue = Enum.valueOf(ecls, value); PropertyUtils.setSimpleProperty(entity, attr.Field.getName(), evalue); } } else if (attr.Reference != null) { Class<?> rt = Class.forName(attr.Reference.Class); Object obj = rt.newInstance(); if (!(obj instanceof AbstractEntity)) throw new Exception("Unsupported Entity type [" + rt.getCanonicalName() + "]"); AbstractEntity rentity = (AbstractEntity) obj; if (path.size() > 0) { path.peek().setKey(attr.Column); } KeyValuePair<Class<?>> cls = new KeyValuePair<Class<?>>(); cls.setValue(rentity.getClass()); path.push(cls); setEntity(rentity, rs, gr, path); PropertyUtils.setSimpleProperty(entity, attr.Field.getName(), rentity); path.pop(); } }
From source file:com.sqewd.open.dal.core.persistence.db.EntityHelper.java
@SuppressWarnings({ "rawtypes", "unchecked" }) public static Object getColumnValue(final ResultSet rs, final StructAttributeReflect attr, final AbstractEntity entity, final AbstractJoinGraph gr, final Stack<KeyValuePair<Class<?>>> path) throws Exception { Object value = null;/*from w ww. ja v a 2 s .co m*/ KeyValuePair<String> alias = gr.getAliasFor(path, attr.Column, 0); String tabprefix = alias.getKey(); if (EnumPrimitives.isPrimitiveType(attr.Field.getType())) { EnumPrimitives prim = EnumPrimitives.type(attr.Field.getType()); switch (prim) { case ECharacter: String sv = rs.getString(tabprefix + "." + attr.Column); if (!rs.wasNull()) { PropertyUtils.setSimpleProperty(entity, attr.Field.getName(), sv.charAt(0)); } break; case EShort: short shv = rs.getShort(tabprefix + "." + attr.Column); if (!rs.wasNull()) { PropertyUtils.setSimpleProperty(entity, attr.Field.getName(), shv); } break; case EInteger: int iv = rs.getInt(tabprefix + "." + attr.Column); if (!rs.wasNull()) { PropertyUtils.setSimpleProperty(entity, attr.Field.getName(), iv); } break; case ELong: long lv = rs.getLong(tabprefix + "." + attr.Column); if (!rs.wasNull()) { PropertyUtils.setSimpleProperty(entity, attr.Field.getName(), lv); } break; case EFloat: float fv = rs.getFloat(tabprefix + "." + attr.Column); if (!rs.wasNull()) { PropertyUtils.setSimpleProperty(entity, attr.Field.getName(), fv); } break; case EDouble: double dv = rs.getDouble(tabprefix + "." + attr.Column); if (!rs.wasNull()) { PropertyUtils.setSimpleProperty(entity, attr.Field.getName(), dv); } break; default: throw new Exception("Unsupported Data type [" + prim.name() + "]"); } } else if (attr.Convertor != null) { // TODO : Not supported at this time. value = rs.getString(tabprefix + "." + attr.Column); } else if (attr.Field.getType().equals(String.class)) { value = rs.getString(tabprefix + "." + attr.Column); if (rs.wasNull()) { value = null; } } else if (attr.Field.getType().equals(Date.class)) { long lvalue = rs.getLong(tabprefix + "." + attr.Column); if (!rs.wasNull()) { Date dt = new Date(lvalue); value = dt; } } else if (attr.Field.getType().isEnum()) { String svalue = rs.getString(tabprefix + "." + attr.Column); if (!rs.wasNull()) { Class ecls = attr.Field.getType(); value = Enum.valueOf(ecls, svalue); } } else if (attr.Reference != null) { Class<?> rt = Class.forName(attr.Reference.Class); Object obj = rt.newInstance(); if (!(obj instanceof AbstractEntity)) throw new Exception("Unsupported Entity type [" + rt.getCanonicalName() + "]"); AbstractEntity rentity = (AbstractEntity) obj; if (path.size() > 0) { path.peek().setKey(attr.Column); } KeyValuePair<Class<?>> cls = new KeyValuePair<Class<?>>(); cls.setValue(rentity.getClass()); path.push(cls); setEntity(rentity, rs, gr, path); value = rentity; path.pop(); } return 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 a 2 s . 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.zimbra.cs.mailbox.util.MetadataDump.java
private static Row getItemRow(DbConnection conn, int groupId, int mboxId, int itemId, boolean fromDumpster) throws ServiceException { PreparedStatement stmt = null; ResultSet rs = null; try {// w ww . ja v a 2s .c o m String sql = "SELECT * FROM " + DbMailItem.getMailItemTableName(groupId, fromDumpster) + " WHERE mailbox_id = " + mboxId + " AND id = " + itemId; stmt = conn.prepareStatement(sql); rs = stmt.executeQuery(); if (!rs.next()) throw ServiceException.INVALID_REQUEST("No such item: mbox=" + mboxId + ", item=" + itemId, null); Row row = new Row(); ResultSetMetaData rsMeta = rs.getMetaData(); int cols = rsMeta.getColumnCount(); for (int i = 1; i <= cols; i++) { String colName = rsMeta.getColumnName(i); String colValue = rs.getString(i); if (rs.wasNull()) colValue = null; row.addColumn(colName, colValue); } return row; } catch (SQLException e) { throw ServiceException.INVALID_REQUEST("No such item: mbox=" + mboxId + ", item=" + itemId, e); } finally { DbPool.closeResults(rs); DbPool.closeStatement(stmt); } }
From source file:dk.netarkivet.common.utils.DBUtils.java
/** * Get a Long from the resultSet in column i. * @param rs the resultset/* w w w. j av a2 s. c om*/ * @param i the column where the wanted Long resides * @return a Long object located in column i in the resultset * @throws SQLException If the columnIndex is not valid, or a database * access error occurs or this method is called on a closed result set */ public static Long getLongMaybeNull(ResultSet rs, int i) throws SQLException { ArgumentNotValid.checkNotNull(rs, "ResultSet rs"); Long res = rs.getLong(i); if (rs.wasNull()) { return null; } return res; }
From source file:dk.netarkivet.common.utils.DBUtils.java
/** * Get an Integer from the resultSet in column i. * @param rs the resultset//from ww w. j a v a2s . c om * @param i the column where the wanted Integer resides * @return an Integer object located in column i in the resultset * @throws SQLException If the columnIndex is not valid, or a database * access error occurs or this method is called on a closed result set */ public static Integer getIntegerMaybeNull(ResultSet rs, int i) throws SQLException { ArgumentNotValid.checkNotNull(rs, "ResultSet rs"); Integer res = rs.getInt(i); if (rs.wasNull()) { return null; } return res; }