Skip to content

Commit 065ec38

Browse files
committed
자바jdbc
1 parent 3f24d0b commit 065ec38

File tree

3 files changed

+176
-0
lines changed

3 files changed

+176
-0
lines changed

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ dependencies {
1818
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
1919
testImplementation 'org.springframework.boot:spring-boot-starter-test'
2020
testImplementation 'io.rest-assured:rest-assured:5.3.1'
21+
runtimeOnly 'com.h2database:h2'
2122
}
2223

2324
test {
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
package roomescape.dao;
2+
3+
import jakarta.annotation.PostConstruct;
4+
import java.sql.Connection;
5+
import java.sql.DriverManager;
6+
import java.sql.PreparedStatement;
7+
import java.sql.SQLException;
8+
import java.time.LocalDate;
9+
import java.time.LocalTime;
10+
import java.time.format.DateTimeFormatter;
11+
import org.springframework.stereotype.Component;
12+
import roomescape.domain.Reservation;
13+
14+
@Component
15+
public class ReservationDAO {
16+
17+
private static final String URL = "jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1";
18+
private static final String USERNAME = "sa";
19+
private static final String PASSWORD = "";
20+
21+
public Connection getConnection() {
22+
try {
23+
return DriverManager.getConnection(URL, USERNAME, PASSWORD);
24+
} catch (final SQLException e) {
25+
throw new RuntimeException("DB 연결 실패: " + e.getMessage(), e);
26+
}
27+
}
28+
29+
30+
@PostConstruct
31+
public void createTable() {
32+
33+
final var query = "CREATE TABLE IF NOT EXISTS RESERVATION (" +
34+
"ID BIGINT PRIMARY KEY, " +
35+
"NAME VARCHAR(50), " +
36+
"DATE VARCHAR(20), " +
37+
"TIME VARCHAR(20))";
38+
39+
try (
40+
final var connection = getConnection();
41+
) {
42+
connection.createStatement().execute(query);
43+
} catch (final SQLException e) {
44+
throw new RuntimeException(e);
45+
}
46+
}
47+
48+
public void addReservation(final Reservation reservation) {
49+
final var query = "INSERT INTO RESERVATION VALUES(?, ?, ?, ?)";
50+
try (
51+
final var connection = getConnection();
52+
final var preparedStatement = connection.prepareStatement(query);
53+
) {
54+
preparedStatement.setLong(1, reservation.getId());
55+
preparedStatement.setString(2, reservation.getName());
56+
preparedStatement.setString(3, reservation.getDate().toString());
57+
preparedStatement.setString(4, reservation.getTime().toString());
58+
preparedStatement.executeUpdate();
59+
} catch (final SQLException e) {
60+
throw new RuntimeException(e);
61+
}
62+
}
63+
64+
public Reservation findReservation(final int id) {
65+
final var query = "SELECT * FROM RESERVATION WHERE ID = ?";
66+
try (
67+
final var connection = getConnection();
68+
final var preparedStatement = connection.prepareStatement(query);
69+
) {
70+
preparedStatement.setLong(1, id);
71+
72+
final var resultSet = preparedStatement.executeQuery();
73+
74+
if (resultSet.next()) {
75+
LocalDate date = LocalDate.parse(resultSet.getString("DATE"), DateTimeFormatter.ISO_DATE);
76+
LocalTime time = LocalTime.parse(resultSet.getString("TIME"), DateTimeFormatter.ISO_TIME);
77+
78+
System.out.println(date + " " + time);
79+
return new Reservation(
80+
resultSet.getInt("ID"),
81+
resultSet.getString("NAME"),
82+
date, time
83+
);
84+
}
85+
86+
87+
} catch (final SQLException e) {
88+
throw new RuntimeException(e);
89+
}
90+
91+
return null;
92+
}
93+
94+
public void deleteReservation(final int id) {
95+
final var query = "DELETE FROM RESERVATION WHERE ID = ?";
96+
try (
97+
final var connection = getConnection();
98+
final var preparedStatement = connection.prepareStatement(query)
99+
) {
100+
preparedStatement.setLong(1, id);
101+
preparedStatement.executeUpdate();
102+
} catch (final SQLException e) {
103+
throw new RuntimeException(e);
104+
}
105+
}
106+
107+
public void resetTable() {
108+
try (var connection = getConnection()) {
109+
var stmt = connection.createStatement();
110+
stmt.execute("DROP TABLE IF EXISTS RESERVATION");
111+
stmt.execute("CREATE TABLE RESERVATION (" +
112+
"ID BIGINT PRIMARY KEY, " +
113+
"NAME VARCHAR(50), " +
114+
"DATE VARCHAR(20), " +
115+
"TIME VARCHAR(20))");
116+
} catch (SQLException e) {
117+
throw new RuntimeException(e);
118+
}
119+
}
120+
121+
}

src/test/java/roomescape/DAOTest.java

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package roomescape;
2+
3+
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
4+
5+
import java.sql.SQLException;
6+
import java.time.LocalDate;
7+
import java.time.LocalTime;
8+
import org.junit.jupiter.api.BeforeEach;
9+
import org.junit.jupiter.api.Test;
10+
import roomescape.dao.ReservationDAO;
11+
import roomescape.domain.Reservation;
12+
13+
public class DAOTest {
14+
15+
private final ReservationDAO reservationDAO = new ReservationDAO();
16+
17+
@BeforeEach
18+
void setUp() {
19+
reservationDAO.resetTable();
20+
}
21+
22+
@Test
23+
public void connection() {
24+
try (final var connection = reservationDAO.getConnection()) {
25+
assertThat(connection).isNotNull();
26+
} catch (SQLException e) {
27+
throw new RuntimeException(e);
28+
}
29+
}
30+
31+
@Test
32+
void addReservation() {
33+
final var reservation = new Reservation(1, "전서희", LocalDate.parse("2026-05-12"), LocalTime.parse("19:00"));
34+
reservationDAO.addReservation(reservation);
35+
}
36+
37+
@Test
38+
void findReservation() {
39+
final var reservation = new Reservation(1, "전서희", LocalDate.parse("2026-05-12"), LocalTime.parse("19:00"));
40+
reservationDAO.addReservation(reservation);
41+
final var reservation2 = reservationDAO.findReservation(reservation.getId());
42+
assertThat(reservation2).isEqualTo(new Reservation(1, "전서희", LocalDate.parse("2026-05-12"), LocalTime.parse("19:00")));
43+
}
44+
45+
@Test
46+
void deleteReservation() {
47+
final var reservation = new Reservation(1, "전서희", LocalDate.parse("2026-05-12"), LocalTime.parse("19:00"));
48+
reservationDAO.addReservation(reservation);
49+
reservationDAO.deleteReservation(reservation.getId());
50+
final var result = reservationDAO.findReservation(reservation.getId());
51+
assertThat(result).isNull();
52+
}
53+
54+
}

0 commit comments

Comments
 (0)