Java Code Examples for org.springframework.jdbc.core.PreparedStatementCreator

The following are top voted examples for showing how to use org.springframework.jdbc.core.PreparedStatementCreator. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.
Example 1
Project: EasyTransaction   File: OrderService.java   Source Code and License 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 2
Project: spring-data-jdbc   File: BaseJdbcRepository.java   Source Code and License 6 votes vote down vote up
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());
}
 
Example 3
Project: jigsaw-payment   File: JdbcProtobufTemplate.java   Source Code and License 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 4
Project: multitenancybymycat   File: UserDao.java   Source Code and License 6 votes vote down vote up
@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;
}
 
Example 5
Project: nixmash-blog   File: GithubJobUI.java   Source Code and License 6 votes vote down vote up
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;
}
 
Example 6
Project: nixmash-blog   File: GitHubTests.java   Source Code and License 6 votes vote down vote up
@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);
}
 
Example 7
Project: idea-forge   File: IdeaRepository.java   Source Code and License 6 votes vote down vote up
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;
}
 
Example 8
Project: buffer-slayer   File: BatchJdbcTemplateTest.java   Source Code and License 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 9
Project: nh-micro   File: MicroMetaDao.java   Source Code and License 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 10
Project: lorne_mysql   File: BaseJdbcTemplate.java   Source Code and License 6 votes vote down vote up
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;
}
 
Example 11
Project: keti   File: V2_0_1__InitializeIdentityZones.java   Source Code and License 6 votes vote down vote up
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();
}
 
Example 12
Project: Spring-Security-Third-Edition   File: JdbcCalendarUserDao.java   Source Code and 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 13
Project: Spring-Security-Third-Edition   File: JdbcCalendarUserDao.java   Source Code and 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 14
Project: Spring-Security-Third-Edition   File: JdbcCalendarUserDao.java   Source Code and 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 15
Project: Spring-Security-Third-Edition   File: JdbcCalendarUserDao.java   Source Code and 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 16
Project: Spring-Security-Third-Edition   File: JdbcCalendarUserDao.java   Source Code and 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 17
Project: Spring-Security-Third-Edition   File: JdbcCalendarUserDao.java   Source Code and 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 18
Project: Spring-Security-Third-Edition   File: JdbcCalendarUserDao.java   Source Code and 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 19
Project: Spring-Security-Third-Edition   File: JdbcCalendarUserDao.java   Source Code and 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 20
Project: Spring-Security-Third-Edition   File: JdbcCalendarUserDao.java   Source Code and 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 21
Project: Spring-Security-Third-Edition   File: JdbcCalendarUserDao.java   Source Code and 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
Project: Spring-Security-Third-Edition   File: JdbcCalendarUserDao.java   Source Code and 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
Project: Spring-Security-Third-Edition   File: JdbcCalendarUserDao.java   Source Code and 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 24
Project: Spring-Security-Third-Edition   File: JdbcCalendarUserDao.java   Source Code and 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 25
Project: Spring-Security-Third-Edition   File: JdbcCalendarUserDao.java   Source Code and 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 26
Project: Spring-Security-Third-Edition   File: JdbcCalendarUserDao.java   Source Code and 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
Project: Spring-Security-Third-Edition   File: JdbcCalendarUserDao.java   Source Code and 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 28
Project: Spring-Security-Third-Edition   File: JdbcCalendarUserDao.java   Source Code and 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
Project: Spring-Security-Third-Edition   File: JdbcCalendarUserDao.java   Source Code and 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
Project: Spring-Security-Third-Edition   File: JdbcCalendarUserDao.java   Source Code and 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 31
Project: Spring-Security-Third-Edition   File: JdbcCalendarUserDao.java   Source Code and 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 32
Project: Spring-Security-Third-Edition   File: JdbcCalendarUserDao.java   Source Code and 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 33
Project: Spring-Security-Third-Edition   File: JdbcCalendarUserDao.java   Source Code and 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 34
Project: Spring-Security-Third-Edition   File: JdbcCalendarUserDao.java   Source Code and 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 35
Project: Spring-Security-Third-Edition   File: JdbcCalendarUserDao.java   Source Code and 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 36
Project: SpringMvcBlog   File: PostDaoImpl.java   Source Code and License 6 votes vote down vote up
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;
		}
	}
 
Example 37
Project: shiro-demo   File: PermissionDaoImpl.java   Source Code and License 6 votes vote down vote up
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;
}
 
Example 38
Project: shiro-demo   File: RoleDaoImpl.java   Source Code and License 6 votes vote down vote up
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;
}
 
Example 39
Project: shiro-demo   File: UserDaoImpl.java   Source Code and License 6 votes vote down vote up
public User createUser(final User user) {
    final String sql = "insert into sys_users(username, password, salt, locked) 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, user.getUsername());
            psst.setString(2, user.getPassword());
            psst.setString(3, user.getSalt());
            psst.setBoolean(4, user.getLocked());
            return psst;
        }
    }, keyHolder);

    user.setId(keyHolder.getKey().longValue());
    return user;
}
 
Example 40
Project: nh-micro   File: MicroMetaDao.java   Source Code and License 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 41
Project: tcc-transaction   File: TradeOrderRepository.java   Source Code and License 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 42
Project: oauth_demo   File: ClientDaoImpl.java   Source Code and License 6 votes vote down vote up
public Client createClient(final Client client) {
    final String sql = "insert into oauth2_client(client_name, client_id, client_secret) values(?,?,?)";

    GeneratedKeyHolder keyHolder = new GeneratedKeyHolder();
    jdbcTemplate.update(new PreparedStatementCreator() {
        @Override
        public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
            PreparedStatement psst = connection.prepareStatement(sql, new String[]{"id"});
            int count = 1;
            psst.setString(count++, client.getClientName());
            psst.setString(count++, client.getClientId());
            psst.setString(count++, client.getClientSecret());
            return psst;
        }
    }, keyHolder);

    client.setId(keyHolder.getKey().longValue());
    return client;
}
 
Example 43
Project: oauth_demo   File: UserDaoImpl.java   Source Code and License 6 votes vote down vote up
public User createUser(final User user) {
    final String sql = "insert into oauth2_user(username, password, salt) values(?,?,?)";

    GeneratedKeyHolder keyHolder = new GeneratedKeyHolder();
    jdbcTemplate.update(new PreparedStatementCreator() {
        @Override
        public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
            PreparedStatement psst = connection.prepareStatement(sql, new String[]{"id"});
            int count = 1;
            psst.setString(count++, user.getUsername());
            psst.setString(count++, user.getPassword());
            psst.setString(count++, user.getSalt());
            return psst;
        }
    }, keyHolder);

    user.setId(keyHolder.getKey().longValue());
    return user;
}
 
Example 44
Project: dazzle   File: SpringDaoUtils.java   Source Code and License 6 votes vote down vote up
/**@author [email protected]*/
public static final Long insert(
		JdbcOperations jdbcOperations, 
		final String sql,
		final List<Object> params) {
	KeyHolder keyHolder = new GeneratedKeyHolder();
	try {
		Logger.getLogger("org.dazzle.util").debug("sql ==>" + sql.toString());
		Logger.getLogger("org.dazzle.util").debug("args==>" + params);
		jdbcOperations.update(
				new PreparedStatementCreator() {
					@Override
					public PreparedStatement createPreparedStatement(Connection conn) throws SQLException {
						PreparedStatement ps = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
						setParams(ps, params);
						return ps;
					}
				}, keyHolder);
		if(null != keyHolder.getKey()) {
			return keyHolder.getKey().longValue();
		}
	} catch (RuntimeException e) {
		catchException(e);
	}
	return null;
}
 
Example 45
Project: dazzle   File: SpringDaoUtils.java   Source Code and License 6 votes vote down vote up
/**@author [email protected]*/
public static final void update(
		JdbcOperations jdbcOperations, 
		final String sql, 
		final List<Object> params) {
	try {
		Logger.getLogger("org.dazzle.util").debug("sql ==>" + sql.toString());
		Logger.getLogger("org.dazzle.util").debug("args==>" + params);
		jdbcOperations.update(
				new PreparedStatementCreator() {
					@Override
					public PreparedStatement createPreparedStatement(Connection conn) throws SQLException {
						PreparedStatement ps = conn.prepareStatement(sql.toString());
						setParams(ps, params);
						return ps;
					}
				});
	} catch (RuntimeException e) {
		catchException(e);
	}
}
 
Example 46
Project: dazzle   File: SpringDaoUtils.java   Source Code and License 6 votes vote down vote up
/**@author [email protected]*/
public static final void delete(
		JdbcOperations jdbcOperations, 
		final String sql, 
		final List<Object> params) {
	Logger.getLogger("org.dazzle.util").debug("sql ==>" + sql.toString());
	Logger.getLogger("org.dazzle.util").debug("args==>" + params);
	try {
		jdbcOperations.update(new PreparedStatementCreator() {
			@Override
			public PreparedStatement createPreparedStatement(Connection conn) throws SQLException {
				PreparedStatement ps = conn.prepareStatement(sql);
				setParams(ps, params);
				return ps;
			}
		});
	} catch (RuntimeException e) {
		catchException(e);
	}
}
 
Example 47
Project: dazzle   File: SpringDaoUtils.java   Source Code and License 6 votes vote down vote up
/**@author [email protected]*/
public static final List<Map<String, Object>> find(
		JdbcOperations jdbcOperations, 
		List<Map<String, Object>> resultMappping, 
		Boolean needResultSequence, 
		Boolean needResultFieldFullName, 
		final String sql, 
		final List<Object> params) {
	try {
		Logger.getLogger("org.dazzle.util").debug("sql ==>" + sql.toString());
		Logger.getLogger("org.dazzle.util").debug("args==>" + params);
		List<Map<String, Object>> result = jdbcOperations.query(
				new PreparedStatementCreator() {
					@Override
					public PreparedStatement createPreparedStatement(Connection conn) throws SQLException {
						PreparedStatement ps = conn.prepareStatement(sql);
						setParams(ps, params);
						return ps;
					}
				}, newRowMapper(resultMappping, needResultSequence, needResultFieldFullName, sql, params));
		return result;
	} catch (RuntimeException e) {
		catchException(e);
		return null;
	}
}
 
Example 48
Project: simplerequirementtracker   File: SimpleUserRepositoryImpl.java   Source Code and License 6 votes vote down vote up
@Override
public int addUser(User user) {
	int rows = jdbc.update(new PreparedStatementCreator() {
		public PreparedStatement createPreparedStatement(Connection con) throws SQLException {
			String sql="INSERT INTO Users (userName, userDescription, password) VALUES (?,?,?)";
	        PreparedStatement ps=con.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
	        ps.setString(1,user.getUserName());
	        ps.setString(2, user.getUserDescription());
	        ps.setString(3, user.getPassword());
	        return ps;
		}
	}, keyHolder);
	Long userId=keyHolder.getKey().longValue();
	user.setUserId(userId);
	return rows;
}
 
Example 49
Project: spring_jta   File: ProductDaoJdbc.java   Source Code and License 6 votes vote down vote up
@Override
public Product insert(Product product) {
    Product insProduct = new Product();
    PreparedStatementCreator preparedStatementCreator = connection -> {
        PreparedStatement ps = connection.prepareStatement(SQL_INSERT, new String[]{ID_FIELD});
        ps.setByte(1, product.getFtiny());
        ps.setShort(2, product.getFsmall());
        ps.setLong(3, product.getFbig());
        ps.setDouble(4, product.getFdouble());
        ps.setTimestamp(5, product.getFdate());
        ps.setInt(6, product.getFyear());
        ps.setString(7, product.getFchar());
        ps.setString(8, product.getFvchar());
        ps.setBigDecimal(9, product.getFdec());
        return ps;
    };
    KeyHolder keyHolder = new GeneratedKeyHolder();
    this.getJdbcTemplate().update(preparedStatementCreator, keyHolder);
    insProduct.setId(keyHolder.getKey().intValue());
    return insProduct;
}
 
Example 50
Project: spring_jta   File: OrderDaoJdbc.java   Source Code and License 6 votes vote down vote up
@Override
public Order insert(Order order) {
    Order insOrder = new Order();
    PreparedStatementCreator preparedStatementCreator = connection -> {
        PreparedStatement ps = connection.prepareStatement(SQL_INSERT, new String[]{ID_FIELD});
        ps.setByte(1, order.getFtiny());
        ps.setShort(2, order.getFsmall());
        ps.setLong(3, order.getFbig());
        ps.setDouble(4, order.getFdouble());
        ps.setTimestamp(5, order.getFdate());
        ps.setInt(6, order.getFyear());
        ps.setString(7, order.getFchar());
        ps.setString(8, order.getFvchar());
        ps.setBigDecimal(9, order.getFdec());
        return ps;
    };
    KeyHolder keyHolder = new GeneratedKeyHolder();
    this.getJdbcTemplate().update(preparedStatementCreator, keyHolder);
    insOrder.setId(keyHolder.getKey().intValue());
    return insOrder;
}
 
Example 51
Project: spring_jta   File: UserDaoJdbc.java   Source Code and License 6 votes vote down vote up
@Override
public User insert(User user) {
    User insUser = new User();
    PreparedStatementCreator preparedStatementCreator = connection -> {
        PreparedStatement ps = connection.prepareStatement(SQL_INSERT, new String[]{ID_FIELD});
        ps.setByte(1, user.getFtiny());
        ps.setShort(2, user.getFsmall());
        ps.setLong(3, user.getFbig());
        ps.setDouble(4, user.getFdouble());
        ps.setTimestamp(5, user.getFdate());
        ps.setInt(6, user.getFyear());
        ps.setString(7, user.getFchar());
        ps.setString(8, user.getFvchar());
        ps.setBigDecimal(9, user.getFdec());
        return ps;
    };
    KeyHolder keyHolder = new GeneratedKeyHolder();
    this.getJdbcTemplate().update(preparedStatementCreator, keyHolder);
    insUser.setId(keyHolder.getKey().intValue());
    return insUser;
}
 
Example 52
Project: dchatsdk   File: UserPrefsDAO.java   Source Code and License 6 votes vote down vote up
public synchronized int insert(final UserPrefs p) {
	final String sql = "insert into userPrefs (username,switchNotice,updateTime,filterRule) values(?,?,?,?)";
	KeyHolder keyHolder = new GeneratedKeyHolder();
	jdbcTemplate.update(new PreparedStatementCreator() {
		public PreparedStatement createPreparedStatement(Connection conn) throws SQLException {
			int i = 0;
			PreparedStatement ps = conn.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
			ps.setString(++i, p.getUsername());
			ps.setString(++i, p.getSwitchNotice());
			ps.setTimestamp(++i, new Timestamp(new Date().getTime()));
			ps.setString(++i, p.getFilterRule());
			return ps;
		}
	}, keyHolder);
	return keyHolder.getKey().intValue();
}
 
Example 53
Project: dchatsdk   File: EmbedConfigDAO.java   Source Code and License 6 votes vote down vote up
public int save(final EmbedConfig ec) {
	final String sql = "insert into dchat_embed_config (account,unitName,displayName,duration,indexURL,telephone,editTime)"
			+ " values(?,?,?,?,?,?,?)";
	KeyHolder keyHolder = new GeneratedKeyHolder();
	jdbcTemplate.update(new PreparedStatementCreator() {
		public PreparedStatement createPreparedStatement(Connection conn) throws SQLException {
			int i = 0;
			PreparedStatement ps = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
			ps.setString(++i, ec.getAccount());
			ps.setString(++i, ec.getUnitName());
			ps.setString(++i, ec.getDisplayName());
			ps.setString(++i, ec.getDuration());
			ps.setString(++i, ec.getIndexURL());
			ps.setString(++i, ec.getTelephone());
			ps.setTimestamp(++i, new Timestamp(new Date().getTime()));
			return ps;
		}
	}, keyHolder);
	return keyHolder.getKey().intValue();
}
 
Example 54
Project: firefly   File: HistoryAndTagsDao.java   Source Code and License 6 votes vote down vote up
public SearchInfo addSearchHistory(final String loginname, final String queryString, final String desc, final boolean isFavorite) {
    JdbcTemplate jdbcTemplate = JdbcFactory.getTemplate(DbInstance.operation);
    KeyHolder keyHolder = new GeneratedKeyHolder();
    final String ndesc = desc != null && desc.length() > MAX_DESC_COL_WIDTH ?
                        desc.substring(0, MAX_DESC_COL_WIDTH) : desc;
    final String updateSql = "insert into queryhistory (loginname, historytoken, description, favorite, timeadded, appname) values (?,?,?,?,?,?)";
    final java.util.Date now = new java.util.Date();
    jdbcTemplate.update(new PreparedStatementCreator(){
        public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
            PreparedStatement stmt = connection.prepareStatement(updateSql);
            stmt.setString(1, loginname);
            stmt.setString(2, queryString);
            stmt.setString(3, ndesc);
            stmt.setBoolean(4, isFavorite);
            stmt.setTimestamp(5, new Timestamp(now.getTime()));
            stmt.setString(6, ServerContext.getAppName());
            return stmt;
        }
    }, keyHolder);
    return new SearchInfo(keyHolder.getKey().intValue(), loginname, queryString, desc, isFavorite, now);
}
 
Example 55
Project: service   File: CourseServiceImpl.java   Source Code and License 6 votes vote down vote up
@Override
public long booking(final long userId, final long childId, final long orderId, final long packageId, final CourseSku sku) {
    KeyHolder keyHolder = insert(new PreparedStatementCreator() {
        @Override
        public PreparedStatement createPreparedStatement(Connection con) throws SQLException {
            String sql = "INSERT INTO SG_BookedCourse(UserId, ChildId, OrderId, PackageId, CourseId, CourseSkuId, AddTime) VALUES(?, ?, ?, ?, ?, ?, NOW())";
            PreparedStatement ps = con.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
            ps.setLong(1, userId);
            ps.setLong(2, childId);
            ps.setLong(3, orderId);
            ps.setLong(4, packageId);
            ps.setLong(5, sku.getCourseId());
            ps.setLong(6, sku.getId());

            return ps;
        }
    });

    return keyHolder.getKey().longValue();
}
 
Example 56
Project: service   File: OrderServiceImpl.java   Source Code and License 6 votes vote down vote up
@Override
public long add(final Order order) {
    KeyHolder keyHolder = insert(new PreparedStatementCreator() {
        @Override
        public PreparedStatement createPreparedStatement(Connection con) throws SQLException {
            String sql = "INSERT INTO SG_SubjectOrder(UserId, SubjectId, Contact, Mobile, InviteCode, CouponCode, AddTime) VALUES(?, ?, ?, ?, ?, ?, NOW())";
            PreparedStatement ps = con.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
            ps.setLong(1, order.getUserId());
            ps.setLong(2, order.getSubjectId());
            ps.setString(3, order.getContact());
            ps.setString(4, order.getMobile());
            ps.setString(5, order.getInviteCode());
            ps.setString(6, order.getCouponCode());

            return ps;
        }
    });

    long orderId = keyHolder.getKey().longValue();
    if (orderId < 0) throw new MomiaErrorException("下单失败");

    addOrderPackages(orderId, order);

    return orderId;
}
 
Example 57
Project: service   File: CourseCommentServiceImpl.java   Source Code and License 6 votes vote down vote up
private long addComment(final CourseComment comment) {
    KeyHolder keyHolder = insert(new PreparedStatementCreator() {
        @Override
        public PreparedStatement createPreparedStatement(Connection con) throws SQLException {
            String sql = "INSERT INTO SG_CourseComment(UserId, BookingId, CourseId, Star, Teacher, Environment, Content, AddTime) VALUES(?, ?, ?, ?, ?, ?, ?, NOW())";
            PreparedStatement ps = con.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
            ps.setLong(1, comment.getUserId());
            ps.setLong(2, comment.getBookingId());
            ps.setLong(3, comment.getCourseId());
            ps.setInt(4, comment.getStar());
            ps.setInt(5, comment.getTeacher());
            ps.setInt(6, comment.getEnvironment());
            ps.setString(7, comment.getContent());

            return ps;
        }
    });

    return keyHolder.getKey().longValue();
}
 
Example 58
Project: service   File: UserServiceImpl.java   Source Code and License 6 votes vote down vote up
@Override
public long add(final String nickName, final String mobile, final String password) {
    KeyHolder keyHolder = insert(new PreparedStatementCreator() {
        @Override
        public PreparedStatement createPreparedStatement(Connection con) throws SQLException {
            String sql = "INSERT INTO SG_User(NickName, Mobile, Password, Token, InviteCode, AddTime) VALUES (?, ?, ?, ?, ?, NOW())";
            PreparedStatement ps = con.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
            ps.setString(1, nickName);
            ps.setString(2, mobile);
            ps.setString(3, encryptPassword(mobile, password, Configuration.getString("SecretKey.Password")));
            ps.setString(4, generateToken(mobile));
            ps.setString(5, generateInviteCode(nickName, mobile));

            return ps;
        }
    });

    return keyHolder.getKey().longValue();
}
 
Example 59
Project: service   File: SaleServiceImpl.java   Source Code and License 6 votes vote down vote up
@Override
public long add(final Sale sale) {
    KeyHolder keyHolder = insert(new PreparedStatementCreator() {
        @Override
        public PreparedStatement createPreparedStatement(Connection con) throws SQLException {
            String sql = "INSERT INTO SG_Sale (SaleCode, Mobile, Address, AddTime) VALUES (?, ?, ?, NOW())";
            PreparedStatement ps = con.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
            ps.setString(1, sale.getSaleCode());
            ps.setString(2, sale.getMobile());
            ps.setString(3, sale.getAddress().equals("") ? "" : sale.getAddress());
            return ps;
        }
    });

    return keyHolder.getKey().longValue();
}
 
Example 60
Project: service   File: ChildServiceImpl.java   Source Code and License 6 votes vote down vote up
@Override
public long add(final Child child) {
    KeyHolder keyHolder = insert(new PreparedStatementCreator() {
        @Override
        public PreparedStatement createPreparedStatement(Connection con) throws SQLException {
            String sql = "INSERT INTO SG_Child (UserId, Avatar, Name, Sex, Birthday, AddTime) VALUES (?, ?, ?, ?, ?, NOW())";
            PreparedStatement ps = con.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
            ps.setLong(1, child.getUserId());
            ps.setString(2, child.getAvatar());
            ps.setString(3, child.getName());
            ps.setString(4, child.getSex());
            ps.setDate(5, new java.sql.Date(child.getBirthday().getTime()));

            return ps;
        }
    });

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