Search in sources :

Example 1 with PreparedStatementCreator

use of org.springframework.jdbc.core.PreparedStatementCreator in project camel by apache.

the class ElsqlProducer method processStreamList.

protected void processStreamList(final Exchange exchange, final String sql, final SqlParameterSource param) throws Exception {
    // spring JDBC to parse the SQL and build the prepared statement creator
    // this is what NamedJdbcTemplate does internally
    final ParsedSql parsedSql = NamedParameterUtils.parseSqlStatement(sql);
    final String sqlToUse = NamedParameterUtils.substituteNamedParameters(parsedSql, param);
    final Object[] params = NamedParameterUtils.buildValueArray(parsedSql, param, null);
    final List<SqlParameter> declaredParameters = NamedParameterUtils.buildSqlParameterList(parsedSql, param);
    final PreparedStatementCreatorFactory pscf = new PreparedStatementCreatorFactory(sqlToUse, declaredParameters);
    final PreparedStatementCreator statementCreator = pscf.newPreparedStatementCreator(params);
    processStreamList(exchange, statementCreator, sqlToUse);
}
Also used : SqlParameter(org.springframework.jdbc.core.SqlParameter) PreparedStatementCreator(org.springframework.jdbc.core.PreparedStatementCreator) ParsedSql(org.springframework.jdbc.core.namedparam.ParsedSql) PreparedStatementCreatorFactory(org.springframework.jdbc.core.PreparedStatementCreatorFactory)

Example 2 with PreparedStatementCreator

use of org.springframework.jdbc.core.PreparedStatementCreator in project nixmash-blog by mintster.

the class GitHubTests method githubStatRecordKeyReturned.

@Test
public void githubStatRecordKeyReturned() throws Exception {
    KeyHolder keyHolder = new GeneratedKeyHolder();
    long statId = -1L;
    try {
        statId = jdbcTemplate.queryForObject("SELECT stat_id FROM github_stats WHERE stat_date = '2010-10-10'", Long.class);
    } catch (EmptyResultDataAccessException e) {
        jdbcTemplate.update(new PreparedStatementCreator() {

            String INSERT_SQL = "INSERT INTO github_stats (stat_date) VALUES ('2010-10-10')";

            public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
                PreparedStatement ps = connection.prepareStatement(INSERT_SQL, new String[] { "stat_id" });
                return ps;
            }
        }, keyHolder);
        statId = keyHolder.getKey().longValue();
    }
    assertThat(statId).isGreaterThan(0);
}
Also used : GeneratedKeyHolder(org.springframework.jdbc.support.GeneratedKeyHolder) PreparedStatementCreator(org.springframework.jdbc.core.PreparedStatementCreator) Connection(java.sql.Connection) EmptyResultDataAccessException(org.springframework.dao.EmptyResultDataAccessException) PreparedStatement(java.sql.PreparedStatement) KeyHolder(org.springframework.jdbc.support.KeyHolder) GeneratedKeyHolder(org.springframework.jdbc.support.GeneratedKeyHolder) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 3 with PreparedStatementCreator

use of org.springframework.jdbc.core.PreparedStatementCreator in project bitcampSCOpen2017 by ryuyj.

the class MemberDao method insert.

public // 생명주기가 다르기 때문에 final로 지정
void insert(// 생명주기가 다르기 때문에 final로 지정
final Member member) {
    KeyHolder keyHolder = new GeneratedKeyHolder();
    jdbcTemplate.update(new PreparedStatementCreator() {

        @Override
        public PreparedStatement createPreparedStatement(Connection con) throws SQLException {
            PreparedStatement pstmt = con.prepareStatement("INSERT INTO MEMBER (EMAIL, PASSWORD, NAME, REGDATE, PHOTO) VALUES (?, ?, ?, ?, ?)", new String[] { "ID" });
            pstmt.setString(1, member.getEmail());
            pstmt.setString(2, member.getPassword());
            pstmt.setString(3, member.getName());
            pstmt.setTimestamp(4, new Timestamp(member.getRegisterDate().getTime()));
            pstmt.setString(5, member.getPhoto());
            return pstmt;
        }
    }, keyHolder);
    Number keyValue = keyHolder.getKey();
    member.setId(keyValue.longValue());
}
Also used : GeneratedKeyHolder(org.springframework.jdbc.support.GeneratedKeyHolder) SQLException(java.sql.SQLException) PreparedStatementCreator(org.springframework.jdbc.core.PreparedStatementCreator) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) KeyHolder(org.springframework.jdbc.support.KeyHolder) GeneratedKeyHolder(org.springframework.jdbc.support.GeneratedKeyHolder) Timestamp(java.sql.Timestamp)

Example 4 with PreparedStatementCreator

use of org.springframework.jdbc.core.PreparedStatementCreator in project training_portal by training-portal.

the class QuizDaoJdbc method addQuiz.

@Transactional
@Override
public Long addQuiz(Quiz quiz) {
    KeyHolder keyHolder = new GeneratedKeyHolder();
    template.update(new PreparedStatementCreator() {

        @Override
        public PreparedStatement createPreparedStatement(Connection con) throws SQLException {
            PreparedStatement stmt = con.prepareStatement(ADD_QUIZ, new String[] { "quiz_id" });
            stmt.setString(1, quiz.getName());
            stmt.setString(2, quiz.getDescription());
            stmt.setString(3, quiz.getExplanation());
            stmt.setDate(4, Date.valueOf(quiz.getCreationDate()));
            stmt.setTime(5, quiz.getPassingTime() == null ? null : Time.valueOf(LocalTime.MIDNIGHT.plus(quiz.getPassingTime())));
            stmt.setLong(6, quiz.getAuthorId());
            stmt.setString(7, quiz.getTeacherQuizStatus().getTeacherQuizStatus());
            return stmt;
        }
    }, keyHolder);
    long quizId = keyHolder.getKey().longValue();
    quiz.setQuizId(quizId);
    logger.info("Quiz added: " + quiz);
    return quizId;
}
Also used : GeneratedKeyHolder(org.springframework.jdbc.support.GeneratedKeyHolder) PreparedStatementCreator(org.springframework.jdbc.core.PreparedStatementCreator) KeyHolder(org.springframework.jdbc.support.KeyHolder) GeneratedKeyHolder(org.springframework.jdbc.support.GeneratedKeyHolder) Transactional(org.springframework.transaction.annotation.Transactional)

Example 5 with PreparedStatementCreator

use of org.springframework.jdbc.core.PreparedStatementCreator in project training_portal by training-portal.

the class AnswerSimpleDaoJdbc method addAnswerSimple.

@Transactional
@Override
public Long addAnswerSimple(AnswerSimple answerSimple) {
    KeyHolder keyHolder = new GeneratedKeyHolder();
    template.update(new PreparedStatementCreator() {

        @Override
        public PreparedStatement createPreparedStatement(Connection con) throws SQLException {
            PreparedStatement stmt = con.prepareStatement(ADD_ANSWER_SIMPLE, new String[] { "answer_simple_id" });
            stmt.setLong(1, answerSimple.getQuestionId());
            stmt.setString(2, answerSimple.getBody());
            stmt.setBoolean(3, answerSimple.isCorrect());
            return stmt;
        }
    }, keyHolder);
    long answerSimpleId = keyHolder.getKey().longValue();
    answerSimple.setAnswerSimpleId(answerSimpleId);
    logger.info("Added answerSimple: " + answerSimple);
    return answerSimpleId;
}
Also used : GeneratedKeyHolder(org.springframework.jdbc.support.GeneratedKeyHolder) SQLException(java.sql.SQLException) PreparedStatementCreator(org.springframework.jdbc.core.PreparedStatementCreator) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) KeyHolder(org.springframework.jdbc.support.KeyHolder) GeneratedKeyHolder(org.springframework.jdbc.support.GeneratedKeyHolder) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

PreparedStatementCreator (org.springframework.jdbc.core.PreparedStatementCreator)17 GeneratedKeyHolder (org.springframework.jdbc.support.GeneratedKeyHolder)14 KeyHolder (org.springframework.jdbc.support.KeyHolder)12 Connection (java.sql.Connection)10 PreparedStatement (java.sql.PreparedStatement)10 SQLException (java.sql.SQLException)8 Transactional (org.springframework.transaction.annotation.Transactional)5 BaseObject (com.github.knightliao.apollo.db.bo.BaseObject)2 List (java.util.List)2 Map (java.util.Map)2 Test (org.junit.Test)2 EmptyResultDataAccessException (org.springframework.dao.EmptyResultDataAccessException)2 PreparedStatementCreatorFactory (org.springframework.jdbc.core.PreparedStatementCreatorFactory)2 SqlParameter (org.springframework.jdbc.core.SqlParameter)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 ServerErrorException (com.rdbcache.exceptions.ServerErrorException)1 AnyKey (com.rdbcache.helpers.AnyKey)1 KvPairs (com.rdbcache.helpers.KvPairs)1 KeyInfo (com.rdbcache.models.KeyInfo)1