|
| 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 | +} |
0 commit comments