데이터를 가져와서 어떻게 쓰는지 전체적으로 돌아가는 구조를 보기 위함
MainApplication 클래스
import java.util.*;
import database.DAO.BoardDAO;
import database.Entity.BoardEntity;
public class MainApplication {
public static void main(String[] args) {
// DAO 인스턴스 생성
BoardDAO dao = new BoardDAO();
// DB에서 Board 테이블의 전체 레코드를 검색해서 출력
List<BoardEntity> findResult = dao.find();
for(BoardEntity entity : findResult) System.out.println(entity.toString());
}
}
BoardDAO 클래스
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.*;
import database.DataBaseConnector;
import database.Entity.BoardEntity;
import dto.InsertBoardDTO;
// DAO : Data Access Object
// DB에 접근해서 데이터 검색 및 삽입 등의 DB 작업을 담당하는 클래스
public class BoardDAO {
// DB의 Board 테이블에서 모든 컬럼을 선택해서 반환하는 메서드
// SQL : SELECT * FROM Board;
// 예상되는 반환 결과는 0개 이상의 복수의 레코드
public List<BoardEntity> find(){
List<BoardEntity> result = new ArrayList<BoardEntity>();
final String SQL = "SELECT * FROM Board";
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
try {
// DB 접근은 try catch 안에
connection = DataBaseConnector.createConnection();
statement = connection.createStatement();
resultSet = statement.executeQuery(SQL);
while(resultSet.next()) {
Integer id = resultSet.getInt(1);
String boardTitle = resultSet.getString(2);
String boardContent = resultSet.getString(3);
String boardDateTime = resultSet.getString(4);
Integer boardLike = resultSet.getInt(5);
Integer boardWriter = resultSet.getInt(6);
BoardEntity entity = new BoardEntity(id, boardTitle, boardContent, boardDateTime, boardLike, boardWriter);
result.add(entity);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if(connection != null && !connection.isClosed()) {
connection.close();
}
if(statement != null && !statement.isClosed())
statement.close();
if(resultSet != null && !resultSet.isClosed())
resultSet.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
return result;
}
}
BoardEntity 클래스
// Entity : DB의 테이블과 서로 매핑되는 JAVA CLASS
// 목적 : DB 테이블의 레코드를 각각의 레코드를 인스턴스로 받기 위한 용도
// 조건 : DB 테이블의 각 컬럼명, 데이터 타입과 Entity 클래스의 필드명, 데이터 타입이 일치해야함
public class BoardEntity {
private Integer id;
private String boardTitle;
private String boardContent;
private String boardDateTime;
private Integer boardLike;
private Integer boardWriter;
public BoardEntity(Integer id, String boardTitle, String boardContent, String boardDateTime, Integer boardLike,
Integer boardWriter) {
this.id = id;
this.boardTitle = boardTitle;
this.boardContent = boardContent;
this.boardDateTime = boardDateTime;
this.boardLike = boardLike;
this.boardWriter = boardWriter;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getBoardTitle() {
return boardTitle;
}
public void setBoardTitle(String boardTitle) {
this.boardTitle = boardTitle;
}
public String getBoardContent() {
return boardContent;
}
public void setBoardContent(String boardContent) {
this.boardContent = boardContent;
}
public String getBoardDateTime() {
return boardDateTime;
}
public void setBoardDateTime(String boardDateTime) {
this.boardDateTime = boardDateTime;
}
public Integer getBoardLike() {
return boardLike;
}
public void setBoardLike(Integer boardLike) {
this.boardLike = boardLike;
}
public Integer getBoardWriter() {
return boardWriter;
}
public void setBoardWriter(Integer boardWriter) {
this.boardWriter = boardWriter;
}
@Override
public String toString() {
return "BoardEntity [id=" + id + ", boardTitle=" + boardTitle + ", boardContent=" + boardContent
+ ", boardDateTime=" + boardDateTime + ", boardLike=" + boardLike + ", boardWriter=" + boardWriter
+ "]";
}
}
InsertBoardDTO 클래스
public class InsertBoardDTO {
private String boardTitle;
private String boardContent;
private Integer boardWriter;
public InsertBoardDTO(String boardTitle, String boardContent, Integer boardWriter) {
super();
this.boardTitle = boardTitle;
this.boardContent = boardContent;
this.boardWriter = boardWriter;
}
public String getBoardTitle() {
return boardTitle;
}
public void setBoardTitle(String boardTitle) {
this.boardTitle = boardTitle;
}
public String getBoardContent() {
return boardContent;
}
public void setBoardContent(String boardContent) {
this.boardContent = boardContent;
}
public Integer getBoardWriter() {
return boardWriter;
}
public void setBoardWriter(Integer boardWriter) {
this.boardWriter = boardWriter;
}
@Override
public String toString() {
return "InsertBoardDTO [boardTitle=" + boardTitle + ", boardContent=" + boardContent + ", boardWriter="
+ boardWriter + "]";
}
}
DataBaseConnector 클래스
import java.sql.Connection;
import java.sql.DriverManager;
public class DataBaseConnector {
private static final String DATABASE_URL = "jdbc:mysql://127.0.0.1:3306/peed";
private static final String USER_NAME = "root";
private static final String USER_PASSWORD = "root";
public static Connection createConnection() {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
return DriverManager.getConnection(DATABASE_URL, USER_NAME, USER_PASSWORD);
} catch (Exception e) {
return null;
}
}
}
'Java' 카테고리의 다른 글
HashSet (1) | 2023.02.17 |
---|---|
isEmpty(), isBlank() (0) | 2023.01.29 |
이클립스, MySQL 파일 연동 (0) | 2023.01.26 |
삼항 연산자 (0) | 2023.01.17 |
제네릭 ( Generic ) (0) | 2023.01.13 |
댓글