Package org.springframework.jdbc.core.namedparam

Examples of org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate


    this.conceptDao = conceptDao;
  }

  public void setDataSource(DataSource ds) {
    this.jdbcTemplate = new JdbcTemplate(ds);
    this.namedParamJdbcTemplate = new NamedParameterJdbcTemplate(ds);
  }
View Full Code Here


  }

  public void setDataSource(DataSource ds) {
    this.jdbcTemplate = new JdbcTemplate(ds);
    this.simpleJdbcTemplate = new SimpleJdbcTemplate(ds);
    this.namedJdbcTemplate = new NamedParameterJdbcTemplate(ds);
  }
View Full Code Here

    }

    @Override
    public void appendRecords(Collection<Record> records) {
        // Create jdbc template
        final NamedParameterJdbcTemplate npjt = new NamedParameterJdbcTemplate(jdbcTemplate);
       
        // Create array of parameter sources
        int len = records.size();
        if (parameters == null || parameters.length != len) {
            parameters =  new SqlParameterSourceRecordWrapper[records.size()];
        }
       
        // Wrap records with type mapper
        int i = 0;
        for (Record r : records) {
            if (parameters[i] == null || ! (parameters[i] instanceof SqlParameterSourceRecordWrapper)) {
                parameters[i] = new SqlParameterSourceRecordWrapper();
                parameters[i].setTypeMap(typeMap);
            }           
            parameters[i].setRecord(r);
            ++i;
        }
       
        // Batch update in transaction
        TransactionTemplate txTemplate = new TransactionTemplate(txManager);
        try {
            txTemplate.execute(new TransactionCallbackWithoutResult() {

                @Override
                public void doInTransactionWithoutResult(TransactionStatus ts) {
                    npjt.batchUpdate(insertQuery, parameters);
                }
            });
           
            return;
        } catch (RuntimeException ex) {
            log.warn(this, ex);
        }
       
        // Do line by line
        int insertCount = 0;
        for (i = 0; i < parameters.length; ++i) {
            try {
                npjt.update(insertQuery, parameters[i]);
                ++insertCount;
            } catch (RuntimeException ex) {
                log.warn(parameters[i].getRecord().toString(), ex);
            }
        }
View Full Code Here

    }

    @Override
    public void appendRecords(Collection<Record> records) {
        // Create jdbc template
        final NamedParameterJdbcTemplate npjt = new NamedParameterJdbcTemplate(jdbcTemplate);       
        // init sql parameters for placeholder fields, for all records       
        final ArrayList<MapSqlParameterSource> parameters = new ArrayList<MapSqlParameterSource>();
       
        // Copy record contents to sql parameter source
        for (Record r: records) {
            try {
                MapSqlParameterSource p = new MapSqlParameterSource() ;
               
                for (String f: placeholders) {
                    try {
                        if (! r.has(f) || r.get(f).getValue() == null) {
                            // Handle undefined or null-valued fields by simply adding null                           
                            p.addValue(placeholderMap.get(f), null);
                        } else {
                            if (typeMap.get(f).equals(java.sql.Types.NUMERIC)) {
                                try {
                                    p.addValue(placeholderMap.get(f), Long.valueOf(r.get(f).getValue()),
                                            java.sql.Types.NUMERIC);
                                } catch (NumberFormatException ex) {
                                    // Parse to big decimal as a fallback option
                                    p.addValue(placeholderMap.get(f), new java.math.BigDecimal(r.get(f).getValue()),
                                            java.sql.Types.NUMERIC);
                                }
                            }
                            else {
                                p.addValue(placeholderMap.get(f), r.get(f).getValue(), typeMap.get(f)) ;
                            }
                        }
                    } catch (NullPointerException ex) {
                        log.warn("Target SQL placeholder '" + f + "' refers to a nonexistent field.");
                        p.addValue(placeholderMap.get(f), null);
                    }
                }
               
                parameters.add(p);
            } catch (NumberFormatException ex) {
                log.warn("Couldn't parse one of the fields of numeric-fields-to-convert; record: " + r.toString());
            }
        }
       
        // Batch update in transaction
        TransactionTemplate txTemplate = new TransactionTemplate(txManager);       
        try {
            txTemplate.execute(new TransactionCallbackWithoutResult() {
                @Override
                public void doInTransactionWithoutResult(TransactionStatus ts) {
                    int[] affectedRows =
                            npjt.batchUpdate(insertQuery.getSql(), parameters.toArray(new MapSqlParameterSource[0]));
                   
                    int sum = 0;
                    for (int i = 0; i < affectedRows.length; ++i) {
                        sum += DatabaseUtils.getAffectedRowsNumber(affectedRows[i]);
                    }
                   
                    insertedRecords += sum;
                    // log.debug(String.format("Inserted %1$d rows in batch.", sum));
                }
            });
           
        } catch (RuntimeException ex) {
            log.warn(this, ex);
       
            // Do line by line
            int insertCount = 0;
            for (int i = 0; i < parameters.size(); ++i) {
                try {
                    int affectedRows = npjt.update(insertQuery.getSql(), parameters.get(i));                   
                    insertCount += DatabaseUtils.getAffectedRowsNumber(affectedRows);
                   
                } catch (RuntimeException ex2) {
                    log.warn("Could not insert record: "
                            + DatabaseUtils.sqlParameterSourceToText(parameters.get(i)), ex2);
View Full Code Here

      dataSource = (DataSource) ApplicationContextHolder
          .getApplicationContext().getBean(
              "collectionReaderDataSource");
    }
    simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource);
    namedJdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
  }
View Full Code Here

   *
   * @param dataSource
   *            the new data source
   */
  public void setDataSource(DataSource dataSource) {
    this.jdbcTemp = new NamedParameterJdbcTemplate(dataSource);
  }
View Full Code Here

  private JdbcListFactory listFactory;

  public JdbcClientDetailsService(DataSource dataSource) {
    Assert.notNull(dataSource, "DataSource required");
    this.jdbcTemplate = new JdbcTemplate(dataSource);
    this.listFactory = new DefaultJdbcListFactory(new NamedParameterJdbcTemplate(jdbcTemplate));
  }
View Full Code Here

        this.insertOwner = new SimpleJdbcInsert(dataSource)
                .withTableName("owners")
                .usingGeneratedKeyColumns("id");

        this.namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(dataSource);

        this.visitRepository = visitRepository;
    }
View Full Code Here

    private VisitRepository visitRepository;


    @Autowired
    public JdbcPetRepositoryImpl(DataSource dataSource, OwnerRepository ownerRepository, VisitRepository visitRepository) {
        this.namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(dataSource);

        this.insertPet = new SimpleJdbcInsert(dataSource)
                .withTableName("pets")
                .usingGeneratedKeyColumns("id");
View Full Code Here

  public Account findById(Long id) {
    return jdbcTemplate.queryForObject(AccountMapper.SELECT_ACCOUNT + " where id = ?", accountMapper, id);
  }

  public List<ProfileReference> findProfileReferencesByIds(List<Long> accountIds) {
    NamedParameterJdbcTemplate namedTemplate = new NamedParameterJdbcTemplate(jdbcTemplate);
    Map<String, Object> params = new HashMap<String, Object>(2, 1);
    params.put("accountIds", accountIds);
    return namedTemplate.query(AccountMapper.SELECT_ACCOUNT_REFERENCE + " where id in ( :accountIds )", params,
        accountMapper.getReferenceMapper());
  }
View Full Code Here

TOP

Related Classes of org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.