1. BoardDao.list() : 게시글 조회 기능
- BoardDao : list()
public List<Board> list() {
List<Board> list = new ArrayList<Board>();
try {
//클래스 로드
Class.forName("oracle.jdbc.driver.OracleDriver");
// connection 취득
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521/xe",
"odoung", "1234");
// 문장 생성
String sql = "SELECT BNO, TITLE, HITCOUNT,\r\n" +
" CASE\r\n" +
" WHEN SYSDATE - REGDATE > 1 THEN TO_CHAR(REGDATE, 'YY/MM/DD')\r\n" +
" ELSE TO_CHAR(REGDATE, 'HH24:MI:SS')\r\n" +
" END REGDATE,\r\n" +
" WRITER\r\n" +
"FROM TBL_BOARD ORDER BY 1 DESC";
PreparedStatement pstmt = conn.prepareStatement(sql);
// 결과집합 생성
ResultSet rs = pstmt.executeQuery();
// 결과집합 순회 후 데이터 바인딩
while(rs.next()) {
Board board = new Board(rs.getLong(1), rs.getString(2), null,
rs.getInt(3), rs.getString(4), rs.getString(5));
list.add(board);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
- BoardService : list()
public void list() {
System.out.println("목록 조회");
List<Board> boards = new BoardDao().list();
for(Board board : boards) {
System.out.print(board.bno + " ");
System.out.print(board.title + " ");
System.out.print(board.hitCount + " ");
System.out.print(board.regDate + " ");
System.out.println(board.writer + " ");
}
}
2. BoardDao.register() : 게시글 등록 기능
- BoardDao : register()
public void register(Board board) {
try {
//클래스 로드
Class.forName("oracle.jdbc.driver.OracleDriver");
// connection 취득
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521/xe",
"odoung", "1234");
// 문장 생성
String sql = "INSERT INTO TBL_BOARD(BNO, TITLE, CONTENT, WRITER) "
+ "VALUES (SEQ_BOARD.NEXTVAL, ?, ?, ?)";
PreparedStatement pstmt = conn.prepareStatement(sql);
// 파라미터 바인딩
pstmt.setString(1, board.title);
pstmt.setString(2, board.content);
pstmt.setString(3, board.writer);
// 문장 실행(반영)
pstmt.executeUpdate();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
- BoardService : register()
public void register() {
System.out.println("게시글 등록");
Scanner scanner = new Scanner(System.in);
System.out.println("제목 > ");
String title = scanner.nextLine();
System.out.println("내용 > ");
String content = scanner.nextLine();
System.out.println("작성자 > ");
String writer = scanner.nextLine();
Board board = new Board(null, title, content, writer);
new BoardDao().register(board);
}