Java - NoticeService 생성
데이터 select insert, update, delete 네개를 줄여서 CRUD 라고 많이쓴다.
4개 작업을 합쳐서 만들어보자 !
Notice
public class Notice {
private int id ;
private String title ;
private String wirterid ;
private Date regDate;
private String content ;
private int hit ;
private String files;
public Notice() {
}
public Notice(int id, String title, String wirterid, Date regDate, String content, int hit, String files) {
this.id = id;
this.title = title;
this.wirterid = wirterid;
this.regDate = regDate;
this.content = content;
this.hit = hit;
this.files = files;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getWirterid() {
return wirterid;
}
public void setWirterid(String wirterid) {
this.wirterid = wirterid;
}
public Date getRegDate() {
return regDate;
}
public void setRegDate(Date regDate) {
this.regDate = regDate;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public int getHit() {
return hit;
}
public void setHit(int hit) {
this.hit = hit;
}
public String getFiles() {
return files;
}
public void setFiles(String files) {
this.files = files;
}
}
그릇을 만드는것. 값을 담을수있는 좀더 그룹화된 자료형이다.
SELECT
public List<Notice> getList() throws ClassNotFoundException, SQLException {
String url = "jdbc:oracle:thin:@localhost:1521/xe";
String sql = "SELECT * FROM NOTICE";
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection(url,"NEWLEC","0000");
Statement st = con.createStatement();
ResultSet rs = st.executeQuery(sql);
List<Notice> list = new ArrayList<Notice>();
while(rs.next()) {
int id = rs.getInt("ID");
String title = rs.getString("title");
String wirterid = rs.getString("WRITER_ID");
Date regDate=rs.getDate("REGDATE");
String content = rs.getString("CONTENT");
int hit =rs.getInt("HIT");
Notice notice = new Notice(
id,
title,
wirterid,
regDate,
content,
hit
);
list.add(notice);
}
rs.close();
st.close();
con.close();
return list;
}
}
SELECT부분이다.
NoticeService
public class NoticeService {
private String url = "jdbc:oracle:thin:@localhost:1521/xe";
private String uid= "lec";
private String pwd = "0000";
private String driver = "oracle.jdbc.driver.OracleDriver";
public List<Notice> getList() throws ClassNotFoundException, SQLException {
String sql = "SELECT * FROM NOTICE";
Class.forName(driver);
Connection con = DriverManager.getConnection(url,uid,pwd);
Statement st = con.createStatement();
ResultSet rs = st.executeQuery(sql);
List<Notice> list = new ArrayList<Notice>();
while(rs.next()) {
int id = rs.getInt("ID");
String title = rs.getString("title");
String wirterid = rs.getString("WRITER_ID");
Date regDate=rs.getDate("REGDATE");
String content = rs.getString("CONTENT");
int hit =rs.getInt("HIT");
String files = rs.getString("FILES");
Notice notice = new Notice(
id,
title,
wirterid,
regDate,
content,
hit,
files
);
list.add(notice);
}
rs.close();
st.close();
con.close();
return list;
}
public int insert(Notice notice) throws ClassNotFoundException, SQLException {
String title = notice.getTitle();
String writerId =notice.getWirterid();
String content =notice.getContent();
String files = notice.getFiles();
String url = "jdbc:oracle:thin:@localhost:1521/xe";
String sql = "INSERT INTO NOTICE(TITLE,WRITER_ID, CONTENT, FILES) VALUES ( ?,?,?,?)";
Class.forName(driver);
Connection con = DriverManager.getConnection(url,uid,pwd);
PreparedStatement st = con.prepareStatement(sql);
st.setString(1, title);
st.setString(2, writerId);
st.setString(3, content);
st.setString(4, files);
int result = st.executeUpdate();
st.close();
con.close();
return result;
}
public int update(Notice notice) throws ClassNotFoundException, SQLException {
String title = notice.getTitle();
String content =notice.getContent();
String files =notice.getFiles();
int id = notice.getId();
String url = "jdbc:oracle:thin:@localhost:1521/xe";
String sql = "" +
" UPDATE NOTICE " +
"SET" +
" TITLE = ?," +
" CONTENT =?," +
" FILES=?" +
"WHERE ID = ?";
Class.forName(driver);
Connection con = DriverManager.getConnection(url,uid,pwd);
PreparedStatement st = con.prepareStatement(sql);
st.setString(1, title);
st.setString(2, content);
st.setString(3, files);
st.setInt(4, id);
int result = st.executeUpdate();
st.close();
con.close();
return result;
}
public int delete(int id) throws ClassNotFoundException, SQLException {
String url = "jdbc:oracle:thin:@localhost:1521/xe";
String sql = "DELETE NOTICE WHERE ID = ?";
Class.forName(driver);
Connection con = DriverManager.getConnection(url,uid,pwd);
PreparedStatement st = con.prepareStatement(sql);
st.setInt(1, id);
int result = st.executeUpdate();
st.close();
con.close();
return result;
}
}
위에다 private 해서 하나로 묶었는데 수업들을때도 적용해볼예정이다.
```java
댓글남기기