
๐ก ์์กด์ฑ ์ต์ (build.gradle > dependencies)
implemenataionย ์ต์
- ์ง์ ์ ์ธ ์์กด์ฑ์ ์ถ๊ฐํ ๋ ์ฌ์ฉ
- ํ๋ก์ ํธ ์ปดํ์ผ ์ ํ์ํ์ง๋ง, ์ธ๋ถ๋ก ๊ณต๊ฐ โ
runtimeOnlyย ์ต์
- ๋ฐํ์ ์์ ์ ํ์ํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ
testImplementationย ์ต์
- ํ ์คํธ ์ฝ๋๋ฅผ ์ํํ ๋ ์ ์ฉํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ
๐กJava ๊ธฐ๋ฐ์ RDBMS(Database Engine) ์ฆ, DB ์์ง
SQL์ ์ฌ์ฉํ๊ณ , JDBC๋ก ์ฐ๊ฒฐํ ์ ์๋ ๊ด๊ณํ ๋ฐ์ดํฐ๋ฒ ์ด์ค ์์คํ
build.gradle or application.properties ์ค์ )build.gradle or application.properties ์ค์ )| Mode | H2 ๋ค์ด๋ก๋ ์ฌ๋ถ | ์คํ ์ฃผ์ฒด | DB ์ ์ฅ ์์น | ์ฌ์ฉ ์ฉ๋ |
|---|---|---|---|---|
| โ Server Mode | O | ์ธ๋ถ | ๋ก์ปฌ (ํ์ผ ์์คํ ) | ๋ฐฐํฌ ์ฉ๋ |
| โ In-Memory Mode | X | ์คํ๋ง | ๋ฉ๋ชจ๋ฆฌ | ํ ์คํธ ์ฉ๋ |
| โ Embedded Mode | X | ์คํ๋ง | ๋ก์ปฌ (ํ์ผ ์์คํ ) | ๊ฐ๋ฐ ์ฉ๋ |
๐ก
jbbc:h2:~/test:~์ ํ๋๋ ํ ๋ฆฌ, ํ๋๋ ํ ๋ฆฌ์ test์ ์ ์ฅ๋๋คโ๏ธ
application.ymlspring: datasource: driver-class-name: org.h2.Driver โ url: jdbc:h2:mem:{DB ์ด๋ฆ} ----> 2๏ธโฃ In-Memory Mode โ url: jdbc:h2:{DB๊ฐ ์ ์ฅ๋ ๊ฒฝ๋ก} ----> 3๏ธโฃ Embedded Mode username: sa password:
โ๏ธ
application.properties# application.properties spring.datasource.driver-class-name=org.h2.Driver โ spring.datasource.url=jdbc:h2:mem:{DB ์ด๋ฆ} ----> 2๏ธโฃ In-Memory Mode โ spring.datasource.url=jdbc:h2:{DB๊ฐ ์ ์ฅ๋ ๊ฒฝ๋ก} ----> 3๏ธโฃ Embedded Mode spring.datasource.username=sa spring.datasource.password=
โ๏ธ
build.gradle
![]()
โ
Path์ฐธ๊ณ ํด์ ์ฝ์ url ์ ์![]()
โ properties ์ค์ ๊ณผ ๋๊ฐ์ด ์์ฑํ๊ธฐ (
url,username,password...)![]()
๐ก In-Memory ๋ชจ๋์์ ์ฝ์ ์กฐํ๊ฐ ์๋๋ ๊ฒฝ์ฐ
#url: 'jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE'
create table
alter table
drop table
TRUNCATE TABLE orders; -> ๋ฐ์ดํฐ ์ด๊ธฐํ ( ํ
์ด๋ธ ๋ด์ฉ ์ง์ฐ๊ธฐ )
TRUNCATE TABLE products;
TRUNCATE TABLE users;
๋ฐ์ดํฐ ์ฐพ๊ธฐ
select where ๋ฑ..
order by group by
join
๋ฐ์ดํฐ ์กฐ์ํ๊ธฐ
insert into
update
delete from
์ ํ๋ฆฌ์ผ์ด์ - ๋ฐ์ดํฐ๋ฒ ์ด์ค ๊ฐ์ ํต์ ์ ์ค๊ฐํ๋ ์ญํ

Spring Boot๋ ๋ค์ํ JDBC ๋๋ผ์ด๋ฒ๋ฅผ ์ง์ํ๋ค
-spring-boot-starter-jdbc
: ์คํํฐ ํจํค์ง, ์ฃผ์ ์์กด์ฑ, ์๋๊ตฌ์ฑ ๊ธฐ๋ฅ์ด ํฌํจ๋์ด ์์
: ์ฝ๋ ๊ฐ์ํ, ์๋ ๊ตฌ์ฑ, ์์ธ์ฒ๋ฆฌ ( JDBC์์ ๋ฐ์ํ๋ ์์ธ -> Spring ์์ธ์ฒด๊ณ๋ก ๋ณํ)
โ ์๋์ผ๋ก close()๋ฅผ ํธ์ถํด์ฃผ๋ try ๊ตฌ๋ฌธ์ด์ผ. (์์ ํด์ )
JDBC์์ Connection, Statement, ResultSet ๊ฐ์ ๊ฐ์ฒด๋ ๋ฐ๋์ ๋ซ์์ค์ผ ํด.
๊ทธ๋ฐ๋ฐ ์ค์๋ก connection.close()๋ฅผ ์ ์ฐ๊ฑฐ๋, return ์ ์ ๋น ์ง๋ฉด ๋ฆฌ์์ค ๋์๊ฐ ๋์ง.
๊ทธ๋์ Java7๋ถํฐ๋ AutoCloseable ์ธํฐํ์ด์ค๋ฅผ ๊ตฌํํ ๊ฐ์ฒด๋ try ๊ดํธ ์์ ๋ฃ์ผ๋ฉด ์๋์ผ๋ก close()ํด์ค!

| ๊ตฌ๋ถ | try-catch | try-with-resources |
|---|---|---|
| ๋ชฉ์ | ์์ธ ์ฒ๋ฆฌ | ์์ธ ์ฒ๋ฆฌ + ๋ฆฌ์์ค ์๋ ๋ฐํ |
| ๋ฆฌ์์ค close | ์ง์ ๋ช
์ํด์ผ ํจ (finally์์) | ์๋ ํธ์ถ (close() ํธ์ถ๋จ) |
| ์ฝ๋ ๊ธธ์ด | ๋ ๊ธธ๊ณ ๋ณต์กํจ | ์งง๊ณ ์์ ์ ์ |
| ์ฌ์ฉ ์กฐ๊ฑด | ์ ํ ์์ | AutoCloseable ๊ตฌํ ๊ฐ์ฒด ํ์ |
@SpringBootApplication
public class JdbcApplication {
public static void main(String[] args) throws SQLException {
// ์ดํ๋ฆฌ์ผ์ด์
์คํ ์ปจํ
์คํธ ์์ฑ
SpringApplication.run(JdbcApplication.class, args);
// โถ๏ธ ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ฐ๊ฒฐ
String url = "jdbc:h2:mem:test"; // โ
in-memory ๋ชจ๋ H2 DB
String username = "sa"; // ์ฌ์ฉ์๋ช
// ๐น DriverManager.getConnection()
// - JDBC ๋๋ผ์ด๋ฒ๋ฅผ ํตํด DB์ ์ฐ๊ฒฐ
try (Connection connection = DriverManager.getConnection(url, username, null)) { // โ
ํ๋ผ๋ฏธํฐ๋ก ์ฐ๊ฒฐ ์ ๋ณด ๋ฃ์ด์ฃผ๊ธฐ
try {
// โถ๏ธ ํ
์ด๋ธ ์์ฑ
// ๐น connection.prepareStatement(String sql)
// - SQL ๋ฌธ์ ๋ฏธ๋ฆฌ ์ปดํ์ผ๋ ์ํ๋ก ์ค๋น
String creatSql = "CREATE TABLE USERS (id SERIAL, username varchar(255))";
try (PreparedStatement statement = connection.prepareStatement(creatSql)) {
// ๐น statement.execute()
// - ์ปดํ์ผ ๋ SQL๋ฌธ์ ์คํ
statement.execute();
}
// โถ๏ธ ๋ฐ์ดํฐ ์ถ๊ฐ
String insertSql = "INSERT INTO USERS (username) VALUES ('teasun kim')";
try (PreparedStatement statement = connection.prepareStatement(insertSql)) {
statement.execute();
}
// โถ๏ธ ๋ฐ์ดํฐ ์กฐํ
String selectSql = "SELECT * FROM USERS";
try (PreparedStatement statement = connection.prepareStatement(selectSql)) {
// ๐น statement.executeQuery()
// - SELECT ๋ฌธ ์คํ
var rs = statement.executeQuery();
// ๐น rs.next()
while (rs.next()) {
// ๐น rs.getInt("์ปฌ๋ผ๋ช
")
// ๐น rs.getString("์ปฌ๋ผ๋ช
")
System.out.printf("%d, %s", rs.getInt("id"), rs.getString("username"));
}
}
} catch (SQLException e) {
// ์์ธ ๋ฉ์์ง๋ฅผ ๋ฌธ์์ด๋ก ๋น๊ตํด ์์ธ ์ํฉ์ ๋ถ๊ธฐ ์ฒ๋ฆฌ
if (e.getMessage().equals("ERROR: relation \"account\" already exists")) {
System.out.println("USERS ํ
์ด๋ธ์ด ์ด๋ฏธ ์กด์ฌํฉ๋๋ค.");
} else {
// ๊ทธ ์ธ ์์ธ๋ ๋ฐํ์ ์์ธ๋ก ๊ฐ์ธ์ ๋์ง
throw new RuntimeException();
}
}
}
}
}