반응형
이번 강의에서는 DB 연동의 기초 구조를 구성하는 두 개의 핵심 클래스를 만들어 봅니다. 게시판 데이터를 담을 수 있는 BoardVO 클래스와, 데이터베이스와 연결을 위한 도우미 클래스 JDBCUtil을 직접 구현해보는 시간입니다.
📦 BoardVO.java
package com.springbook.biz.board;
import java.sql.Date;
public class BoardVO {
private int seq;
private String title;
private String writer;
private String content;
private Date regDate;
private int cnt;
public int getSeq() {
return seq;
}
public void setSeq(int seq) {
this.seq = seq;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getWriter() {
return writer;
}
public void setWriter(String writer) {
this.writer = writer;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public Date getRegDate() {
return regDate;
}
public void setRegDate(Date regDate) {
this.regDate = regDate;
}
public int getCnt() {
return cnt;
}
public void setCnt(int cnt) {
this.cnt = cnt;
}
}
📦 JDBCUtil.java
package com.springbook.biz.board;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class JDBCUtil {
public static Connection getConnection() {
try {
Class.forName("org.h2.Driver");
return DriverManager.getConnection("jdbc:h2:~/test", "sa", "");
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static void close(PreparedStatement stmt, Connection conn) {
if (stmt != null)
try {
if (!stmt.isClosed())
stmt.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
stmt = null;
}
if (conn != null)
try {
if (!conn.isClosed())
conn.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
conn = null;
}
}
public static void close(ResultSet rs, PreparedStatement stmt, Connection conn) {
if (rs != null)
try {
if (!rs.isClosed())
rs.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
rs = null;
}
if (stmt != null)
try {
if (!stmt.isClosed())
stmt.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
stmt = null;
}
if (conn != null)
try {
if (!conn.isClosed())
conn.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
conn = null;
}
}
}
💬 코드 설명
✅ BoardVO
- 게시판 데이터를 담기 위한 VO (Value Object) 클래스입니다.
- 필드 구성:
- seq : 글 번호
- title : 제목
- writer : 작성자
- content : 내용
- regDate : 등록일 (java.sql.Date)
- cnt : 조회수
- 각 필드는 getter/setter를 통해 접근합니다.
✅ JDBCUtil
- DB 연결과 자원 해제를 편리하게 도와주는 유틸리티 클래스입니다.
- 주요 메서드:
- getConnection() : H2 데이터베이스에 연결
- close() : PreparedStatement, ResultSet, Connection 객체를 안전하게 닫음
- 반복적인 JDBC 자원 해제 코드를 간단하게 처리해줍니다.
💡 포인트 정리
- BoardVO는 DB와 연동할 때 하나의 게시글 정보를 담는 데이터 전송 객체 역할을 합니다.
- JDBCUtil은 코드 재사용성과 안정성을 높이는 DB 헬퍼 클래스입니다.
- 이 구조는 다음 단계인 DAO(Data Access Object) 구성에 기반이 됩니다.
📌정리하자면, 이번 강의에서는 데이터를 담을 VO 클래스(BoardVO)와 DB 연결/해제를 담당하는 JDBCUtil 클래스를 직접 구현해 보았습니다. 이 두 클래스는 앞으로 DB 연동 기능(Insert, Select 등)을 작성할 때 필수적으로 사용되며, DAO 설계의 핵심 구성 요소입니다.
'Spring > 실습 정리' 카테고리의 다른 글
Spring 7강 - UserDAO & UserService 구성 (1) | 2025.06.18 |
---|---|
Spring 6강 - BoardService 계층 구성과 실행 테스트 (0) | 2025.06.18 |
Spring 5강 - BoardDAO 클래스와 CRUD 기능 구현 (0) | 2025.06.18 |
Spring 3강 - 어노테이션 기반 의존성 주입 (0) | 2025.06.17 |
Spring 2강 - 생성자 주입과 다형성 객체 활용 (1) | 2025.06.16 |
Spring 1강 - DI(의존성 주입) 기초 예제 (1) | 2025.06.12 |