/** * @param ds the data source to keep the mapping information */ public void setDataSource(DataSource ds) { this.ds = ds; jdbc = new SimpleJdbcTemplate(this.ds); }
public List<String> get(String sourceType, String source, String targetType) { return jdbc.query("select "+COL_TARGET+" from "+TABLE+" where "+ COL_ID_TYPE+"=? and "+COL_ID+"=? and "+COL_TARGET_TYPE+"=?", STRING_MAPPER, sourceType, source, targetType); }
public boolean delete(String sourceType, String source, String targetType, String target) { int n = jdbc.update("delete from "+TABLE+" where "+COL_ID_TYPE+"=? and "+COL_ID+"=? and "+ COL_TARGET_TYPE+"=? and "+COL_TARGET+"=?", sourceType, source, targetType, target); if (n == 1) return true; return false; }
public class JdbcActorDao implements ActorDao { private SimpleJdbcTemplate simpleJdbcTemplate; public void setDataSource(DataSource dataSource) { this.simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource); } public int[] batchUpdate(final List<Actor> actors) { SqlParameterSource[] batch = SqlParameterSourceUtils.createBatch(actors.toArray()); int[] updateCounts = simpleJdbcTemplate.batchUpdate( "update t_actor set first_name = :firstName, last_name = :lastName where id = :id", batch); return updateCounts; } // ... additional methods }
public Map<String, Object> queryForMap(String sql, Object... args) throws DataAccessException { return (ObjectUtils.isEmpty(args) ? getJdbcOperations().queryForMap(sql) : getJdbcOperations().queryForMap(sql, getArguments(args))); }
public Conversation getConversation(int id) throws DataAccessException { try { MapSqlParameterSource params = new MapSqlParameterSource(); params.addValue(ID, id, Types.INTEGER); SimpleJdbcTemplate template = new SimpleJdbcTemplate( getNamedParameterJdbcTemplate()); return template.queryForObject(SELECT_SUMMARY, CONVERSATION_MAPPER, params); } catch (EmptyResultDataAccessException erdae) { return null; } }
public Collection<Integer> listConversationsSince(int id) throws DataAccessException { MapSqlParameterSource params = new MapSqlParameterSource(); try { params.addValue(ID, id, Types.INTEGER); SimpleJdbcTemplate template = new SimpleJdbcTemplate( getNamedParameterJdbcTemplate()); return template.query(SELECT_CONVERSATIONS, ID_MAPPER, params); } catch (EmptyResultDataAccessException erdae) { return Collections.emptyList(); } }
/** * This method purges all records in the Prior Year Account table in the DB. * * @return Number of records that were purged. */ public int purgePriorYearAccounts() { final String priorYrAcctTableName = getPersistenceStructureService().getTableName(PriorYearAccount.class); // 1. Count how many rows are currently in the prior year acct table int count = getSimpleJdbcTemplate().queryForInt("SELECT COUNT(1) FROM " + priorYrAcctTableName); // 2. Purge all the rows from the prior year acct table getSimpleJdbcTemplate().update("DELETE FROM " + priorYrAcctTableName); return count; }
@Repository public class DatabaseDAO { @Autowired private SimpleJdbcTemplate jdbcTemplate; public List<String> getAllRecords() { return jdbcTemplate.queryForObject("select record from my_table", List.class); } }
public int getMessageContentId(int headerId) throws DataAccessException { try { MapSqlParameterSource params = new MapSqlParameterSource(); params.addValue(ID, headerId, Types.INTEGER); SimpleJdbcTemplate template = new SimpleJdbcTemplate( getNamedParameterJdbcTemplate()); return template.queryForInt(SELECT_CONTENT_ID, params); } catch (EmptyResultDataAccessException erdae) { return -1; } }
@Override public FlowCollection getFlows(CriteriaAdapter criteria) { FlowCollection flowCollection = new FlowCollection(); final String whereClause = criteria.getWhereClause(); final String countQuery = dialect.getCountQuery().replaceAll(DatabaseDialect.SUBSTITUTION_STRING, (whereClause != null && whereClause.length() > 0) ? " AND " + whereClause : ""); int rowCount = getSimpleJdbcTemplate().queryForInt(countQuery, criteria); int offset = Integer.parseInt(criteria.getValue("offset").toString()); int limit = Integer.parseInt(criteria.getValue("limit").toString()); List<Flow> flows = null; if (offset < rowCount) { String dataQuery = dialect.getDataQuery(criteria); if ((rowCount - offset) < limit) limit = rowCount - offset; String soffset = String.valueOf(offset); String slimit = String.valueOf(limit); dataQuery = dataQuery.replaceAll(SUBSTITUTION_STRING_LIMIT, slimit).replaceAll(SUBSTITUTION_STRING_OFFSET, soffset); flows = getSimpleJdbcTemplate().query(dataQuery, flowMapper, criteria); } if(flows == null) flows = new ArrayList<Flow>(); flowCollection.setFlows(flows); flowCollection.setCount(rowCount); return flowCollection; }
private void conditionalShutdown() { if(!beenShutDown) { beenShutDown=true; log.debug("Shutdown is shutting down database."); jdbc.getJdbcOperations().execute("SHUTDOWN"); if(file!=null) { log.info("HSQL database (file: "+file+") has been shut down."); } else { log.info("HSQL database has been shut down."); } } }
/** * Determines if the currently inquiring user has associated temporary pending entries in the temporary pending entry table * * @param sessionId the unique web id of the inquiring user * @return true if this inquiring user has temporary pending entries, false otherwise */ protected boolean hasEntriesInPendingTable(String sessionId) { return getSimpleJdbcTemplate().queryForInt("select count(*) as COUNT from GL_PENDING_ENTRY_MT WHERE sesid = ?", sessionId) != 0; }
public int[] batchUpdate(String sql, List<Object[]> batchArgs) { return batchUpdate(sql, batchArgs, new int[0]); }
public byte[] loadMessageContent(int id) throws DataAccessException { try { MapSqlParameterSource params = new MapSqlParameterSource(); params.addValue(ID, id, Types.INTEGER); SimpleJdbcTemplate template = new SimpleJdbcTemplate( getNamedParameterJdbcTemplate()); return template.queryForObject(SELECT_CONTENT, CONTENT_MAPPER, params); } catch (EmptyResultDataAccessException erdae) { return null; } }
/** * This method purges all records in the Prior Year Organization table in the DB. * * @return Number of records that were purged. * @see org.kuali.kfs.coa.dataaccess.PriorYearOrganizationDao#purgePriorYearOrganizations() */ public int purgePriorYearOrganizations() { String priorYrOrgTableName = MetadataManager.getInstance().getGlobalRepository().getDescriptorFor(PriorYearOrganization.class).getFullTableName(); // 1. Count how many rows are currently in the prior year org table int count = getSimpleJdbcTemplate().queryForInt("SELECT COUNT(" + OBJ_ID + ") from " + priorYrOrgTableName); // 2. Purge all the rows from the prior year org table getSimpleJdbcTemplate().update("DELETE from " + priorYrOrgTableName); return count; }
public Spitter getSpitterById(long id) { return jdbcTemplate.queryForObject( SQL_SELECT_SPITTER_BY_ID, new ParameterizedRowMapper<Spitter>() { public Spitter mapRow(ResultSet rs, int rowNum) throws SQLException { Spitter spitter = new Spitter(); spitter.setId(rs.getLong(1)); spitter.setUsername(rs.getString(2)); spitter.setPassword(rs.getString(3)); spitter.setFullName(rs.getString(4)); return spitter; } }, id ); }
public int getMessageContentSize(int id) throws DataAccessException { try { MapSqlParameterSource params = new MapSqlParameterSource(); params.addValue(ID, id, Types.INTEGER); SimpleJdbcTemplate template = new SimpleJdbcTemplate( getNamedParameterJdbcTemplate()); return template.queryForInt(SELECT_CONTENT_SIZE, params); } catch (EmptyResultDataAccessException erdae) { return -1; } }
public class JdbcActorDao implements ActorDao { private SimpleJdbcTemplate simpleJdbcTemplate; public void setDataSource(DataSource dataSource) { this.simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource); } public int[] batchUpdate(final List<Actor> actors) { List<Object[]> batch = new ArrayList<Object[]>(); for (Actor actor : actors) { Object[] values = new Object[] { actor.getFirstName(), actor.getLastName(), actor.getId()}; batch.add(values); } int[] updateCounts = simpleJdbcTemplate.batchUpdate( "update t_actor set first_name = ?, last_name = ? where id = ?", batch); return updateCounts; } // ... additional methods }
public List<Map<String, Object>> queryForList(String sql, Object... args) throws DataAccessException { return (ObjectUtils.isEmpty(args) ? getJdbcOperations().queryForList(sql) : getJdbcOperations().queryForList(sql, getArguments(args))); }