H2 데이터베이스?
- 별도의 설치X, 적은 용량의 프로그램, application.properties(yml) 설정이 단순하다. -> 가벼운 개발 및 테스트에는 H2 데이터베이스 사용
- 대규모 데이터 처리에는 불리, 일부 기능 제한, 동시성 처리에 불리
- 서비스 배포시에는 MySQL 사용
H2 데이터베이스 사용하기 (윈도우 기준)
1️⃣ 아래 사이트에서 H2 데이터베이스를 다운로드하고 압축 해제
H2 Database Engine (redirect)
H2 Database Engine Welcome to H2, the free SQL database. The main feature of H2 are: It is free to use for everybody, source code is included Written in Java, but also available as native executable JDBC and (partial) ODBC API Embedded and client/server mo
www.h2database.com

2️⃣[h2] > [bin] 폴더 에서 h2.bat 실행

만약 접속이 안된다면 url 주소를 바꿔 접속해보자.
Ex) 219.33.137.32:8082/login.jsp?jsessionid=7517c43bf68f0a46135135e87ba
-> localhost:8082/ login.jsp?jsessionid=7517c43bf68f0a46135135e87ba
3️⃣JDBC URL이 jdbc:h2:~/test 인지 확인하고 연결하고 껐다가 다시 킨다. (최초 1회만 진행)
그 다음으로 JDBC URL을 JDBC URL을 jdbc:h2:tcp://localhost/~/test 으로 작성하여 연결합니다. (앞으로는 jdbc:h2:tcp://localhost/~/test 으로 작성하여 연결)

4️⃣아래 화면까지 왔다면 성공! cmd 창은 계속 끄지 않고 유지하고 있어야 합니다.
자유롭게 명령어를 작성하여 SQL 작업을 하면 됩니다.

행 삽입 (테이블에 데이터 추가) | -- member 테이블에서 name 칼럼의 값을 spring으로 지정하여 데이터 삽입 INSERT INTO member(name) VALUES('spring') |
행 삭제 (테이블에 특정 데이터 삭제) | -- member 테이블에서 name 값이 spring인 행을 제거 DELETE FROM member WHERE name='spring' |
행 수정 (테이블의 특정 데이터 수정) | -- member 테이블에서 name 값이 spring 인 것을 hello으로 수정 UPDATE member SET name='hello' WHERE name='spring' |
Spring과 H2 데이터베이스 연결하기
5️⃣build.gradle에 연결 설정 추가
[Build] > [Build Project] 로 적용(빌드)해준다.
plugins {
id 'java'
id 'org.springframework.boot' version '3.4.4'
id 'io.spring.dependency-management' version '1.1.7'
}
group = 'hello'
version = '0.0.1-SNAPSHOT'
java {
toolchain {
languageVersion = JavaLanguageVersion.of(17)
}
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.springframework.boot:spring-boot-starter-web'
// jdbc, jpa, h2 데이터베이스 관련 라이브러리 추가
// implementation 'org.springframework.boot:spring-boot-starter-jdbc'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
runtimeOnly 'com.h2database:h2'
// ------------------------------------------------------------------------------
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
}
tasks.named('test') {
useJUnitPlatform()
}
6️⃣application.properties 에 데이터베이스 연결 설정 추가
spring.datasource.url=jdbc:h2:tcp://localhost/~/test
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.username=sa