Example usage for org.springframework.jdbc.core JdbcTemplate update

List of usage examples for org.springframework.jdbc.core JdbcTemplate update

Introduction

In this page you can find the example usage for org.springframework.jdbc.core JdbcTemplate update.

Prototype

@Override
    public int update(String sql, @Nullable Object... args) throws DataAccessException 

Source Link

Usage

From source file:org.sakaiproject.dash.dao.impl.DashboardDaoImpl.java

public boolean addAvailabilityCheck(AvailabilityCheck availabilityCheck) {
    if (log.isDebugEnabled()) {
        log.debug("addAvailabilityCheck( " + availabilityCheck.toString() + ")");
    }//from  w ww .  j  a v  a  2 s.c o  m

    // entity_ref, scheduled_time

    try {
        JdbcTemplate template = getJdbcTemplate();
        String sql = getStatement("insert.AvailabilityCheck");

        template.update(sql, new Object[] { availabilityCheck.getEntityReference(),
                availabilityCheck.getEntityTypeId(), availabilityCheck.getScheduledTime() });
        return true;
    } catch (DataIntegrityViolationException e) {
        // this means we're trying to insert a duplicate
        log.debug("addAvailabilityCheck() " + e);
        return false;
    } catch (DataAccessException ex) {
        log.warn("addAvailabilityCheck: Error executing query: " + ex.getClass() + ":" + ex.getMessage());
        return false;
    } catch (Exception e) {
        log.warn("addAvailabilityCheck: Error executing query: " + e.getClass() + ":" + e.getMessage());
        return false;
    }
}

From source file:org.sakaiproject.dash.dao.impl.DashboardDaoImpl.java

public boolean addCalendarItem(CalendarItem calendarItem) {
    if (log.isDebugEnabled()) {
        log.debug("addCalendarItem( " + calendarItem.toString() + ")");
    }//w  w  w. j  av  a2 s. c o  m

    // calendar_time, title , entity_url, entity_ref, source_type, context_id, realm_id

    String subtype = calendarItem.getSubtype();
    // DASH-191
    if (subtype != null && subtype.length() > MAX_LENGTH_SUBTYPE_FIELD) {
        StringBuilder buf = new StringBuilder();
        buf.append("addCalendarItem().  Truncating subtype ");
        buf.append(subtype);
        buf.append(" for entity ");
        buf.append(calendarItem.getEntityReference());
        log.warn(buf);
        subtype = subtype.substring(0, MAX_LENGTH_SUBTYPE_FIELD - 1);
    }
    try {
        JdbcTemplate template = getJdbcTemplate();
        Object[] params = null;
        String sql = null;
        if (calendarItem.getRepeatingCalendarItem() == null) {
            sql = getStatement("insert.CalendarItem");
            params = new Object[] { calendarItem.getCalendarTime(), calendarItem.getCalendarTimeLabelKey(),
                    calendarItem.getTitle(), calendarItem.getEntityReference(), subtype,
                    calendarItem.getSourceType().getId(), calendarItem.getContext().getId() };
        } else {
            sql = getStatement("insert.CalendarItem.repeats");
            params = new Object[] { calendarItem.getCalendarTime(), calendarItem.getCalendarTimeLabelKey(),
                    calendarItem.getTitle(), calendarItem.getEntityReference(), subtype,
                    calendarItem.getSourceType().getId(), calendarItem.getContext().getId(),
                    calendarItem.getRepeatingCalendarItem().getId(), calendarItem.getSequenceNumber() };
        }
        int result = template.update(sql, params);

        return result > 0;
    } catch (DataIntegrityViolationException e) {
        // this means we're trying to insert a duplicate
        log.warn("addCalendarItem() " + e);
    } catch (DataAccessException ex) {
        log.warn("addCalendarItem: Error executing query: " + ex.getClass() + ":" + ex.getMessage());
        // System.out.println("addCalendarItem: Error executing query: " + ex.getClass() + ":" + ex.getMessage());
    }
    return false;
}

From source file:org.sakaiproject.dash.dao.impl.DashboardDaoImpl.java

public boolean addNewsItem(NewsItem newsItem) {
    if (log.isDebugEnabled()) {
        log.debug("addNewsItem( " + newsItem.toString() + ")");
    }/*from  w ww .  ja  va2 s. com*/

    // news_time, title , entity_url, entity_ref, source_type, context_id, realm_id
    String subtype = newsItem.getSubtype();
    // DASH-191
    if (subtype != null && subtype.length() > MAX_LENGTH_SUBTYPE_FIELD) {
        StringBuilder buf = new StringBuilder();
        buf.append("addNewsItem().  Truncating subtype ");
        buf.append(subtype);
        buf.append(" for entity ");
        buf.append(newsItem.getEntityReference());
        log.warn(buf);
        subtype = subtype.substring(0, MAX_LENGTH_SUBTYPE_FIELD - 1);
    }

    try {
        JdbcTemplate template = getJdbcTemplate();
        template.update(getStatement("insert.NewsItem"),
                new Object[] { newsItem.getNewsTime(), newsItem.getTitle(), newsItem.getNewsTimeLabelKey(),
                        newsItem.getEntityReference(), subtype, newsItem.getSourceType().getId(),
                        newsItem.getContext().getId(), newsItem.getGroupingIdentifier() });

        return true;
    } catch (DataIntegrityViolationException e) {
        // this means we're trying to insert a duplicate
        log.debug("addNewsItem() " + e);
    } catch (DataAccessException ex) {
        log.warn("addNewsItem: Error executing query: " + ex.getClass() + ":" + ex.getMessage());
    }
    return false;
}

From source file:org.sakaiproject.dash.dao.impl.DashboardDaoImpl.java

public boolean updateCalendarItem(CalendarItem calendarItem) {
    if (log.isDebugEnabled()) {
        log.debug("updateCalendarItem( " + calendarItem + ")");
    }/*from   w w  w .j a  v a2  s  . c om*/

    String subtype = calendarItem.getSubtype();
    // DASH-191
    if (subtype != null && subtype.length() > MAX_LENGTH_SUBTYPE_FIELD) {
        StringBuilder buf = new StringBuilder();
        buf.append("addCalendarItem().  Truncating subtype ");
        buf.append(subtype);
        buf.append(" for entity ");
        buf.append(calendarItem.getEntityReference());
        log.warn(buf);
        subtype = subtype.substring(0, MAX_LENGTH_SUBTYPE_FIELD - 1);
    }

    JdbcTemplate template = getJdbcTemplate();
    Object[] params = null;
    String sql = null;
    if (calendarItem.getRepeatingCalendarItem() == null) {
        sql = getStatement("update.CalendarItem");
        params = new Object[] { calendarItem.getCalendarTime(), calendarItem.getCalendarTimeLabelKey(),
                calendarItem.getTitle(), calendarItem.getEntityReference(), subtype,
                calendarItem.getSourceType().getId(), calendarItem.getContext().getId(), calendarItem.getId() };
    } else {
        sql = getStatement("update.CalendarItem.repeats");
        params = new Object[] { calendarItem.getCalendarTime(), calendarItem.getCalendarTimeLabelKey(),
                calendarItem.getTitle(), calendarItem.getEntityReference(), subtype,
                calendarItem.getSourceType().getId(), calendarItem.getContext().getId(),
                calendarItem.getRepeatingCalendarItem().getId(), calendarItem.getSequenceNumber(),
                calendarItem.getId() };
    }

    template.update(sql, params);
    return true;
}

From source file:org.sakaiproject.dash.dao.impl.DashboardDaoImpl.java

public void setConfigProperty(String propertyName, Integer propertyValue) {

    if (log.isDebugEnabled()) {
        log.debug("setConfigProperty( " + propertyName + "," + propertyValue + ")");
    }/*from ww w.  ja v a 2  s  . co  m*/

    // insert into dash_config (property_name, property_value) values (?, ?)
    String sql_insert = getStatement("insert.Config");
    Object[] params_insert = new Object[] { propertyName, propertyValue };

    // update dash_config set property_value=? where property_name=?
    String sql_update = getStatement("update.Config.propertyName");
    Object[] params_update = new Object[] { propertyValue, propertyName };

    JdbcTemplate jdbcTemplate = getJdbcTemplate();
    try {
        jdbcTemplate.update(sql_insert, params_insert);
    } catch (DataIntegrityViolationException e) {
        // this means we're trying to insert a duplicate
        log.debug("setConfigProperty() " + e);
    } catch (Exception e) {
        // insert failed -- try update instead of insert
        try {
            jdbcTemplate.update(sql_update, params_update);
        } catch (DataAccessException ex) {
            log.warn("setConfigProperty: Error executing query: " + ex.getClass() + ":" + ex.getMessage());
        } catch (Exception ex) {
            log.warn("setConfigProperty: Error executing query: " + ex.getClass() + ":" + ex.getMessage());
        }
    }

}

From source file:org.springframework.batch.item.database.IbatisPagingItemReaderAsyncTests.java

@After
public void destroy() {
    JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
    jdbcTemplate.update("DELETE from T_FOOS where ID>?", maxId);
}

From source file:org.springframework.jdbc.core.JdbcTemplateTests.java

/**
 * Test update with dynamic SQL./*  w w w  . j a v  a2 s  .com*/
 */
public void testSqlUpdateWithArguments() throws Exception {
    final String sql = "UPDATE NOSUCHTABLE SET DATE_DISPATCHED = SYSDATE WHERE ID = ? and PR = ?";
    int rowsAffected = 33;

    MockControl ctrlStatement = MockControl.createControl(PreparedStatement.class);
    PreparedStatement mockStatement = (PreparedStatement) ctrlStatement.getMock();
    mockStatement.setObject(1, new Integer(4));
    ctrlStatement.setVoidCallable();
    mockStatement.setObject(2, new Float(1.4142), Types.NUMERIC, 2);
    ctrlStatement.setVoidCallable();
    mockStatement.executeUpdate();
    ctrlStatement.setReturnValue(33);
    if (debugEnabled) {
        mockStatement.getWarnings();
        ctrlStatement.setReturnValue(null);
    }
    mockStatement.close();
    ctrlStatement.setVoidCallable();

    mockConnection.prepareStatement(sql);
    ctrlConnection.setReturnValue(mockStatement);
    ctrlStatement.replay();
    replay();

    JdbcTemplate template = new JdbcTemplate(mockDataSource);
    int actualRowsAffected = template.update(sql,
            new Object[] { new Integer(4), new SqlParameterValue(Types.NUMERIC, 2, new Float(1.4142)) });
    assertTrue("Actual rows affected is correct", actualRowsAffected == rowsAffected);
    ctrlStatement.verify();
}

From source file:org.springframework.test.jdbc.JdbcTestUtils.java

/**
 * Delete rows from the given table, using the provided {@code WHERE} clause.
 * <p>If the provided {@code WHERE} clause contains text, it will be prefixed
 * with {@code " WHERE "} and then appended to the generated {@code DELETE}
 * statement. For example, if the provided table name is {@code "person"} and
 * the provided where clause is {@code "name = 'Bob' and age > 25"}, the
 * resulting SQL statement to execute will be
 * {@code "DELETE FROM person WHERE name = 'Bob' and age > 25"}.
 * <p>As an alternative to hard-coded values, the {@code "?"} placeholder can
 * be used within the {@code WHERE} clause, binding to the given arguments.
 * @param jdbcTemplate the JdbcTemplate with which to perform JDBC operations
 * @param tableName the name of the table to delete rows from
 * @param whereClause the {@code WHERE} clause to append to the query
 * @param args arguments to bind to the query (leaving it to the PreparedStatement
 * to guess the corresponding SQL type); may also contain {@link SqlParameterValue}
 * objects which indicate not only the argument value but also the SQL type and
 * optionally the scale./*from   ww w.  j a va2  s . c om*/
 * @return the number of rows deleted from the table
 */
public static int deleteFromTableWhere(JdbcTemplate jdbcTemplate, String tableName, String whereClause,
        Object... args) {

    String sql = "DELETE FROM " + tableName;
    if (StringUtils.hasText(whereClause)) {
        sql += " WHERE " + whereClause;
    }
    int rowCount = (args.length > 0 ? jdbcTemplate.update(sql, args) : jdbcTemplate.update(sql));
    if (logger.isInfoEnabled()) {
        logger.info("Deleted " + rowCount + " rows from table " + tableName);
    }
    return rowCount;
}

From source file:wherehows.dao.table.DatasetsDao.java

/**
 * Update dataset owners, set removed owners as deleted, and update existing owner information.
 * @param jdbcTemplate JdbcTemplate//from  www .  j  a  v  a 2  s .c  o  m
 * @param user String
 * @param datasetId int
 * @param datasetUrn String
 * @param owners List<DatasetOwner>
 * @throws Exception
 */
public void updateDatasetOwners(JdbcTemplate jdbcTemplate, String user, int datasetId, String datasetUrn,
        List<DatasetOwner> owners) throws Exception {
    // first mark existing owners as deleted, new owners will be updated later
    jdbcTemplate.update(MARK_DATASET_OWNERS_AS_DELETED, datasetId);

    if (owners.size() > 0) {
        updateDatasetOwnerDatabase(jdbcTemplate, datasetId, datasetUrn, owners);
    }
}