Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions src/main/java/io/zipcoder/persistenceapp/DataSourceConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package io.zipcoder.persistenceapp;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;

import javax.sql.DataSource;

@Configuration
@ComponentScan
public class DataSourceConfig {

@Bean
public JdbcTemplate jdbcTemplate(){
return new JdbcTemplate(dataSource());
}

@Bean(name = "dataSource")
public DataSource dataSource(){
EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
builder.setName("testdb");
EmbeddedDatabase build = builder
.setName("testdb;MODE=MySQL;DB_CLOSE_ON_EXIT=false")
.setType(EmbeddedDatabaseType.H2)
.addScript("classpath:schema-h2.sql")
.addScript("classpath:data-h2.sql")
.build();
return build;

}
}
72 changes: 72 additions & 0 deletions src/main/java/io/zipcoder/persistenceapp/Person.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package io.zipcoder.persistenceapp;

import java.util.Date;

public class Person {
private Integer id;
private String first_name;
private String last_name;
private String mobile;
private Date birthday;
private Integer home_id;

public Person (){}

public Person(Integer id, String first_name, String last_name,
String mobile, Date birthday, Integer home_id) {
this.id = id;
this.first_name = first_name;
this.last_name = last_name;
this.mobile = mobile;
this.birthday = birthday;
this.home_id = home_id;
}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getFirst_name() {
return first_name;
}

public void setFirst_name(String first_name) {
this.first_name = first_name;
}

public String getLast_name() {
return last_name;
}

public void setLast_name(String last_name) {
this.last_name = last_name;
}

public String getMobile() {
return mobile;
}

public void setMobile(String mobile) {
this.mobile = mobile;
}

public Date getBirthday() {
return birthday;
}

public void setBirthday(Date birthday) {
this.birthday = birthday;
}

public Integer getHome_id() {
return home_id;
}

public void setHome_id(Integer home_id) {
this.home_id = home_id;
}
}
8 changes: 8 additions & 0 deletions src/main/java/io/zipcoder/persistenceapp/PersonService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package io.zipcoder.persistenceapp;

import org.springframework.stereotype.Service;

@Service
public class PersonService {

}
13 changes: 11 additions & 2 deletions src/main/resources/application-h2.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
spring.datasource.url=jdbc:h2:mem:testdb;Mode=Oracle
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.platform=h2
spring.datasource.username=sa
spring.datasource.password=password
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect

spring.jpa.hibernate.ddl-auto=none
spring.datasource.continue-on-error=true


spring.h2.console.path=/h2-console
spring.h2.console.settings.trace=false
spring.h2.console.settings.web-allow-others=false
11 changes: 10 additions & 1 deletion src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
spring.profiles.active=h2
logging.level.org.springframework.boot.context.embedded=INFO
spring.jpa.database-platform=org.hibernate.dialect.Oracle10gDialect
spring.jpa.database-platform=org.hibernate.dialect.Oracle10gDialect

spring.datasource.tomcat.test-on-borrow=true
spring.datasource.tomcat.validation-query=SELECT 1
spring.datasource.tomcat.validation-interval=0


#org.postgresql.Driver=select version();

#spring.datasource.url = jdbc:h2:file:/src/main/resources/schema-h2.sql
13 changes: 13 additions & 0 deletions src/main/resources/data-h2.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
INSERT INTO HOME (ADDRESS, HOMENUMBER) VALUES ('36 E. Bayberry Rd.Savannah, GA 31404', '565-6895');
INSERT INTO HOME (ADDRESS, HOMENUMBER) VALUES ('11 Essex Dr.Farmingdale, NY 11735', '454-4544');
INSERT INTO HOME (ADDRESS, HOMENUMBER) VALUES ('920 Arlington Street Clifton, NJ 07011', '985-4515');
INSERT INTO HOME (ADDRESS, HOMENUMBER) VALUES ('234 High Street, PA 19159 ', '267-3940');


INSERT INTO PERSON ( LAST_NAME, FIRST_NAME, MOBILE, BIRTHDAY, HOME_ID ) VALUES ('Carbral', 'Sheeri', '230-4233', '1970-02-23', 2);
INSERT INTO PERSON ( LAST_NAME, FIRST_NAME, MOBILE, BIRTHDAY, HOME_ID) VALUES ( 'Sharam', 'Raj', '186-5223', '1980-08-31', 3);
INSERT INTO PERSON ( LAST_NAME, FIRST_NAME, MOBILE, BIRTHDAY, HOME_ID)VALUES ('Durand', 'Noelle', '395-6161', '1960-07-06', 1);
INSERT INTO PERSON ( LAST_NAME, FIRST_NAME, MOBILE, BIRTHDAY, HOME_ID)VALUES ('Smith', 'Thomas', '395-6181', '1987-07-06', 1);
INSERT INTO PERSON ( LAST_NAME, FIRST_NAME, MOBILE, BIRTHDAY, HOME_ID)VALUES ('Smith', 'Jane', '393-6181', '1987-12-06', 3);
INSERT INTO PERSON ( LAST_NAME, FIRST_NAME, MOBILE, BIRTHDAY, HOME_ID)VALUES ('Brown', 'Doug', '466-6241', '1954-12-07', 3);

24 changes: 24 additions & 0 deletions src/main/resources/script.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
INSERT INTO movies (id, title, runtime, genre, imdb_score, rating) VALUES (DEFAULT, 'Howard the Duck', '110','Sci-Fi', '4.6', 'PG');
INSERT INTO movies (id, title, runtime, genre, imdb_score, rating) VALUES (DEFAULT, 'Lavalantula', '83','Horror', '4.7', 'TV-14');
INSERT INTO movies (id, title, runtime, genre, imdb_score, rating) VALUES (DEFAULT, 'Starship Trooper', '129', 'Sci-Fi', '7.2', 'PG-13');
INSERT INTO movies (id, title, runtime, genre, imdb_score, rating) VALUES (DEFAULT, 'Waltz With Bashir', '90', 'Documentary', '8.0', 'R');
INSERT INTO movies (id, title, runtime, genre, imdb_score, rating) VALUES (DEFAULT, 'Spaceballs', '96', 'Comedy', '7.1', 'PG');
INSERT INTO movies (id, title, runtime, genre, imdb_score, rating) VALUES (DEFAULT, 'Monsters Inc.', '92', 'Animation', '8.1', 'G');

SELECT * FROM movies WHERE genre='Sci-Fi';

SELECT * FROM movies WHERE imdb_score > 6.5;

SELECT * FROM movies WHERE rating='G' OR rating='PG' AND runtime < 100;

SELECT genre, AVG(runtime) FROM movies WHERE imdb_score <= 7 GROUP BY genre;

UPDATE movies SET rating='R' WHERE title='Starship Troopers';

SELECT id, rating FROM movies WHERE genre='Horror' OR genre='Documentary';

SELECT AVG(imdb_score), MAX(imdb_score), MIN(imdb_score) FROM movies GROUP BY rating;

SELECT AVG(imdb_score), MAX(imdb_score), MIN(imdb_score) FROM movies GROUP BY rating HAVING COUNT(*) > 1;

DELETE FROM movies WHERE rating='R';