[Java] Connection with local DB (IntelliJ + SpringBoot)
Connection with DB by IJ + Spring Boot
- It is troublesome to start Spring Boot every time and check it, so ** create a dummyDB of H2 locally and test it **
Select Data Base tab> Data Source> H2
Data Sources and Drivers
- DB name to dummy, ** Connection type to in-memory **
- Confirm connection with Test Connection (check green)
- Now you have a ** DB ** for wall hitting!
** Write and execute the schema to be executed on the H2 console **
create table if not exists employee (
employee_id bigint primary key,
employee_name varchar(50),
age int
);
** When executed, tabel is created **
- If the corresponding item is in the Data Base View, it will check if the field name matches.
- Execution statement selection on the console (IntelliJ can be selected if there are multiple SQLs)
INSERT INTO employee(employee_id,employee_name,age)
VALUES(1,'Harry Potter',11);
INSERT INTO employee(employee_id,employee_name,age)
VALUES(2,'Hermione Granger',11);
INSERT INTO employee(employee_id,employee_name,age)
VALUES(3,'Ron Weasley',12);
INSERT INTO employee(employee_id,employee_name,age)
VALUES(4,'Albus Dumbledore',110);
--When to delete
-- DELETE FROM EMPLOYEE
-- WHERE employee_id = 1
Check if DB is created
- Click the table created on the DataBase tab and check if the data can be inserted.
- ** I was able to confirm locally that there were no grammatical problems! ** **
create data.sql
- Created by copying the sql statement to
data.sql
- Add classpath
data: classpath: data.sql
to application.yml
application.yml
spring:
datasource:
url: jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
username: sa
password:
driver-class-name: org.h2.Driver
schema: classpath:schema.sql
data: classpath:data.sql
h2:
console:
enabled: true
You can also check it on the h2 console!
- http://localhost:8080/h2-console/