[이미지 게시판의 vo, dao, service 만들기]
① 이미지 보드 VO, DAO, SERVICE 클래스 생성
● vo 생성
package imgboard;
import java.sql.Date;
import java.util.ArrayList;
import img_reps.ImgrepVo;
public class ImgVo {
private int num;
private String writer;
private Date w_date;
private String title;
private String content;
private String path;
private ArrayList<ImgrepVo> reps; // 전체이미지 게시판을 보여줄때 댓글들을 리스트에 담아서 함께 보여줄 예정이다.
public ImgVo() {
}
public ImgVo(int num, String writer, Date w_date, String title, String content, String path) {
super();
this.num = num;
this.writer = writer;
this.w_date = w_date;
this.title = title;
this.content = content;
this.path = path;
}
public ArrayList<ImgrepVo> getReps() {
return reps;
}
public void setReps(ArrayList<ImgrepVo> reps) {
this.reps = reps;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public String getWriter() {
return writer;
}
public void setWriter(String writer) {
this.writer = writer;
}
public Date getW_date() {
return w_date;
}
public void setW_date(Date w_date) {
this.w_date = w_date;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
@Override
public String toString() {
return "ImgVo [num=" + num + ", writer=" + writer + ", w_date=" + w_date + ", title=" + title + ", content="
+ content + ", path=" + path + "]";
}
}
● dao 생성
package imgboard;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import conn.DBConnect;
public class ImgDao {
private DBConnect dbconn;
public ImgDao() {
dbconn = DBConnect.getInstance();
}
public void insert(ImgVo vo) {
Connection conn = dbconn.conn();
String sql = "insert into imgboard values (seq_imgnum.nextval,?,sysdate,?,?,?)";
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, vo.getWriter());
pstmt.setString(2, vo.getTitle());
pstmt.setString(3, vo.getContent());
pstmt.setString(4, vo.getPath());
pstmt.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void update(ImgVo vo) {
Connection conn = dbconn.conn();
String sql = "update imgboard set title=?, content=? where num =? ";
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, vo.getTitle());
pstmt.setString(2, vo.getContent());
pstmt.setInt(3, vo.getNum());
pstmt.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void delete(int num) {
Connection conn = dbconn.conn();
String sql = "delete from imgboard where num = ?";
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, num);
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public ImgVo select(int num) {
ImgVo vo = null;
Connection conn = dbconn.conn();
String sql = "select * from imgboard where num =?";
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, num);
ResultSet rs = pstmt.executeQuery();
if (rs.next()) {
return new ImgVo(rs.getInt(1), rs.getString(2), rs.getDate(3), rs.getString(4), rs.getString(5),
rs.getString(6));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
public ArrayList<ImgVo> selectAll() {
ArrayList<ImgVo> list = new ArrayList<ImgVo>();
Connection conn = dbconn.conn();
String sql = "select * from imgboard";
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
list.add(new ImgVo(rs.getInt(1), rs.getString(2), rs.getDate(3), rs.getString(4), rs.getString(5),
rs.getString(6)));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return list;
}
}
● service 생성
package imgboard;
import java.util.ArrayList;
public class ImgService {
private ImgDao dao;
public ImgService() {
dao = new ImgDao();
}
//추가
public void addImg(ImgVo vo) {
dao.insert(vo);
}
//수정
public void editImg(ImgVo vo) {
dao.update(vo);
}
//삭제
public void delImg (int num) {
dao.delete(num);
}
//숫자로 찾기
public ImgVo getImg(int num) {
return dao.select(num);
}
//전체검색
public ArrayList<ImgVo> getAll(){
return dao.selectAll();
}
}
'비동기 요청' 카테고리의 다른 글
[비동기 요청] 비동기 요청을 이용한 이미지 게시판 만들기 ⑤ (0) | 2023.04.09 |
---|---|
[비동기 요청] 비동기 요청을 이용한 이미지 게시판 만들기 ④ (0) | 2023.04.07 |
[비동기 요청] 비동기 요청을 이용한 이미지 게시판 만들기 ③ (0) | 2023.04.06 |
[비동기 요청] 비동기 요청을 이용한 이미지 게시판 만들기 ② (0) | 2023.04.06 |