当前位置: 首页>>代码示例>>Java>>正文


Java PreparedStatementCreator类代码示例

本文整理汇总了Java中org.springframework.jdbc.core.PreparedStatementCreator的典型用法代码示例。如果您正苦于以下问题:Java PreparedStatementCreator类的具体用法?Java PreparedStatementCreator怎么用?Java PreparedStatementCreator使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


PreparedStatementCreator类属于org.springframework.jdbc.core包,在下文中一共展示了PreparedStatementCreator类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: saveOrderRecord

import org.springframework.jdbc.core.PreparedStatementCreator; //导入依赖的package包/类
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();
}
 
开发者ID:QNJR-GROUP,项目名称:EasyTransaction,代码行数:21,代码来源:OrderService.java

示例2: insertWithAutoGeneratedKey

import org.springframework.jdbc.core.PreparedStatementCreator; //导入依赖的package包/类
private <S extends T> S insertWithAutoGeneratedKey(S entity, Map<String, Object> columns) {
    removeIdColumns(columns);

    final String insertQuery = sqlGenerator.insert(table, columns);
    final Object[] queryParams = columns.values().toArray();
    final GeneratedKeyHolder key = new GeneratedKeyHolder();

    jdbcOps.update(new PreparedStatementCreator() {
        public PreparedStatement createPreparedStatement(Connection con) throws SQLException {
            String idColumnName = table.getPkColumns().get(0);
            PreparedStatement ps = con.prepareStatement(insertQuery, new String[]{idColumnName});
            for (int i = 0; i < queryParams.length; ++i) {
                ps.setObject(i + 1, queryParams[i]);
            }
            return ps;
        }
    }, key);

    return postInsert(entity, key.getKey());
}
 
开发者ID:rubasace,项目名称:spring-data-jdbc,代码行数:21,代码来源:BaseJdbcRepository.java

示例3: update

import org.springframework.jdbc.core.PreparedStatementCreator; //导入依赖的package包/类
/**
 * 
 * @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;
		}

	});
}
 
开发者ID:jigsaw-projects,项目名称:jigsaw-payment,代码行数:25,代码来源:JdbcProtobufTemplate.java

示例4: create

import org.springframework.jdbc.core.PreparedStatementCreator; //导入依赖的package包/类
@Transactional
public User create(String tenantSchema, final User user) {
	final String sql = MessageFormat.format(TENANT_SQL_TEMPLATE, tenantSchema, "insert into user(name,password) values(?,?)");

	KeyHolder holder = new GeneratedKeyHolder();

	jdbcTemplate.update(new PreparedStatementCreator() {

		@Override
		public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
			PreparedStatement ps = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
			ps.setString(1, user.getName());
			ps.setString(2, user.getPassword());
			return ps;
		}
	}, holder);

	int newUserId = holder.getKey().intValue();
	user.setId(newUserId);
	return user;
}
 
开发者ID:tangaiyun,项目名称:multitenancybymycat,代码行数:22,代码来源:UserDao.java

示例5: getCurrentGithubId

import org.springframework.jdbc.core.PreparedStatementCreator; //导入依赖的package包/类
public long getCurrentGithubId() {
    KeyHolder keyHolder = new GeneratedKeyHolder();
    String sql = "SELECT stat_id FROM github_stats WHERE stat_date = current_date()";
    long statId = -1;
    try {
        statId = jdbcTemplate.queryForObject(sql, Long.class);
    } catch (EmptyResultDataAccessException e) {

        jdbcTemplate.update(
                new PreparedStatementCreator() {
                    String INSERT_SQL = "INSERT INTO github_stats (stat_date) VALUES (current_date())";
                    public PreparedStatement createPreparedStatement(Connection cn) throws SQLException {
                        PreparedStatement ps = cn.prepareStatement(INSERT_SQL, new String[] {"stat_id"});
                        return ps;
                    }
                },
                keyHolder);
        statId = keyHolder.getKey().longValue();

    }
    logger.info("Current GitHub Stats ID: " + statId);
    return statId;
}
 
开发者ID:mintster,项目名称:nixmash-blog,代码行数:24,代码来源:GithubJobUI.java

示例6: githubStatRecordKeyReturned

import org.springframework.jdbc.core.PreparedStatementCreator; //导入依赖的package包/类
@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);
}
 
开发者ID:mintster,项目名称:nixmash-blog,代码行数:24,代码来源:GitHubTests.java

示例7: insert

import org.springframework.jdbc.core.PreparedStatementCreator; //导入依赖的package包/类
public Idea insert(Idea idea, User currentUser) {
	String sql = "insert into idea (title, description, user_id) values (?,?,?)";
	KeyHolder keyHolder = new GeneratedKeyHolder();
	jdbcTemplate.update(new PreparedStatementCreator() {
		@Override
		public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
			PreparedStatement ps = connection.prepareStatement(sql, new String[] { "id" });
			ps.setString(1, idea.getTitle());
			ps.setString(2, idea.getDescription());
			ps.setLong(3, currentUser.getId());
			return ps;
		}
	}, keyHolder);
	idea.setId(keyHolder.getKey().longValue());
	return idea;
}
 
开发者ID:garydamm,项目名称:idea-forge,代码行数:17,代码来源:IdeaRepository.java

示例8: updateWithPreparedStatementCreator

import org.springframework.jdbc.core.PreparedStatementCreator; //导入依赖的package包/类
@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);
}
 
开发者ID:tramchamploo,项目名称:buffer-slayer,代码行数:24,代码来源:BatchJdbcTemplateTest.java

示例9: insertObj

import org.springframework.jdbc.core.PreparedStatementCreator; //导入依赖的package包/类
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;
	}
 
开发者ID:jeffreyning,项目名称:nh-micro,代码行数:25,代码来源:MicroMetaDao.java

示例10: insertAndGetKey

import org.springframework.jdbc.core.PreparedStatementCreator; //导入依赖的package包/类
public Long insertAndGetKey(final String sql, final Object... args) {
    final String mSql = initSql(sql);
    KeyHolder keyHolder = new GeneratedKeyHolder();
    getJdbcTemplate().update(new PreparedStatementCreator() {
        public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
            PreparedStatement ps = connection.prepareStatement(mSql, Statement.RETURN_GENERATED_KEYS);
            if (args != null && args.length > 0) {
                for (int index = 0; index < args.length; index++) {
                    ps.setObject((index + 1), args[index]);
                }
            }
            return ps;
        }
    }, keyHolder);
    Long generatedId = keyHolder.getKey().longValue();
    return generatedId;
}
 
开发者ID:1991wangliang,项目名称:lorne_mysql,代码行数:18,代码来源:BaseJdbcTemplate.java

示例11: createDefaultAuthzZone

import org.springframework.jdbc.core.PreparedStatementCreator; //导入依赖的package包/类
private Long createDefaultAuthzZone(final JdbcTemplate jdbcTemplate) {
    final String insertZoneSql =
            "INSERT INTO authorization_zone (name, description, subdomain) " + "VALUES (?,?,?)";
    KeyHolder holder = new GeneratedKeyHolder();

    jdbcTemplate.update(new PreparedStatementCreator() {

        @Override
        public PreparedStatement createPreparedStatement(final Connection connection) throws SQLException {
            PreparedStatement ps = connection.prepareStatement(insertZoneSql, new String[] { "id" });
            ps.setString(1, "apm-migrated");
            ps.setString(2, "APM Migrated Zone from mvp1");
            ps.setString(3, "apm-migrated");
            return ps;
        }
    }, holder);

    return holder.getKey().longValue();
}
 
开发者ID:eclipse,项目名称:keti,代码行数:20,代码来源:V2_0_1__InitializeIdentityZones.java

示例12: createUser

import org.springframework.jdbc.core.PreparedStatementCreator; //导入依赖的package包/类
@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();
}
 
开发者ID:PacktPublishing,项目名称:Spring-Security-Third-Edition,代码行数:24,代码来源:JdbcCalendarUserDao.java

示例13: saveVisitorAndComment

import org.springframework.jdbc.core.PreparedStatementCreator; //导入依赖的package包/类
public boolean saveVisitorAndComment(final Comment comment){

		jdbcTemplate = new JdbcTemplate(dataSource);
		
		final String sql = "INSERT INTO visitors(VisitorName,Email) VALUES(?,?)";
		
		KeyHolder keyHolder = new GeneratedKeyHolder();
		
		int result = jdbcTemplate.update( new PreparedStatementCreator() {
	        public PreparedStatement createPreparedStatement(Connection con) throws SQLException {
	            PreparedStatement pst = con.prepareStatement(sql, new String[] {"Id"});
	            pst.setString(1, comment.getName());
	            pst.setString(2, comment.getEmail());
	            return pst;
	        }
	    }, keyHolder);
		
		final Long lastId = (Long)keyHolder.getKey();
		
		if(result != 0){
			comment.setCommentatorId(lastId.intValue());
			return saveVisitor(comment);
		} else {
			return false;
		}
	}
 
开发者ID:eyupgevenim,项目名称:SpringMvcBlog,代码行数:27,代码来源:PostDaoImpl.java

示例14: createPermission

import org.springframework.jdbc.core.PreparedStatementCreator; //导入依赖的package包/类
public Permission createPermission(final Permission permission) {
    final String sql = "insert into sys_permissions(permission, description, available) values(?,?,?)";

    GeneratedKeyHolder keyHolder = new GeneratedKeyHolder();
    jdbcTemplate.update(new PreparedStatementCreator() {
        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;
}
 
开发者ID:l81893521,项目名称:shiro-demo,代码行数:17,代码来源:PermissionDaoImpl.java

示例15: createRole

import org.springframework.jdbc.core.PreparedStatementCreator; //导入依赖的package包/类
public Role createRole(final Role role) {
    final String sql = "insert into sys_roles(role, description, available) values(?,?,?)";

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

    return role;
}
 
开发者ID:l81893521,项目名称:shiro-demo,代码行数:18,代码来源:RoleDaoImpl.java


注:本文中的org.springframework.jdbc.core.PreparedStatementCreator类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。