Example usage for org.springframework.jdbc.support KeyHolder getKey

List of usage examples for org.springframework.jdbc.support KeyHolder getKey

Introduction

In this page you can find the example usage for org.springframework.jdbc.support KeyHolder getKey.

Prototype

@Nullable
Number getKey() throws InvalidDataAccessApiUsageException;

Source Link

Document

Retrieve the first item from the first map, assuming that there is just one item and just one map, and that the item is a number.

Usage

From source file:edu.pitt.sis.infsci2730.finalProject.dao.TransactionDao.java

public static TransactionDBModel InsertTransactionByID(final String[] para) throws SQLException {

    KeyHolder holder = new GeneratedKeyHolder();
    jdbcTemplate.update(new PreparedStatementCreator() {

        String sql = "insert into Transactions (transaction_date, customer_id) "
                + "values (CURRENT_TIMESTAMP,?)";

        @Override//from w  ww  . j a  va 2  s.co  m
        public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
            PreparedStatement ps = connection.prepareStatement(sql.toString(), Statement.RETURN_GENERATED_KEYS);
            ps.setString(1, para[0]);

            return ps;
        }
    }, holder);

    int newId = holder.getKey().intValue();

    return jdbcTemplate.queryForObject("select * from Transactions where TRANSACTION_ID=" + newId,
            new TransactionRowMapper());
}

From source file:edu.pitt.sis.infsci2730.finalProject.dao.AddressDao.java

/**
 * add new AddressDBModel//from  w  ww  .  j ava 2  s  .  co m
 *
 * @param para
 * @return
 */
//    public static int addAddress(final String[] para) throws SQLException {
//        String sql = "insert into Address (city,street,state_,zipCode) values (?,?,?,?)";
//        return jdbcTemplate.update(sql,
//                para,
//                new int[]{java.sql.Types.VARCHAR, java.sql.Types.VARCHAR, java.sql.Types.VARCHAR, java.sql.Types.VARCHAR});
//    }

public static Long addAddress(final String[] para) throws SQLException {

    KeyHolder holder = new GeneratedKeyHolder();
    jdbcTemplate.update(new PreparedStatementCreator() {

        String sql = "insert into Address (city,street,state_,zipCode) values (?,?,?,?)";

        @Override
        public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
            PreparedStatement ps = connection.prepareStatement(sql.toString(), Statement.RETURN_GENERATED_KEYS);
            ps.setString(1, para[0]);
            ps.setString(2, para[1]);
            ps.setString(3, para[2]);
            ps.setString(4, para[3]);
            return ps;
        }
    }, holder);

    Long newPersonId = holder.getKey().longValue();

    return newPersonId;
}

From source file:com.unito.model.repository.TableRepository.java

public int saveTable(Table tableToSave) {
    System.out.println("saving");
    KeyHolder keyHolder = new GeneratedKeyHolder();
    System.out.println(keyHolder.getKey());
    //http://docs.spring.io/spring/docs/2.5.x/reference/jdbc.html#jdbc-auto-genereted-keys
    jdbcTemplate.update(new PreparedStatementCreator() {
        @Override//  w  w  w.j  a v a2 s .c om
        public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
            PreparedStatement ps = connection.prepareStatement(INSERT_TABLE, new String[] { "id" });
            ps.setString(1, tableToSave.getName());
            ps.setString(2, tableToSave.getOwner());
            return ps;
        }
    }, keyHolder);
    return keyHolder.getKey().intValue() >= 0 ? keyHolder.getKey().intValue() : -1;
}

From source file:com.javacreed.examples.spring.ExampleDao.java

public long addNew(final String name) {
    final PreparedStatementCreator psc = new PreparedStatementCreator() {
        @Override/*from w  w  w .  j  a v a 2 s  . c om*/
        public PreparedStatement createPreparedStatement(final Connection connection) throws SQLException {
            final PreparedStatement ps = connection.prepareStatement("INSERT INTO `names` (`name`) VALUES (?)",
                    Statement.RETURN_GENERATED_KEYS);
            ps.setString(1, name);
            return ps;
        }
    };

    // The newly generated key will be saved in this object
    final KeyHolder holder = new GeneratedKeyHolder();

    jdbcTemplate.update(psc, holder);

    final long newNameId = holder.getKey().longValue();
    return newNameId;
}

From source file:com.xinferin.dao.DAOActivationImpl.java

@Override
public int add(Activation activation) {
    String sql = "INSERT INTO activation (licence_id, date, machine_code, ipaddress, success)"
            + " VALUES (:licence_id, NOW(), :machine_code, :ipaddress, :success)";

    SqlParameterSource sqlParameters = new BeanPropertySqlParameterSource(activation);
    KeyHolder keyHolder = new GeneratedKeyHolder();
    jdbcTemplate.update(sql, sqlParameters, keyHolder);

    return keyHolder.getKey().intValue();
}

From source file:org.sakaiproject.imagegallery.springutil.BaseJdbcTemplate.java

/**
 * The default implementation assumes that the DB vendor and table definition
 * support JDBC 3.0 generated keys. Non-JDBC-3.0 DBs will need to hack another
 * implementation of this method.//from   w w  w . j  a  v  a2 s. com
 * 
 * @param pscf object that provides SQL and the types of any required parameters
 * @param keyColumnName not used by some non-JDBC-3.0 DBs; needed for Oracle
 * even if there's only one candidate column since otherwise ROWNUM is returned
 * @param args the parameters for the query
 * @return the generated ID for the new record
 */
public Long insertAndReturnGeneratedId(PreparedStatementCreatorFactory pscf, String keyColumnName,
        Object... args) {
    pscf.setGeneratedKeysColumnNames(new String[] { keyColumnName });
    KeyHolder keyHolder = new GeneratedKeyHolder();
    getJdbcOperations().update(pscf.newPreparedStatementCreator(args), keyHolder);
    return new Long(keyHolder.getKey().longValue());
}

From source file:cherry.foundation.type.jdbc.SecureStringTest.java

@Test
public void testSaveAndLoad() {
    String plain = RandomStringUtils.randomAlphanumeric(32);
    VerifySecure record = new VerifySecure();
    record.setStr(SecureString.plainValueOf(plain));

    KeyHolder keyHolder = new GeneratedKeyHolder();
    int count = dao.insert(record, keyHolder);

    assertThat(count, is(1));/*from  w  w  w . j ava 2s .  c o m*/
    assertThat(keyHolder.getKey().intValue(), is(not(0)));

    List<VerifySecure> list = dao.selectAll();
    assertThat(list.isEmpty(), is(false));
    VerifySecure r = list.get(0);
    assertThat(r.getStr().plain(), is(plain));
}

From source file:com.apress.prospringintegration.springenterprise.stocks.dao.jdbc.JdbcTemplateStockDao.java

@Override
public void insert(Stock stock) {
    KeyHolder keyHolder = new GeneratedKeyHolder();
    this.jdbcTemplate.update(new PSStockCreater(stock), keyHolder);
    stock.setId(keyHolder.getKey().intValue());
}

From source file:cherry.foundation.type.jdbc.JdbcSecureStringTest.java

@Test
public void testSaveAndLoad() {
    String plain = RandomStringUtils.randomAlphanumeric(32);
    ConversionTest record = new ConversionTest();
    record.setSecStr(SecureString.plainValueOf(plain));

    KeyHolder keyHolder = new GeneratedKeyHolder();
    int count = jdbcDao.insert(record, keyHolder);

    assertThat(count, is(1));/*from w  w w.  j  a va2 s.  co m*/
    assertThat(keyHolder.getKey().intValue(), is(not(0)));

    List<ConversionTest> list = jdbcDao.selectAll();
    assertThat(list.isEmpty(), is(false));
    ConversionTest r = list.get(0);
    assertThat(r.getSecStr().plain(), is(plain));
}

From source file:com.xinferin.dao.DAOLicenceImpl.java

@Override
public int add(Licence licence) {
    String sql = "INSERT INTO licence (product_id, issue_date)" + " VALUES (:product_id, NOW())";

    SqlParameterSource sqlParameters = new BeanPropertySqlParameterSource(licence);
    KeyHolder keyHolder = new GeneratedKeyHolder();
    jdbcTemplate.update(sql, sqlParameters, keyHolder);

    return keyHolder.getKey().intValue();
}