|
15 | 15 | @SpringBootApplication |
16 | 16 | public class RelationalDataAccessApplication implements CommandLineRunner { |
17 | 17 |
|
18 | | - private static final Logger log = LoggerFactory.getLogger(RelationalDataAccessApplication.class); |
| 18 | + private static final Logger log = LoggerFactory.getLogger(RelationalDataAccessApplication.class); |
19 | 19 |
|
20 | | - public static void main(String args[]) { |
21 | | - SpringApplication.run(RelationalDataAccessApplication.class, args); |
22 | | - } |
| 20 | + public static void main(String args[]) { |
| 21 | + SpringApplication.run(RelationalDataAccessApplication.class, args); |
| 22 | + } |
23 | 23 |
|
24 | | - @Autowired |
25 | | - JdbcTemplate jdbcTemplate; |
| 24 | + @Autowired |
| 25 | + JdbcTemplate jdbcTemplate; |
26 | 26 |
|
27 | | - @Override |
28 | | - public void run(String... strings) throws Exception { |
| 27 | + @Override |
| 28 | + public void run(String... strings) throws Exception { |
29 | 29 |
|
30 | | - log.info("Creating tables"); |
| 30 | + log.info("Creating tables"); |
31 | 31 |
|
32 | | - jdbcTemplate.execute("DROP TABLE customers IF EXISTS"); |
33 | | - jdbcTemplate.execute("CREATE TABLE customers(" + |
34 | | - "id SERIAL, first_name VARCHAR(255), last_name VARCHAR(255))"); |
| 32 | + jdbcTemplate.execute("DROP TABLE customers IF EXISTS"); |
| 33 | + jdbcTemplate.execute("CREATE TABLE customers(" + |
| 34 | + "id SERIAL, first_name VARCHAR(255), last_name VARCHAR(255))"); |
35 | 35 |
|
36 | | - // Split up the array of whole names into an array of first/last names |
37 | | - List<Object[]> splitUpNames = Arrays.asList("John Woo", "Jeff Dean", "Josh Bloch", "Josh Long").stream() |
38 | | - .map(name -> name.split(" ")) |
39 | | - .collect(Collectors.toList()); |
| 36 | + // Split up the array of whole names into an array of first/last names |
| 37 | + List<Object[]> splitUpNames = Arrays.asList("John Woo", "Jeff Dean", "Josh Bloch", "Josh Long").stream() |
| 38 | + .map(name -> name.split(" ")) |
| 39 | + .collect(Collectors.toList()); |
40 | 40 |
|
41 | | - // Use a Java 8 stream to print out each tuple of the list |
42 | | - splitUpNames.forEach(name -> log.info(String.format("Inserting customer record for %s %s", name[0], name[1]))); |
| 41 | + // Use a Java 8 stream to print out each tuple of the list |
| 42 | + splitUpNames.forEach(name -> log.info(String.format("Inserting customer record for %s %s", name[0], name[1]))); |
43 | 43 |
|
44 | | - // Uses JdbcTemplate's batchUpdate operation to bulk load data |
45 | | - jdbcTemplate.batchUpdate("INSERT INTO customers(first_name, last_name) VALUES (?,?)", splitUpNames); |
| 44 | + // Uses JdbcTemplate's batchUpdate operation to bulk load data |
| 45 | + jdbcTemplate.batchUpdate("INSERT INTO customers(first_name, last_name) VALUES (?,?)", splitUpNames); |
46 | 46 |
|
47 | | - log.info("Querying for customer records where first_name = 'Josh':"); |
48 | | - jdbcTemplate.query( |
49 | | - "SELECT id, first_name, last_name FROM customers WHERE first_name = ?", new Object[] { "Josh" }, |
50 | | - (rs, rowNum) -> new Customer(rs.getLong("id"), rs.getString("first_name"), rs.getString("last_name")) |
51 | | - ).forEach(customer -> log.info(customer.toString())); |
52 | | - } |
| 47 | + log.info("Querying for customer records where first_name = 'Josh':"); |
| 48 | + jdbcTemplate.query( |
| 49 | + "SELECT id, first_name, last_name FROM customers WHERE first_name = ?", new Object[] { "Josh" }, |
| 50 | + (rs, rowNum) -> new Customer(rs.getLong("id"), rs.getString("first_name"), rs.getString("last_name")) |
| 51 | + ).forEach(customer -> log.info(customer.toString())); |
| 52 | + } |
53 | 53 | } |
0 commit comments