Example usage for org.springframework.jdbc.core.simple SimpleJdbcInsert execute

List of usage examples for org.springframework.jdbc.core.simple SimpleJdbcInsert execute

Introduction

In this page you can find the example usage for org.springframework.jdbc.core.simple SimpleJdbcInsert execute.

Prototype

@Override
    public int execute(SqlParameterSource parameterSource) 

Source Link

Usage

From source file:main.java.net.bornil.db.service.jdbc.JdbcUserDao.java

@Override
public int createUser(User user) {
    SimpleJdbcInsert insertUser = new SimpleJdbcInsert(this.jdbcTemplate);
    insertUser.withTableName("USERS");
    insertUser.usingColumns("USERNAME", "PASSWORD", "ENABLED");

    Map<String, Object> args = new HashMap<String, Object>(2);
    args.put("USERNAME", user.getUserId());
    args.put("PASSWORD", user.getUserPass());
    args.put("ENABLED", true);

    insertUser.execute(args);

    return 0;/*from w  ww . j  a  v a 2  s. co  m*/
}

From source file:com.krawler.workflow.module.dao.DataObjectOperationDAOImpl.java

public boolean createDataObject(String objName, Map<String, Object> dataObject) {
    boolean result = true;

    try {/*from  w w  w.  j a  v  a  2 s  .co  m*/
        SimpleJdbcInsert insert = new SimpleJdbcInsert(getJdbcTemplate()).withTableName(getTableName(objName));
        insert.execute(dataObject);
    } catch (Exception e) {
        LOG.warn("Can not insert record", e);
        result = false;
    }

    return result;
}

From source file:com.krawler.workflow.module.dao.DataObjectOperationDAOImpl.java

@SuppressWarnings("unchecked")
@Override//from w  w  w.j  av a  2s. c  om
public boolean createDataObject(Object dataObject) {
    boolean result = true;

    try {
        SimpleJdbcInsert insert = new SimpleJdbcInsert(getJdbcTemplate())
                .withTableName(getTableName(dataObject.getClass().getSimpleName()));
        insert.execute(new BeanMap(dataObject));
    } catch (Exception e) {
        LOG.warn("Can not insert record", e);
        result = false;
    }

    return result;
}

From source file:edu.northwestern.bioinformatics.studycalendar.dao.auditing.AuditEventDao.java

public void saveEvent(AuditEvent event) {
    SimpleJdbcInsert insert = new SimpleJdbcInsert(jdbcTemplate.getDataSource());
    insert.withTableName("audit_events");
    Map<String, Object> parameters = new HashMap<String, Object>();
    final DataAuditInfo info = event.getInfo();
    final DataReference reference = event.getReference();
    parameters.put("ip_address", info.getIp());
    parameters.put("user_name", info.getUsername());
    parameters.put("time", info.getTime());
    parameters.put("class_name", reference.getClassName());
    parameters.put("operation", event.getOperation().toString());
    parameters.put("url", info.getUrl());
    /*  Note: version is hardcoded to 0 otherwise SQLException is thrown for non-nullable column,
    if not included in SimpleJdbcInsert query's parameters.
     *///from ww  w.ja  va2 s.co  m
    parameters.put("version", 0);
    parameters.put("object_id", reference.getId());
    parameters.put("user_action_id", event.getUserActionId());

    if (databaseType.contains("Oracle")) {
        int eventId = jdbcTemplate.queryForInt("SELECT SEQ_AUDIT_EVENTS_ID.nextval FROM dual");
        parameters.put("id", eventId);
        insert.execute(parameters);
        final String updateStatement = "insert into audit_event_values(id, audit_event_id, attribute_name , previous_value, new_value) values (?,?,?,?,?)";
        for (DataAuditEventValue value : event.getValues()) {
            int valueId = jdbcTemplate.queryForInt("SELECT SEQ_AUDIT_EVENT_VALUES_ID.nextval FROM dual");
            jdbcTemplate.update(updateStatement, new Object[] { valueId, eventId, value.getAttributeName(),
                    value.getPreviousValue(), value.getCurrentValue() });
        }
    } else {
        int eventId = insert.usingGeneratedKeyColumns("id").executeAndReturnKey(parameters).intValue();
        final String updateStatement = "insert into audit_event_values(audit_event_id, attribute_name , previous_value, new_value) values (?,?,?,?)";
        for (DataAuditEventValue value : event.getValues()) {
            jdbcTemplate.update(updateStatement, new Object[] { eventId, value.getAttributeName(),
                    value.getPreviousValue(), value.getCurrentValue() });
        }
    }
}

From source file:ru.org.linux.site.Message.java

public boolean updateMessageText(Connection db, User editor, List<String> newTags) throws SQLException {
    SingleConnectionDataSource scds = new SingleConnectionDataSource(db, true);

    PreparedStatement pstGet = db.prepareStatement(
            "SELECT message,title FROM msgbase JOIN topics ON msgbase.id=topics.id WHERE topics.id=? FOR UPDATE");

    pstGet.setInt(1, msgid);//ww  w  .j  av  a 2  s .c o m
    ResultSet rs = pstGet.executeQuery();
    if (!rs.next()) {
        throw new RuntimeException("Can't fetch previous message text");
    }

    String oldMessage = rs.getString("message");
    String oldTitle = rs.getString("title");

    rs.close();
    pstGet.close();

    List<String> oldTags = Tags.getMessageTags(db, msgid);

    EditInfoDTO editInfo = new EditInfoDTO();

    editInfo.setMsgid(msgid);
    editInfo.setEditor(editor.getId());

    boolean modified = false;

    SimpleJdbcTemplate jdbcTemplate = new SimpleJdbcTemplate(scds);

    if (!oldMessage.equals(message)) {
        editInfo.setOldmessage(oldMessage);
        modified = true;

        jdbcTemplate.update("UPDATE msgbase SET message=:message WHERE id=:msgid",
                ImmutableMap.of("message", message, "msgid", msgid));
    }

    if (!oldTitle.equals(title)) {
        modified = true;
        editInfo.setOldtitle(oldTitle);

        jdbcTemplate.update("UPDATE topics SET title=:title WHERE id=:id",
                ImmutableMap.of("title", title, "id", msgid));
    }

    if (newTags != null) {
        boolean modifiedTags = Tags.updateTags(db, msgid, newTags);

        if (modifiedTags) {
            editInfo.setOldtags(Tags.toString(oldTags));
            Tags.updateCounters(db, oldTags, newTags);
            modified = true;
        }
    }

    if (modified) {
        SimpleJdbcInsert insert = new SimpleJdbcInsert(scds).withTableName("edit_info").usingColumns("msgid",
                "editor", "oldmessage", "oldtitle", "oldtags");

        insert.execute(new BeanPropertySqlParameterSource(editInfo));
    }

    return modified;
}