org.springframework.jdbc.core.PreparedStatementCreator Java Examples

The following examples show how to use org.springframework.jdbc.core.PreparedStatementCreator. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JdbcCalendarUserDao.java    From maven-framework-project with MIT License 6 votes vote down vote up
@Override
public int createUser(final CalendarUser userToAdd) {
    if (userToAdd == null) {
        throw new IllegalArgumentException("userToAdd cannot be null");
    }
    if (userToAdd.getId() != null) {
        throw new IllegalArgumentException("userToAdd.getId() must be null when creating a "+CalendarUser.class.getName());
    }
    KeyHolder keyHolder = new GeneratedKeyHolder();
    this.jdbcOperations.update(new PreparedStatementCreator() {
        public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
            PreparedStatement ps = connection.prepareStatement(
                    "insert into calendar_users (email, password, first_name, last_name) values (?, ?, ?, ?)",
                    new String[] { "id" });
            ps.setString(1, userToAdd.getEmail());
            ps.setString(2, userToAdd.getPassword());
            ps.setString(3, userToAdd.getFirstName());
            ps.setString(4, userToAdd.getLastName());
            return ps;
        }
    }, keyHolder);
    return keyHolder.getKey().intValue();
}
 
Example #2
Source File: JdbcCalendarUserDao.java    From Spring-Security-Third-Edition with MIT License 6 votes vote down vote up
@Override
public int createUser(final CalendarUser userToAdd) {
    if (userToAdd == null) {
        throw new IllegalArgumentException("userToAdd cannot be null");
    }
    if (userToAdd.getId() != null) {
        throw new IllegalArgumentException("userToAdd.getId() must be null when creating a "+CalendarUser.class.getName());
    }
    KeyHolder keyHolder = new GeneratedKeyHolder();
    this.jdbcOperations.update(new PreparedStatementCreator() {
        public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
            PreparedStatement ps = connection.prepareStatement(
                    "insert into calendar_users (email, password, first_name, last_name) values (?, ?, ?, ?)",
                    new String[] { "id" });
            ps.setString(1, userToAdd.getEmail());
            ps.setString(2, userToAdd.getPassword());
            ps.setString(3, userToAdd.getFirstName());
            ps.setString(4, userToAdd.getLastName());
            return ps;
        }
    }, keyHolder);
    return keyHolder.getKey().intValue();
}
 
Example #3
Source File: JdbcCalendarUserDao.java    From Spring-Security-Third-Edition with MIT License 6 votes vote down vote up
@Override
public int createUser(final CalendarUser userToAdd) {
    if (userToAdd == null) {
        throw new IllegalArgumentException("userToAdd cannot be null");
    }
    if (userToAdd.getId() != null) {
        throw new IllegalArgumentException("userToAdd.getId() must be null when creating a "+CalendarUser.class.getName());
    }
    KeyHolder keyHolder = new GeneratedKeyHolder();
    this.jdbcOperations.update(new PreparedStatementCreator() {
        public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
            PreparedStatement ps = connection.prepareStatement(
                    "insert into calendar_users (email, password, first_name, last_name) values (?, ?, ?, ?)",
                    new String[] { "id" });
            ps.setString(1, userToAdd.getEmail());
            ps.setString(2, userToAdd.getPassword());
            ps.setString(3, userToAdd.getFirstName());
            ps.setString(4, userToAdd.getLastName());
            return ps;
        }
    }, keyHolder);
    return keyHolder.getKey().intValue();
}
 
Example #4
Source File: JdbcCalendarUserDao.java    From Spring-Security-Third-Edition with MIT License 6 votes vote down vote up
@Override
public int createUser(final CalendarUser userToAdd) {
    if (userToAdd == null) {
        throw new IllegalArgumentException("userToAdd cannot be null");
    }
    if (userToAdd.getId() != null) {
        throw new IllegalArgumentException("userToAdd.getId() must be null when creating a "+CalendarUser.class.getName());
    }
    KeyHolder keyHolder = new GeneratedKeyHolder();
    this.jdbcOperations.update(new PreparedStatementCreator() {
        public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
            PreparedStatement ps = connection.prepareStatement(
                    "insert into calendar_users (email, password, first_name, last_name) values (?, ?, ?, ?)",
                    new String[] { "id" });
            ps.setString(1, userToAdd.getEmail());
            ps.setString(2, userToAdd.getPassword());
            ps.setString(3, userToAdd.getFirstName());
            ps.setString(4, userToAdd.getLastName());
            return ps;
        }
    }, keyHolder);
    return keyHolder.getKey().intValue();
}
 
Example #5
Source File: JdbcCalendarUserDao.java    From Spring-Security-Third-Edition with MIT License 6 votes vote down vote up
@Override
public int createUser(final CalendarUser userToAdd) {
    if (userToAdd == null) {
        throw new IllegalArgumentException("userToAdd cannot be null");
    }
    if (userToAdd.getId() != null) {
        throw new IllegalArgumentException("userToAdd.getId() must be null when creating a "+CalendarUser.class.getName());
    }
    KeyHolder keyHolder = new GeneratedKeyHolder();
    this.jdbcOperations.update(new PreparedStatementCreator() {
        public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
            PreparedStatement ps = connection.prepareStatement(
                    "insert into calendar_users (email, password, first_name, last_name) values (?, ?, ?, ?)",
                    new String[] { "id" });
            ps.setString(1, userToAdd.getEmail());
            ps.setString(2, userToAdd.getPassword());
            ps.setString(3, userToAdd.getFirstName());
            ps.setString(4, userToAdd.getLastName());
            return ps;
        }
    }, keyHolder);
    return keyHolder.getKey().intValue();
}
 
Example #6
Source File: JdbcCalendarUserDao.java    From Spring-Security-Third-Edition with MIT License 6 votes vote down vote up
@Override
public int createUser(final CalendarUser userToAdd) {
    if (userToAdd == null) {
        throw new IllegalArgumentException("userToAdd cannot be null");
    }
    if (userToAdd.getId() != null) {
        throw new IllegalArgumentException("userToAdd.getId() must be null when creating a "+CalendarUser.class.getName());
    }
    KeyHolder keyHolder = new GeneratedKeyHolder();
    this.jdbcOperations.update(new PreparedStatementCreator() {
        public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
            PreparedStatement ps = connection.prepareStatement(
                    "insert into calendar_users (email, password, first_name, last_name) values (?, ?, ?, ?)",
                    new String[] { "id" });
            ps.setString(1, userToAdd.getEmail());
            ps.setString(2, userToAdd.getPassword());
            ps.setString(3, userToAdd.getFirstName());
            ps.setString(4, userToAdd.getLastName());
            return ps;
        }
    }, keyHolder);
    return keyHolder.getKey().intValue();
}
 
Example #7
Source File: TransactionLogStorage.java    From dts with Apache License 2.0 6 votes vote down vote up
public void insertGlobalLog(final GlobalLog globalLog) {
    PreparedStatementCreator psc = new PreparedStatementCreator() {

        @Override
        public PreparedStatement createPreparedStatement(Connection con) throws SQLException {
            PreparedStatement ps = con.prepareStatement(
                "insert into dts_global_record (state,client_info,client_ip,gmt_created,gmt_modified) values (?,?,?,now(),now())",
                Statement.RETURN_GENERATED_KEYS);
            ps.setInt(1, globalLog.getState());
            ps.setString(2, globalLog.getClientInfo());
            ps.setString(3, globalLog.getClientIp());
            return ps;
        }
    };
    KeyHolder keyHolder = new GeneratedKeyHolder();
    jdbcTemplate.update(psc, keyHolder);
    long txId = keyHolder.getKey().longValue();
    globalLog.setTransId(txId);
    globalLog.setGmtCreated(Calendar.getInstance().getTime());
    globalLog.setGmtModified(Calendar.getInstance().getTime());
}
 
Example #8
Source File: JdbcCalendarUserDao.java    From Spring-Security-Third-Edition with MIT License 6 votes vote down vote up
@Override
public int createUser(final CalendarUser userToAdd) {
    if (userToAdd == null) {
        throw new IllegalArgumentException("userToAdd cannot be null");
    }
    if (userToAdd.getId() != null) {
        throw new IllegalArgumentException("userToAdd.getId() must be null when creating a "+CalendarUser.class.getName());
    }
    KeyHolder keyHolder = new GeneratedKeyHolder();
    this.jdbcOperations.update(new PreparedStatementCreator() {
        public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
            PreparedStatement ps = connection.prepareStatement(
                    "insert into calendar_users (email, password, first_name, last_name) values (?, ?, ?, ?)",
                    new String[] { "id" });
            ps.setString(1, userToAdd.getEmail());
            ps.setString(2, userToAdd.getPassword());
            ps.setString(3, userToAdd.getFirstName());
            ps.setString(4, userToAdd.getLastName());
            return ps;
        }
    }, keyHolder);
    return keyHolder.getKey().intValue();
}
 
Example #9
Source File: JdbcCalendarUserDao.java    From Spring-Security-Third-Edition with MIT License 6 votes vote down vote up
@Override
public int createUser(final CalendarUser userToAdd) {
    if (userToAdd == null) {
        throw new IllegalArgumentException("userToAdd cannot be null");
    }
    if (userToAdd.getId() != null) {
        throw new IllegalArgumentException("userToAdd.getId() must be null when creating a "+CalendarUser.class.getName());
    }
    KeyHolder keyHolder = new GeneratedKeyHolder();
    this.jdbcOperations.update(new PreparedStatementCreator() {
        public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
            PreparedStatement ps = connection.prepareStatement(
                    "insert into calendar_users (email, password, first_name, last_name) values (?, ?, ?, ?)",
                    new String[] { "id" });
            ps.setString(1, userToAdd.getEmail());
            ps.setString(2, userToAdd.getPassword());
            ps.setString(3, userToAdd.getFirstName());
            ps.setString(4, userToAdd.getLastName());
            return ps;
        }
    }, keyHolder);
    return keyHolder.getKey().intValue();
}
 
Example #10
Source File: InstanceDao.java    From artemis with Apache License 2.0 6 votes vote down vote up
public List<InstanceModel> queryInstances(final String regionId, final List<String> serviceIds) {
    if (CollectionUtils.isEmpty(serviceIds)) {
        return Lists.newArrayList();
    }
    final StringBuilder builder = new StringBuilder();
    for (int i = 0; i < serviceIds.size(); i++) {
        builder.append("?,");
    }
    builder.deleteCharAt(builder.length() - 1);
    final String sql = queryInstancesSql + " and service_id in (" + builder + ")";
    return DataConfig.jdbcTemplate().query(new PreparedStatementCreator() {
        @Override
        public PreparedStatement createPreparedStatement(final Connection conn) throws SQLException {
            final PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1, regionId);
            for (int i = 0, len = serviceIds.size(); i < len; i++) {
                ps.setString(i + 2, serviceIds.get(i));
            }
            return ps;
        }

    }, queryInstancesRowMapper);
}
 
Example #11
Source File: JdbcCalendarUserDao.java    From Spring-Security-Third-Edition with MIT License 6 votes vote down vote up
@Override
public int createUser(final CalendarUser userToAdd) {
    if (userToAdd == null) {
        throw new IllegalArgumentException("userToAdd cannot be null");
    }
    if (userToAdd.getId() != null) {
        throw new IllegalArgumentException("userToAdd.getId() must be null when creating a "+CalendarUser.class.getName());
    }
    KeyHolder keyHolder = new GeneratedKeyHolder();
    this.jdbcOperations.update(new PreparedStatementCreator() {
        public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
            PreparedStatement ps = connection.prepareStatement(
                    "insert into calendar_users (email, password, first_name, last_name) values (?, ?, ?, ?)",
                    new String[] { "id" });
            ps.setString(1, userToAdd.getEmail());
            ps.setString(2, userToAdd.getPassword());
            ps.setString(3, userToAdd.getFirstName());
            ps.setString(4, userToAdd.getLastName());
            return ps;
        }
    }, keyHolder);
    return keyHolder.getKey().intValue();
}
 
Example #12
Source File: ClusterDaoImpl.java    From plow with Apache License 2.0 6 votes vote down vote up
@Override
public Cluster create(final String name, final String[] tags) {
    final UUID id = UUID.randomUUID();
    jdbc.update(new PreparedStatementCreator() {
        @Override
        public PreparedStatement createPreparedStatement(final Connection conn) throws SQLException {
            final PreparedStatement ret = conn.prepareStatement(INSERT);
            ret.setObject(1, id);
            ret.setString(2, name);
            ret.setArray(3, conn.createArrayOf("text", tags));
            return ret;
        }
    });

    ClusterE cluster = new ClusterE();
    cluster.setClusterId(id);
    return cluster;
}
 
Example #13
Source File: JdbcDemonstratingRepository.java    From Spring-Boot-2-Fundamentals with MIT License 6 votes vote down vote up
/**
 * Demonstrate how to insert and retrieve a generated key.
 * <p>
 * The {@code id} column of the {@code short_message} table can generated ids.
 * In order to retrieve the newly generated id, we need an update that returns
 * some data (a JDBC 3 feature). Spring supports this with a keyholder.
 * <p>
 * Unfortunately, {@link JdbcTemplate#update(PreparedStatementCreator, KeyHolder)}
 * needs a {@link PreparedStatementCreator} which is a bit overwhelming
 * here.
 */
public void insertMessage(Author author, String text) {
    String sql = "INSERT INTO short_message(author_id, posted_time, message_text)" +
            " VALUES(?, ?, ?)";
    Timestamp currentTimestamp = Timestamp.valueOf(LocalDateTime.now());

    PreparedStatementCreatorFactory statementCreatorFactory =
            new PreparedStatementCreatorFactory(sql,
                    Types.INTEGER, Types.TIMESTAMP, Types.VARCHAR);
    statementCreatorFactory.setReturnGeneratedKeys(true);
    statementCreatorFactory.setGeneratedKeysColumnNames("id");
    PreparedStatementCreator preparedStatementCreator =
            statementCreatorFactory.newPreparedStatementCreator(
                    new Object[]{author.getId(), currentTimestamp, text});

    JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
    KeyHolder keyHolder = new GeneratedKeyHolder();
    int update = jdbcTemplate.update(preparedStatementCreator, keyHolder);
    log.info("auto-insert created {} row with key {}", update, keyHolder.getKey());
}
 
Example #14
Source File: OrderService.java    From EasyTransaction with Apache License 2.0 6 votes vote down vote up
private Integer saveOrderRecord(JdbcTemplate jdbcTemplate, final int userId, final long money) {
	
	final String INSERT_SQL = "INSERT INTO `order` (`order_id`, `user_id`, `money`, `create_time`) VALUES (NULL, ?, ?, ?);";
	KeyHolder keyHolder = new GeneratedKeyHolder();
	jdbcTemplate.update(
	    new PreparedStatementCreator() {
	    	@Override
	        public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
	            PreparedStatement ps =
	                connection.prepareStatement(INSERT_SQL, new String[] {"id"});
	            ps.setInt(1, userId);
	            ps.setLong(2, money);
	            ps.setDate(3, new Date(System.currentTimeMillis()));
	            return ps;
	        }
	    },
	    keyHolder);
	
	return keyHolder.getKey().intValue();
}
 
Example #15
Source File: TradeOrderRepository.java    From tcc-transaction with Apache License 2.0 6 votes vote down vote up
public void insert(TradeOrder tradeOrder) {
    KeyHolder keyHolder = new GeneratedKeyHolder();
    jdbcTemplate.update(new PreparedStatementCreator() {
        @Override
        public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
            PreparedStatement stmt = connection.prepareStatement("insert into red_trade_order(self_user_id,opposite_user_id,merchant_order_no,amount,status) values(?,?,?,?,?)", Statement.RETURN_GENERATED_KEYS);
            stmt.setObject(1, tradeOrder.getSelfUserId());
            stmt.setObject(2, tradeOrder.getOppositeUserId());
            stmt.setObject(3, tradeOrder.getMerchantOrderNo());
            stmt.setObject(4, tradeOrder.getAmount());
            stmt.setObject(5, tradeOrder.getStatus());
            return stmt;
        }
    }, keyHolder);
    tradeOrder.setId(keyHolder.getKey().longValue());
}
 
Example #16
Source File: OutputDaoImpl.java    From plow with Apache License 2.0 6 votes vote down vote up
@Override
public Output addOutput(final Layer layer, final String path, final Map<String,String> attrs) {

    final UUID id = UUID.randomUUID();

    jdbc.update(new PreparedStatementCreator() {
        @Override
        public PreparedStatement createPreparedStatement(final Connection conn) throws SQLException {
            final PreparedStatement ret = conn.prepareStatement(INSERT_OUTPUT);
            ret.setObject(1, id);
            ret.setObject(2, layer.getLayerId());
            ret.setObject(3, layer.getJobId());
            ret.setString(4, path);
            ret.setObject(5, attrs);
            return ret;
        }
    });

    OutputE output = new OutputE();
    output.setId(id);
    output.setPath(path);
    output.setAttrs(attrs);
    return output;
}
 
Example #17
Source File: OrderService.java    From EasyTransaction with Apache License 2.0 6 votes vote down vote up
private Integer saveOrderRecord(final int userId, final long money) {
	
	final String INSERT_SQL = "INSERT INTO `order` (`order_id`, `user_id`, `money`, `create_time`) VALUES (NULL, ?, ?, ?);";
	KeyHolder keyHolder = new GeneratedKeyHolder();
	jdbcTemplate.update(
	    new PreparedStatementCreator() {
	    	@Override
	        public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
	            PreparedStatement ps =
	                connection.prepareStatement(INSERT_SQL, new String[] {"id"});
	            ps.setInt(1, userId);
	            ps.setLong(2, money);
	            ps.setDate(3, new Date(System.currentTimeMillis()));
	            return ps;
	        }
	    },
	    keyHolder);
	
	return keyHolder.getKey().intValue();
}
 
Example #18
Source File: OrderService.java    From EasyTransaction with Apache License 2.0 6 votes vote down vote up
private Integer saveOrderRecord(JdbcTemplate jdbcTemplate, final int userId, final long money) {
	
	final String INSERT_SQL = "INSERT INTO `order` (`order_id`, `user_id`, `money`, `create_time`) VALUES (NULL, ?, ?, ?);";
	KeyHolder keyHolder = new GeneratedKeyHolder();
	jdbcTemplate.update(
	    new PreparedStatementCreator() {
	    	@Override
	        public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
	            PreparedStatement ps =
	                connection.prepareStatement(INSERT_SQL, new String[] {"id"});
	            ps.setInt(1, userId);
	            ps.setLong(2, money);
	            ps.setDate(3, new Date(System.currentTimeMillis()));
	            return ps;
	        }
	    },
	    keyHolder);
	
	return keyHolder.getKey().intValue();
}
 
Example #19
Source File: OrderService.java    From EasyTransaction with Apache License 2.0 6 votes vote down vote up
private Integer saveOrderRecord(JdbcTemplate jdbcTemplate, final int userId, final long money) {
	
	final String INSERT_SQL = "INSERT INTO `order` (`order_id`, `user_id`, `money`, `create_time`) VALUES (NULL, ?, ?, ?);";
	KeyHolder keyHolder = new GeneratedKeyHolder();
	jdbcTemplate.update(
	    new PreparedStatementCreator() {
	    	@Override
	        public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
	            PreparedStatement ps =
	                connection.prepareStatement(INSERT_SQL, new String[] {"id"});
	            ps.setInt(1, userId);
	            ps.setLong(2, money);
	            ps.setDate(3, new Date(System.currentTimeMillis()));
	            return ps;
	        }
	    },
	    keyHolder);
	
	return keyHolder.getKey().intValue();
}
 
Example #20
Source File: JdbcProtobufTemplate.java    From jigsaw-payment with Apache License 2.0 6 votes vote down vote up
/**
 * 
 * @param sql
 * @param args
 * @return
 */
public int update(final String sql, final List<?> args) {
	if(logger.isDebugEnabled()){
		StringBuilder builder = new StringBuilder();
		builder.append("{sql: \"").append(sql).append("\"; parameters:").append(args);
		logger.debug(builder.toString());
	}
	return jdbcTemplate.update(new PreparedStatementCreator() {

		@Override
		public PreparedStatement createPreparedStatement(Connection con)
				throws SQLException {
			PreparedStatement ps = con.prepareStatement(sql);
			populate(ps, args);
			return ps;
		}

	});
}
 
Example #21
Source File: JdbcCalendarUserDao.java    From maven-framework-project with MIT License 6 votes vote down vote up
@Override
public int createUser(final CalendarUser userToAdd) {
    if (userToAdd == null) {
        throw new IllegalArgumentException("userToAdd cannot be null");
    }
    if (userToAdd.getId() != null) {
        throw new IllegalArgumentException("userToAdd.getId() must be null when creating a "+CalendarUser.class.getName());
    }
    KeyHolder keyHolder = new GeneratedKeyHolder();
    this.jdbcOperations.update(new PreparedStatementCreator() {
        public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
            PreparedStatement ps = connection.prepareStatement(
                    "insert into calendar_users (email, password, first_name, last_name) values (?, ?, ?, ?)",
                    new String[] { "id" });
            ps.setString(1, userToAdd.getEmail());
            ps.setString(2, userToAdd.getPassword());
            ps.setString(3, userToAdd.getFirstName());
            ps.setString(4, userToAdd.getLastName());
            return ps;
        }
    }, keyHolder);
    return keyHolder.getKey().intValue();
}
 
Example #22
Source File: JdbcCalendarUserDao.java    From Spring-Security-Third-Edition with MIT License 6 votes vote down vote up
@Override
public int createUser(final CalendarUser userToAdd) {
    if (userToAdd == null) {
        throw new IllegalArgumentException("userToAdd cannot be null");
    }
    if (userToAdd.getId() != null) {
        throw new IllegalArgumentException("userToAdd.getId() must be null when creating a "+CalendarUser.class.getName());
    }
    KeyHolder keyHolder = new GeneratedKeyHolder();
    this.jdbcOperations.update(new PreparedStatementCreator() {
        public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
            PreparedStatement ps = connection.prepareStatement(
                    "insert into calendar_users (email, password, first_name, last_name) values (?, ?, ?, ?)",
                    new String[] { "id" });
            ps.setString(1, userToAdd.getEmail());
            ps.setString(2, userToAdd.getPassword());
            ps.setString(3, userToAdd.getFirstName());
            ps.setString(4, userToAdd.getLastName());
            return ps;
        }
    }, keyHolder);
    return keyHolder.getKey().intValue();
}
 
Example #23
Source File: JdbcCustomerDAO.java    From maven-framework-project with MIT License 6 votes vote down vote up
/**
 * 返回新增后自动生成的主键
 * @param customer
 * @return
 */
public int getAutoGeneratedID(final Customer customer){
	
	final String sql = "insert into CUSTOMER (name,age) values(?,?)";
	
	KeyHolder keyHolder = new GeneratedKeyHolder();
	
	jdbcTemplate.update(new PreparedStatementCreator() {
		
		@Override
		public PreparedStatement createPreparedStatement(Connection con)
				throws SQLException {
 				 //	autoGeneratedKeys
			 PreparedStatement pst = con.prepareStatement(sql, new String[] {"CUST_ID"});
			 pst.setString(1,customer.getName());//为第一个问号填充值
			 pst.setInt(2,customer.getAge());//为第二个问号填充值
			return pst;
		}
	},keyHolder);
	//返回插入后数据库生成的主键
	return keyHolder.getKey().intValue();
}
 
Example #24
Source File: PermissionDaoImpl.java    From spring-tutorial with Creative Commons Attribution Share Alike 4.0 International 6 votes vote down vote up
@Override
public Permission createPermission(final Permission permission) {
	final String sql = "insert into sys_permissions(permission, description, available) values(?,?,?)";

	GeneratedKeyHolder keyHolder = new GeneratedKeyHolder();
	getJdbcTemplate().update(new PreparedStatementCreator() {
		@Override
		public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
			PreparedStatement psst = connection.prepareStatement(sql, new String[] { "id" });
			psst.setString(1, permission.getPermission());
			psst.setString(2, permission.getDescription());
			psst.setBoolean(3, permission.getAvailable());
			return psst;
		}
	}, keyHolder);
	permission.setId(keyHolder.getKey().longValue());

	return permission;
}
 
Example #25
Source File: BatchJdbcTemplateTest.java    From buffer-slayer with Apache License 2.0 6 votes vote down vote up
@Test
public void updateWithPreparedStatementCreator() {
  reporter = AsyncReporter.builder(new JdbcTemplateSender(underlying))
      .pendingMaxMessages(2)
      .bufferedMaxMessages(2)
      .messageTimeout(0, TimeUnit.MILLISECONDS)
      .build();
  batchJdbcTemplate = new BatchJdbcTemplate(underlying, reporter);

  PreparedStatementCreatorFactory creatorFactory = new PreparedStatementCreatorFactory(INSERTION);
  creatorFactory.addParameter(new SqlParameter(Types.VARCHAR));
  creatorFactory.addParameter(new SqlParameter(Types.TIMESTAMP));

  PreparedStatementCreator creator = creatorFactory
      .newPreparedStatementCreator(new Object[]{randomString(), new Date()});

  batchJdbcTemplate.update(creator);
  batchJdbcTemplate.update(creator);

  reporter.flush();
  int rowCount = batchJdbcTemplate.queryForObject("SELECT COUNT(1) FROM test;", Integer.class);
  assertEquals(2, rowCount);
}
 
Example #26
Source File: JdbcCalendarUserDao.java    From Spring-Security-Third-Edition with MIT License 6 votes vote down vote up
@Override
public int createUser(final CalendarUser userToAdd) {
    if (userToAdd == null) {
        throw new IllegalArgumentException("userToAdd cannot be null");
    }
    if (userToAdd.getId() != null) {
        throw new IllegalArgumentException("userToAdd.getId() must be null when creating a "+CalendarUser.class.getName());
    }
    KeyHolder keyHolder = new GeneratedKeyHolder();
    this.jdbcOperations.update(new PreparedStatementCreator() {
        public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
            PreparedStatement ps = connection.prepareStatement(
                    "insert into calendar_users (email, password, first_name, last_name) values (?, ?, ?, ?)",
                    new String[] { "id" });
            ps.setString(1, userToAdd.getEmail());
            ps.setString(2, userToAdd.getPassword());
            ps.setString(3, userToAdd.getFirstName());
            ps.setString(4, userToAdd.getLastName());
            return ps;
        }
    }, keyHolder);
    return keyHolder.getKey().intValue();
}
 
Example #27
Source File: MicroMetaDao.java    From nh-micro with Apache License 2.0 6 votes vote down vote up
public int insertObj(final String sql,final Object[] paramArray, KeyHolder keyHolder, final String idCol) {

		JdbcTemplate jdbcTemplate =getMicroJdbcTemplate();
		logger.debug(sql);
		logger.debug(Arrays.toString(paramArray));
		Integer retStatus=jdbcTemplate.update(new PreparedStatementCreator() { 

			public PreparedStatement createPreparedStatement(Connection con) 
			throws SQLException { 
					String[] keyColNames=new String[1];
					keyColNames[0]=idCol;
					PreparedStatement ps=con.prepareStatement(sql,keyColNames); 
					if(paramArray!=null){
						int size=paramArray.length;
						for(int i=0;i<size;i++){
							ps.setObject(i+1, paramArray[i]);
						}
					}
		
					return ps; 
				} 
			}, keyHolder);	
		return retStatus;
	}
 
Example #28
Source File: JdbcCalendarUserDao.java    From maven-framework-project with MIT License 6 votes vote down vote up
@Override
public int createUser(final CalendarUser userToAdd) {
    if (userToAdd == null) {
        throw new IllegalArgumentException("userToAdd cannot be null");
    }
    if (userToAdd.getId() != null) {
        throw new IllegalArgumentException("userToAdd.getId() must be null when creating a "+CalendarUser.class.getName());
    }
    KeyHolder keyHolder = new GeneratedKeyHolder();
    this.jdbcOperations.update(new PreparedStatementCreator() {
        public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
            PreparedStatement ps = connection.prepareStatement(
                    "insert into calendar_users (email, password, first_name, last_name) values (?, ?, ?, ?)",
                    new String[] { "id" });
            ps.setString(1, userToAdd.getEmail());
            ps.setString(2, userToAdd.getPassword());
            ps.setString(3, userToAdd.getFirstName());
            ps.setString(4, userToAdd.getLastName());
            return ps;
        }
    }, keyHolder);
    return keyHolder.getKey().intValue();
}
 
Example #29
Source File: JdbcCalendarUserDao.java    From Spring-Security-Third-Edition with MIT License 6 votes vote down vote up
@Override
public int createUser(final CalendarUser userToAdd) {
    if (userToAdd == null) {
        throw new IllegalArgumentException("userToAdd cannot be null");
    }
    if (userToAdd.getId() != null) {
        throw new IllegalArgumentException("userToAdd.getId() must be null when creating a "+CalendarUser.class.getName());
    }
    KeyHolder keyHolder = new GeneratedKeyHolder();
    this.jdbcOperations.update(new PreparedStatementCreator() {
        public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
            PreparedStatement ps = connection.prepareStatement(
                    "insert into calendar_users (email, password, first_name, last_name) values (?, ?, ?, ?)",
                    new String[] { "id" });
            ps.setString(1, userToAdd.getEmail());
            ps.setString(2, userToAdd.getPassword());
            ps.setString(3, userToAdd.getFirstName());
            ps.setString(4, userToAdd.getLastName());
            return ps;
        }
    }, keyHolder);
    return keyHolder.getKey().intValue();
}
 
Example #30
Source File: JdbcCalendarUserDao.java    From Spring-Security-Third-Edition with MIT License 6 votes vote down vote up
@Override
public int createUser(final CalendarUser userToAdd) {
    if (userToAdd == null) {
        throw new IllegalArgumentException("userToAdd cannot be null");
    }
    if (userToAdd.getId() != null) {
        throw new IllegalArgumentException("userToAdd.getId() must be null when creating a "+CalendarUser.class.getName());
    }
    KeyHolder keyHolder = new GeneratedKeyHolder();
    this.jdbcOperations.update(new PreparedStatementCreator() {
        public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
            PreparedStatement ps = connection.prepareStatement(
                    "insert into calendar_users (email, password, first_name, last_name) values (?, ?, ?, ?)",
                    new String[] { "id" });
            ps.setString(1, userToAdd.getEmail());
            ps.setString(2, userToAdd.getPassword());
            ps.setString(3, userToAdd.getFirstName());
            ps.setString(4, userToAdd.getLastName());
            return ps;
        }
    }, keyHolder);
    return keyHolder.getKey().intValue();
}