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:ca.nrc.cadc.vos.server.NodeMapper.java
/** * Map the row to the appropriate type of node object. * @param rs/*from w w w . ja va 2 s.com*/ * @param row * @return a Node * @throws SQLException */ public Object mapRow(ResultSet rs, int row) throws SQLException { long nodeID = rs.getLong("nodeID"); String name = rs.getString("name"); String type = rs.getString("type"); String busyString = rs.getString("busyState"); String groupRead = rs.getString("groupRead"); String groupWrite = rs.getString("groupWrite"); boolean isPublic = rs.getBoolean("isPublic"); boolean isLocked = rs.getBoolean("isLocked"); Object ownerObject = rs.getObject("ownerID"); String contentType = rs.getString("contentType"); String contentEncoding = rs.getString("contentEncoding"); String link = null; Long contentLength = null; Object o = rs.getObject("contentLength"); if (o != null) { Number n = (Number) o; contentLength = new Long(n.longValue()); } log.debug("readNode: contentLength = " + contentLength); Object contentMD5 = rs.getObject("contentMD5"); Date lastModified = rs.getTimestamp("lastModified", cal); String path = basePath + "/" + name; VOSURI vos; try { vos = new VOSURI(new URI("vos", authority, path, null, null)); } catch (URISyntaxException bug) { throw new RuntimeException("BUG - failed to create vos URI", bug); } Node node; if (NodeDAO.NODE_TYPE_CONTAINER.equals(type)) { node = new ContainerNode(vos); } else if (NodeDAO.NODE_TYPE_DATA.equals(type)) { node = new DataNode(vos); ((DataNode) node).setBusy(NodeBusyState.getStateFromValue(busyString)); } else if (NodeDAO.NODE_TYPE_LINK.equals(type)) { link = rs.getString("link"); try { node = new LinkNode(vos, new URI(link)); } catch (URISyntaxException bug) { throw new RuntimeException("BUG - failed to create link URI", bug); } } else { throw new IllegalStateException("Unknown node database type: " + type); } NodeID nid = new NodeID(); nid.id = nodeID; nid.ownerObject = ownerObject; node.appData = nid; if (contentType != null && contentType.trim().length() > 0) { node.getProperties().add(new NodeProperty(VOS.PROPERTY_URI_TYPE, contentType)); } if (contentEncoding != null && contentEncoding.trim().length() > 0) { node.getProperties().add(new NodeProperty(VOS.PROPERTY_URI_CONTENTENCODING, contentEncoding)); } if (contentLength != null) node.getProperties().add(new NodeProperty(VOS.PROPERTY_URI_CONTENTLENGTH, contentLength.toString())); else node.getProperties().add(new NodeProperty(VOS.PROPERTY_URI_CONTENTLENGTH, "0")); if (contentMD5 != null && contentMD5 instanceof byte[]) { byte[] md5 = (byte[]) contentMD5; if (md5.length < 16) { byte[] tmp = md5; md5 = new byte[16]; System.arraycopy(tmp, 0, md5, 0, tmp.length); // extra space is init with 0 } String contentMD5String = HexUtil.toHex(md5); node.getProperties().add(new NodeProperty(VOS.PROPERTY_URI_CONTENTMD5, contentMD5String)); } if (lastModified != null) { node.getProperties().add(new NodeProperty(VOS.PROPERTY_URI_DATE, dateFormat.format(lastModified))); } if (groupRead != null && groupRead.trim().length() > 0) { node.getProperties().add(new NodeProperty(VOS.PROPERTY_URI_GROUPREAD, groupRead)); } if (groupWrite != null && groupWrite.trim().length() > 0) { node.getProperties().add(new NodeProperty(VOS.PROPERTY_URI_GROUPWRITE, groupWrite)); } node.getProperties().add(new NodeProperty(VOS.PROPERTY_URI_ISPUBLIC, isPublic ? "true" : "false")); if (isLocked) node.getProperties().add(new NodeProperty(VOS.PROPERTY_URI_ISLOCKED, isLocked ? "true" : "false")); // set the read-only flag on the properties for (String propertyURI : VOS.READ_ONLY_PROPERTIES) { int propertyIndex = node.getProperties().indexOf(new NodeProperty(propertyURI, "")); if (propertyIndex != -1) { node.getProperties().get(propertyIndex).setReadOnly(true); } } log.debug("read: " + node.getUri() + "," + node.appData); return node; }
From source file:org.traccar.database.QueryBuilder.java
private <T> void addProcessors(List<ResultSetProcessor<T>> processors, final Class<?> parameterType, final Method method, final String name) { if (parameterType.equals(boolean.class)) { processors.add(new ResultSetProcessor<T>() { @Override/*from w w w . j a v a 2 s . c o m*/ public void process(T object, ResultSet resultSet) throws SQLException { try { method.invoke(object, resultSet.getBoolean(name)); } catch (IllegalAccessException | InvocationTargetException error) { Log.warning(error); } } }); } else if (parameterType.equals(int.class)) { processors.add(new ResultSetProcessor<T>() { @Override public void process(T object, ResultSet resultSet) throws SQLException { try { method.invoke(object, resultSet.getInt(name)); } catch (IllegalAccessException | InvocationTargetException error) { Log.warning(error); } } }); } else if (parameterType.equals(long.class)) { processors.add(new ResultSetProcessor<T>() { @Override public void process(T object, ResultSet resultSet) throws SQLException { try { method.invoke(object, resultSet.getLong(name)); } catch (IllegalAccessException | InvocationTargetException error) { Log.warning(error); } } }); } else if (parameterType.equals(double.class)) { processors.add(new ResultSetProcessor<T>() { @Override public void process(T object, ResultSet resultSet) throws SQLException { try { method.invoke(object, resultSet.getDouble(name)); } catch (IllegalAccessException | InvocationTargetException error) { Log.warning(error); } } }); } else if (parameterType.equals(String.class)) { processors.add(new ResultSetProcessor<T>() { @Override public void process(T object, ResultSet resultSet) throws SQLException { try { method.invoke(object, resultSet.getString(name)); } catch (IllegalAccessException | InvocationTargetException error) { Log.warning(error); } } }); } else if (parameterType.equals(Date.class)) { processors.add(new ResultSetProcessor<T>() { @Override public void process(T object, ResultSet resultSet) throws SQLException { try { Timestamp timestamp = resultSet.getTimestamp(name); if (timestamp != null) { method.invoke(object, new Date(timestamp.getTime())); } } catch (IllegalAccessException | InvocationTargetException error) { Log.warning(error); } } }); } else if (parameterType.equals(byte[].class)) { processors.add(new ResultSetProcessor<T>() { @Override public void process(T object, ResultSet resultSet) throws SQLException { try { method.invoke(object, resultSet.getBytes(name)); } catch (IllegalAccessException | InvocationTargetException error) { Log.warning(error); } } }); } else { processors.add(new ResultSetProcessor<T>() { @Override public void process(T object, ResultSet resultSet) throws SQLException { String value = resultSet.getString(name); if (value != null && !value.isEmpty()) { try { method.invoke(object, Context.getObjectMapper().readValue(value, parameterType)); } catch (InvocationTargetException | IllegalAccessException | IOException error) { Log.warning(error); } } } }); } }
From source file:dk.netarkivet.harvester.datamodel.GlobalCrawlerTrapListDBDAO.java
@Override public GlobalCrawlerTrapList read(int id) { Connection conn = HarvestDBConnection.get(); PreparedStatement stmt = null; try {//from w w w. jav a 2 s . c om stmt = conn.prepareStatement(SELECT_TRAPLIST_STMT); stmt.setInt(1, id); ResultSet rs = stmt.executeQuery(); if (!rs.next()) { throw new UnknownID("No such GlobalCrawlerTrapList: '" + id + "'"); } String name = rs.getString("name"); String description = rs.getString("description"); boolean isActive = rs.getBoolean("isActive"); stmt.close(); stmt = conn.prepareStatement(SELECT_TRAP_EXPRESSIONS_STMT); stmt.setInt(1, id); rs = stmt.executeQuery(); List<String> exprs = new ArrayList<String>(); while (rs.next()) { exprs.add(rs.getString("trap_expression")); } return new GlobalCrawlerTrapList(id, exprs, name, description, isActive); } catch (SQLException e) { String message = "Error retrieving trap list for id '" + id + "'\n" + ExceptionUtils.getSQLExceptionCause(e); log.warn(message, e); throw new IOFailure(message, e); } finally { DBUtils.closeStatementIfOpen(stmt); HarvestDBConnection.release(conn); } }
From source file:mupomat.controller.ObradaOperater.java
@Override public List<Operater> dohvatiIzBaze(String uvjet) { List<Operater> lista = new ArrayList<>(); try {//from w w w .j a va 2s . c om Connection veza = MySqlBazaPodataka.getConnection(); PreparedStatement izraz = veza.prepareStatement( "select * from operater where korisnickoime like ? or ime like ? or prezime like ?"); izraz.setString(1, "%" + uvjet + "%"); izraz.setString(2, "%" + uvjet + "%"); izraz.setString(3, "%" + uvjet + "%"); ResultSet rs = izraz.executeQuery(); Operater operater = null; while (rs.next()) { //System.out.println("evo me"); operater = new Operater(); operater.setSifra(rs.getInt("sifra")); operater.setIme(rs.getString("ime")); operater.setPrezime(rs.getString("prezime")); operater.setKorisnickoIme(rs.getString("korisnickoime")); operater.setAktivan(rs.getBoolean("aktivan")); lista.add(operater); } rs.close(); izraz.close(); veza.close(); } catch (Exception e) { // System.out.println(e.getMessage()); e.printStackTrace(); return null; } return lista; }
From source file:com.linkage.community.schedule.dbutils.CustomScalarHandler.java
/** * Returns one <code>ResultSet</code> column as an object via the * <code>ResultSet.getObject()</code> method that performs type * conversions./*from w w w. j a v a2s. co m*/ * @param rs <code>ResultSet</code> to process. * @return The column or <code>null</code> if there are no rows in * the <code>ResultSet</code>. * * @throws SQLException if a database access error occurs * @throws ClassCastException if the class datatype does not match the column type * * @see org.apache.commons.dbutils.ResultSetHandler#handle(java.sql.ResultSet) */ // We assume that the user has picked the correct type to match the column // so getObject will return the appropriate type and the cast will succeed. @SuppressWarnings("unchecked") //@Override public T handle(ResultSet rs) throws SQLException { Object obj = null; if (rs.next()) { if (this.columnName == null) { obj = rs.getObject(this.columnIndex); if (obj instanceof Integer) return (T) (obj = rs.getInt(columnIndex)); else if (obj instanceof Long) return (T) (obj = (new Long(rs.getLong(columnIndex)).intValue())); else if (obj instanceof Boolean) return (T) (obj = rs.getBoolean(columnIndex)); else if (obj instanceof Double) return (T) (obj = rs.getDouble(columnIndex)); else if (obj instanceof Float) return (T) (obj = rs.getFloat(columnIndex)); else if (obj instanceof Short) return (T) (obj = rs.getShort(columnIndex)); else if (obj instanceof Byte) return (T) (obj = rs.getByte(columnIndex)); else return (T) obj; } else { obj = rs.getObject(this.columnName); if (obj instanceof Integer) return (T) (obj = rs.getInt(columnName)); else if (obj instanceof Long) return (T) (obj = rs.getLong(columnName)); else if (obj instanceof Boolean) return (T) (obj = rs.getBoolean(columnName)); else if (obj instanceof Double) return (T) (obj = rs.getDouble(columnName)); else if (obj instanceof Float) return (T) (obj = rs.getFloat(columnName)); else if (obj instanceof Short) return (T) (obj = rs.getShort(columnName)); else if (obj instanceof Byte) return (T) (obj = rs.getByte(columnName)); else return (T) obj; } } return null; }
From source file:com.butler.service.OrderDao.java
public List<OrderDetails> getPendingOrders() { String sql = "select name, o.number, address, o.feedback,o.product, o.quantity, o.immediate,o.stamp as time from orders o,(select name,address,number from user)as y where o.number = y.number and o.status='TODO' order by time desc"; //return this.getJdbcTemplate().queryForList(sql, OrderDetails.class); return this.getJdbcTemplate().query(sql, new RowMapper() { public Object mapRow(ResultSet rs, int i) throws SQLException { OrderDetails details = new OrderDetails(); details.setName(rs.getString("name")); details.setNumber(rs.getString("number")); details.setAddress(rs.getString("address")); details.setProduct(rs.getString("product")); details.setQuantity(rs.getString("quantity")); Timestamp time = rs.getTimestamp("time"); details.setTime(Util.getIndianTime(time)); details.setFeedback(rs.getString("feedback")); details.setImmediate(rs.getBoolean("immediate")); return details; }//from w w w . ja v a 2 s .c o m }); }
From source file:cz.lbenda.dataman.db.RowDesc.java
/** Load initial column value from rs */ public void loadInitialColumnValue(ColumnDesc columnDesc, ResultSet rs) throws SQLException { Object val; if (columnDesc.getDataType() == ColumnType.BIT) { val = rs.getBoolean(columnDesc.getPosition()); if (Boolean.TRUE.equals(val)) { val = (byte) 1; } else if (Boolean.FALSE.equals(val)) { val = (byte) 0; }// www . j a v a2 s . co m } else if (columnDesc.getDataType() == ColumnType.BIT_ARRAY) { val = rs.getBytes(columnDesc.getPosition()); } else { val = rs.getObject(columnDesc.getPosition()); } setInitialColumnValue(columnDesc, val); }
From source file:com.sfs.whichdoctor.dao.EmailRecipientDAOImpl.java
/** * Load email recipient.//from w ww .j a v a 2 s.c o m * * @param rs the rs * @return the email recipient bean * @throws SQLException the sQL exception */ private EmailRecipientBean loadEmailRecipient(final ResultSet rs) throws SQLException { EmailRecipientBean emailRecipient = new EmailRecipientBean(); emailRecipient.setReferenceGUID(rs.getInt("ReferenceGUID")); emailRecipient.setPersonGUID(rs.getInt("PersonGUID")); emailRecipient.setOrganisationGUID(rs.getInt("OrganisationGUID")); emailRecipient.setCustomEmailAddress(rs.getString("CustomEmailAddress")); emailRecipient.setPending(rs.getBoolean("Pending")); emailRecipient.setSent(rs.getBoolean("Sent")); emailRecipient.setFailed(rs.getBoolean("Failed")); emailRecipient.setDeliverOutOfHours(rs.getBoolean("DeliverOutOfHours")); if (emailRecipient.getPersonGUID() > 0) { emailRecipient.setName(rs.getString("PersonName")); emailRecipient.setOrder(rs.getString("PersonOrder")); } if (emailRecipient.getOrganisationGUID() > 0) { emailRecipient.setName(rs.getString("OrganisationName")); emailRecipient.setOrder(rs.getString("OrganisationOrder")); } emailRecipient.setLogMessage(rs.getString("LogMessage")); return emailRecipient; }
From source file:net.algem.security.UserDaoImpl.java
@Override public List<Map<String, Boolean>> listMenuAccess(int userId) { String query = "SELECT m.label, a.autorisation FROM menu2 m JOIN menuaccess a ON m.id = a.idmenu WHERE a.idper = ?"; return jdbcTemplate.query(query, new RowMapper<Map<String, Boolean>>() { @Override/*w w w. j av a 2s .com*/ public Map<String, Boolean> mapRow(ResultSet rs, int rowNum) throws SQLException { Map<String, Boolean> map = new HashMap<String, Boolean>(); map.put(rs.getString(1), rs.getBoolean(2)); return map; } }, userId); }
From source file:com.oltpbenchmark.catalog.Catalog.java
/** * Construct the set of Table objects from a given Connection handle * @param conn/* w w w . j av a 2 s .c o m*/ * @return * @throws SQLException * @see http://docs.oracle.com/javase/6/docs/api/java/sql/DatabaseMetaData.html */ protected void init() throws SQLException { // Load the database's DDL this.benchmark.createDatabase(DB_TYPE, this.conn); // TableName -> ColumnName -> <FkeyTable, FKeyColumn> Map<String, Map<String, Pair<String, String>>> foreignKeys = new HashMap<String, Map<String, Pair<String, String>>>(); DatabaseMetaData md = conn.getMetaData(); ResultSet table_rs = md.getTables(null, null, null, new String[] { "TABLE" }); while (table_rs.next()) { if (LOG.isDebugEnabled()) LOG.debug(SQLUtil.debug(table_rs)); String internal_table_name = table_rs.getString(3); String table_name = origTableNames.get(table_rs.getString(3).toUpperCase()); assert (table_name != null) : "Unexpected table '" + table_rs.getString(3) + "' from catalog"; LOG.debug(String.format("ORIG:%s -> CATALOG:%s", internal_table_name, table_name)); String table_type = table_rs.getString(4); if (table_type.equalsIgnoreCase("TABLE") == false) continue; Table catalog_tbl = new Table(table_name); // COLUMNS if (LOG.isDebugEnabled()) LOG.debug("Retrieving COLUMN information for " + table_name); ResultSet col_rs = md.getColumns(null, null, internal_table_name, null); while (col_rs.next()) { if (LOG.isTraceEnabled()) LOG.trace(SQLUtil.debug(col_rs)); String col_name = col_rs.getString(4); int col_type = col_rs.getInt(5); String col_typename = col_rs.getString(6); Integer col_size = col_rs.getInt(7); String col_defaultValue = col_rs.getString(13); boolean col_nullable = col_rs.getString(18).equalsIgnoreCase("YES"); boolean col_autoinc = false; // FIXME col_rs.getString(22).toUpperCase().equals("YES"); Column catalog_col = new Column(catalog_tbl, col_name, col_type, col_typename, col_size); catalog_col.setDefaultValue(col_defaultValue); catalog_col.setAutoincrement(col_autoinc); catalog_col.setNullable(col_nullable); // FIXME col_catalog.setSigned(); if (LOG.isDebugEnabled()) LOG.debug( String.format("Adding %s.%s [%s / %d]", table_name, col_name, col_typename, col_type)); catalog_tbl.addColumn(catalog_col); } // WHILE col_rs.close(); // PRIMARY KEYS if (LOG.isDebugEnabled()) LOG.debug("Retrieving PRIMARY KEY information for " + table_name); ResultSet pkey_rs = md.getPrimaryKeys(null, null, internal_table_name); SortedMap<Integer, String> pkey_cols = new TreeMap<Integer, String>(); while (pkey_rs.next()) { String col_name = pkey_rs.getString(4); assert (catalog_tbl.getColumnByName(col_name) != null) : String .format("Unexpected primary key column %s.%s", table_name, col_name); int col_idx = pkey_rs.getShort(5); // HACK: SQLite doesn't return the KEY_SEQ, so if we get back // a zero for this value, then we'll just length of the pkey_cols map if (col_idx == 0) col_idx = pkey_cols.size(); LOG.debug(String.format("PKEY[%02d]: %s.%s", col_idx, table_name, col_name)); assert (pkey_cols.containsKey(col_idx) == false); pkey_cols.put(col_idx, col_name); } // WHILE pkey_rs.close(); catalog_tbl.setPrimaryKeyColumns(pkey_cols.values()); // INDEXES if (LOG.isDebugEnabled()) LOG.debug("Retrieving INDEX information for " + table_name); ResultSet idx_rs = md.getIndexInfo(null, null, internal_table_name, false, false); while (idx_rs.next()) { if (LOG.isDebugEnabled()) LOG.debug(SQLUtil.debug(idx_rs)); boolean idx_unique = (idx_rs.getBoolean(4) == false); String idx_name = idx_rs.getString(6); int idx_type = idx_rs.getShort(7); int idx_col_pos = idx_rs.getInt(8) - 1; String idx_col_name = idx_rs.getString(9); String sort = idx_rs.getString(10); SortDirectionType idx_direction; if (sort != null) { idx_direction = sort.equalsIgnoreCase("A") ? SortDirectionType.ASC : SortDirectionType.DESC; } else idx_direction = null; Index catalog_idx = catalog_tbl.getIndex(idx_name); if (catalog_idx == null) { catalog_idx = new Index(catalog_tbl, idx_name, idx_type, idx_unique); catalog_tbl.addIndex(catalog_idx); } assert (catalog_idx != null); catalog_idx.addColumn(idx_col_name, idx_direction, idx_col_pos); } // WHILE idx_rs.close(); // FOREIGN KEYS if (LOG.isDebugEnabled()) LOG.debug("Retrieving FOREIGN KEY information for " + table_name); ResultSet fk_rs = md.getImportedKeys(null, null, internal_table_name); foreignKeys.put(table_name, new HashMap<String, Pair<String, String>>()); while (fk_rs.next()) { if (LOG.isDebugEnabled()) LOG.debug(table_name + " => " + SQLUtil.debug(fk_rs)); assert (fk_rs.getString(7).equalsIgnoreCase(table_name)); String colName = fk_rs.getString(8); String fk_tableName = origTableNames.get(fk_rs.getString(3).toUpperCase()); String fk_colName = fk_rs.getString(4); foreignKeys.get(table_name).put(colName, Pair.of(fk_tableName, fk_colName)); } // WHILE fk_rs.close(); tables.put(table_name, catalog_tbl); } // WHILE table_rs.close(); // FOREIGN KEYS if (LOG.isDebugEnabled()) LOG.debug("Foreign Key Mappings:\n" + StringUtil.formatMaps(foreignKeys)); for (Table catalog_tbl : tables.values()) { Map<String, Pair<String, String>> fk = foreignKeys.get(catalog_tbl.getName()); for (Entry<String, Pair<String, String>> e : fk.entrySet()) { String colName = e.getKey(); Column catalog_col = catalog_tbl.getColumnByName(colName); assert (catalog_col != null); Pair<String, String> fkey = e.getValue(); assert (fkey != null); Table fkey_tbl = tables.get(fkey.first); if (fkey_tbl == null) { throw new RuntimeException("Unexpected foreign key parent table " + fkey); } Column fkey_col = fkey_tbl.getColumnByName(fkey.second); if (fkey_col == null) { throw new RuntimeException("Unexpected foreign key parent column " + fkey); } if (LOG.isDebugEnabled()) LOG.debug(catalog_col.fullName() + " -> " + fkey_col.fullName()); catalog_col.setForeignKey(fkey_col); } // FOR } // FOR return; }