Guest User

Untitled

a guest
Oct 12th, 2018
447
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.90 KB | None | 0 0
  1. CREATE TABLE CUSTOMER(
  2. ID NUMBER(10) NOT NULL,
  3. NAME VARCHAR2(100) NOT NULL,
  4. EMAIL VARCHAR2(100) NOT NULL,
  5. CREATED_DATE DATE NOT NULL,
  6. CONSTRAINT CUSTOMER_PK PRIMARY KEY (ID)
  7. );
  8.  
  9. INSERT INTO "CUSTOMER" (ID, NAME, EMAIL, CREATED_DATE) VALUES(1,
  10. 'mkyong','111@yahoo.com', TO_DATE('2017-02-11', 'yyyy-mm-dd'));
  11. INSERT INTO "CUSTOMER" (ID, NAME, EMAIL, CREATED_DATE) VALUES(2,
  12. 'yflow','222@yahoo.com', TO_DATE('2017-02-12', 'yyyy-mm-dd'));
  13. INSERT INTO "CUSTOMER" (ID, NAME, EMAIL, CREATED_DATE) VALUES(3,
  14. 'zilap','333@yahoo.com', TO_DATE('2017-02-13', 'yyyy-mm-dd'));
  15.  
  16. import java.sql.Date;
  17.  
  18. public class Customer {
  19.  
  20. int id;
  21. String name;
  22. String email;
  23. Date date;
  24.  
  25. public Customer(int id, String name, String email, Date date) {
  26. this.id = id;
  27. this.name = name;
  28. this.email = email;
  29. this.date = date;
  30. }
  31. }
  32.  
  33. import com.example.My.model.Customer;
  34. import org.springframework.beans.factory.annotation.Autowired;
  35. import org.springframework.context.annotation.ComponentScan;
  36. import org.springframework.jdbc.core.JdbcTemplate;
  37. import org.springframework.stereotype.Repository;
  38.  
  39. import java.util.List;
  40.  
  41. @Repository
  42.  
  43. public class CustomerRepository {
  44.  
  45. @Autowired
  46. private JdbcTemplate jdbcTemplate;
  47.  
  48. // thanks Java 8, look the custom RowMapper
  49. public List<Customer> findAll() {
  50.  
  51. List<Customer> result = jdbcTemplate.query(
  52. "SELECT id, name, email, created_date FROM customer",
  53. (rs, rowNum) -> new Customer(rs.getInt("id"),
  54. rs.getString("name"), rs.getString("email"),
  55. rs.getDate("created_date"))
  56. );
  57.  
  58. return result;
  59.  
  60. }
  61. }
  62.  
  63. package com.example.My;
  64.  
  65. import com.example.My.dao.CustomerRepository;
  66. import com.example.My.model.Customer;
  67. import org.springframework.beans.factory.annotation.Autowired;
  68. import org.springframework.boot.CommandLineRunner;
  69. import org.springframework.boot.SpringApplication;
  70. import org.springframework.boot.autoconfigure.SpringBootApplication;
  71.  
  72. import javax.activation.DataSource;
  73. import java.util.List;
  74.  
  75. import static java.lang.System.exit;
  76.  
  77. @SpringBootApplication
  78.  
  79. public class MyApplication implements CommandLineRunner {
  80.  
  81. /**
  82. *
  83. */
  84. @Autowired
  85. DataSource dataSource;
  86.  
  87. @Autowired
  88. CustomerRepository customerRepository;
  89.  
  90.  
  91. public static void main(String[] args) {
  92. SpringApplication.run(MyApplication.class, args);
  93. }
  94.  
  95.  
  96. @Override
  97. public void run(String... args) throws Exception {
  98.  
  99. System.out.println("DATASOURCE = " + dataSource);
  100.  
  101. /// Get dbcp2 datasource settings
  102. // BasicDataSource newds = (BasicDataSource) dataSource;
  103. // System.out.println("BasicDataSource = " + newds.getInitialSize());
  104.  
  105. System.out.println("Display all customers...");
  106. List<Customer> list = customerRepository.findAll();
  107. list.forEach(x -> System.out.println(x));
  108.  
  109. System.out.println("Done!");
  110.  
  111. exit(0);
  112. }
  113.  
  114. }
Add Comment
Please, Sign In to add comment