package gb;
import java.sql.*;
import java.util.*;
import javax.sql.*;
// DAO : Data Access Object (DB에 접속해서 데이터를 핸들링하는 객체)
public class GbDao {
private DataSource ds;
public GbDao(DataSource ds) {
this.ds = ds;
}
public List<GbDto> getList() throws SQLException{
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
ArrayList<GbDto> list = new ArrayList<>();
try {
conn = ds.getConnection();
stmt = conn.createStatement();
// select * from guestbook order by wdate desc
rs = stmt.executeQuery("select * from guestbook order by wdate desc");
while(rs.next()) {
GbDto dto = new GbDto(
rs.getString("NO"),
rs.getString("CONTENT"),
rs.getString("WRITER"),
rs.getString("WDATE"),
rs.getString("req_ip")
);
list.add(dto);
}
} finally {
if(rs!=null) try{rs.close();}catch(Exception e){e.printStackTrace();}
if(stmt!=null) try{stmt.close();}catch(Exception e){e.printStackTrace();}
if(conn!=null) try{conn.close();}catch(Exception e){e.printStackTrace();}
}
return list;
}
// insert into guestbook(no,content,writer)
// values(seq_gb.nextval,?,?) 작성
public void write(GbDto dto) {
Connection conn = null;
PreparedStatement pstmt = null;
StringBuilder sql = new StringBuilder();
sql.append("insert into guestbook(no,content,writer)");
sql.append(" values(seq_gb.nextval,?,?)");
try {
conn = ds.getConnection();
pstmt = conn.prepareStatement(sql.toString());
pstmt.setString(1, dto.getContent());
pstmt.setString(2, dto.getWriter());
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
if(pstmt!=null) try{pstmt.close();}catch(Exception e){e.printStackTrace();}
if(conn!=null) try{conn.close();}catch(Exception e){e.printStackTrace();}
}
}
}
package gb;
// DTO : Data Transfer Object (데이터를 가지고 이동할 때 쓰이는 객체)
public class GbDto {
private String no;
private String content;
private String writer;
private String wdate;
private String reqIP;
public GbDto() {
}
public GbDto(String no, String content, String writer, String wdate, String reqIP) {
super();
this.no = no;
this.content = content;
this.writer = writer;
this.wdate = wdate;
this.reqIP = reqIP;
}
public String getNo() {
return no;
}
public void setNo(String no) {
this.no = no;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getWriter() {
return writer;
}
public void setWriter(String writer) {
this.writer = writer;
}
public String getWdate() {
return wdate;
}
public void setWdate(String wdate) {
this.wdate = wdate;
}
public String getReqIP() {
return reqIP;
}
public void setReqIP(String reqIP) {
this.reqIP = reqIP;
}
}