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

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

Introduction

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

Prototype

@Override
    public SimpleJdbcInsert withTableName(String tableName) 

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);//  w ww.  ja v  a  2 s . c om

    return 0;
}

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  www  .ja va2 s . c  o 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() });
        }
    }
}