1. 댓글 테이블 및 로우데이터 생성
2. 댓글 관련 CRUD 기능 구현
1) ReplyDao.register() : 댓글 작성 기능
- ReplyDao : register()
public void register(Reply reply) {
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_REPLY (RNO, CONTENT, BNO, WRITER)"
+ " VALUES (SEQ_REPLY.NEXTVAL, ?, ?, ?)";
PreparedStatement pstmt = conn.prepareStatement(sql);
// 파라미터 바인딩
pstmt.setString(1, reply.content);
pstmt.setLong(2, reply.bno);
pstmt.setString(3, reply.writer);
// 문장 실행(반영)
pstmt.executeUpdate();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
- ReplyService : register()
public void register() {
System.out.println("댓글 작성");
Scanner scanner = new Scanner(System.in);
System.out.println("글번호 > ");
Long bno = Long.parseLong(scanner.nextLine());
System.out.println("내용 > ");
String content = scanner.nextLine();
System.out.println("작성자 > ");
String writer = scanner.nextLine();
Reply reply = new Reply(null, content, null, bno, writer);
new ReplyDao().register(reply);
}
2) ReplyDao.remove() : 댓글 삭제 기능
- ReplyDao : remove()
public void remove(Long rno) {
try {
//클래스 로드
Class.forName("oracle.jdbc.driver.OracleDriver");
// connection 취득
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521/xe",
"odoung", "1234");
// 문장 생성
String sql = "DELETE TBL_REPLY\r\n" +
"WHERE RNO = ?";
PreparedStatement pstmt = conn.prepareStatement(sql);
// 파라미터 바인딩
pstmt.setLong(1, rno);
// 문장 실행(반영)
pstmt.executeUpdate();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
- ReplyService : remove()
public void remove() {
System.out.println("댓글 삭제");
Scanner scanner = new Scanner(System.in);
System.out.println("댓글번호 > ");
Long rno = Long.parseLong(scanner.nextLine());
new ReplyDao().remove(rno);
}
3) ReplyDao.list() : 댓글 목록 조회 기능
- ReplyDao : list()
public List<Reply> list(Long bno) {
List<Reply> list = new ArrayList<Reply>();
try {
//클래스 로드
Class.forName("oracle.jdbc.driver.OracleDriver");
// connection 취득
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521/xe",
"odoung", "1234");
// 문장 생성
String sql = "SELECT * FROM TBL_REPLY WHERE BNO = ?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setLong(1, bno);
// 결과집합 생성
ResultSet rs = pstmt.executeQuery();
// 결과집합 순회 후 데이터 바인딩
while(rs.next()) {
int idx = 1;
Reply reply = new Reply(rs.getLong(idx++), rs.getString(idx++),
rs.getString(idx++), rs.getLong(idx++), rs.getString(idx++));
list.add(reply);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
- ReplyService : list()
public void list() {
System.out.println("댓글 목록 조회");
Scanner scanner = new Scanner(System.in);
System.out.println("글번호 > ");
Long bno = Long.parseLong(scanner.nextLine());
new ReplyDao().list(bno).forEach(System.out::println);
}
- domain.Reply : toString 적용
package domain;
public class Reply {
public Long rno;
public String content;
public String regDate;
public Long bno;
public String writer;
public Reply() {
// TODO Auto-generated constructor stub
}
public Reply(Long rno, String content, String regDate, Long bno, String writer) {
super();
this.rno = rno;
this.content = content;
this.regDate = regDate;
this.bno = bno;
this.writer = writer;
}
@Override
public String toString() {
return String.format("Reply [rno=%s, content=%s, regDate=%s, bno=%s, writer=%s]", rno, content, regDate, bno,
writer);
}
}
4) 댓글 기능 설계
public static void main(String[] args) {
ReplyService replyService = new ReplyService();
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("1.댓글 목록조회 2. 댓글등록 3.댓글삭제");
int input = Integer.parseInt(scanner.nextLine());
switch (input) {
case 1:
replyService.list();
break;
case 2:
replyService.register();
break;
case 3:
replyService.remove();
break;
default:
break;
}
}
}