티스토리 뷰
반응형
목표
: INSERT, UPDATE, DELETE 추가
NoticeService.java
/* NoticeService.java */
package com.newlecture.app.service;
import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import com.newlecture.app.entity.Notice;
/* CRUD 서비스 목록을 반환하는 함수 */
public class NoticeService {
/* 반복되는 url을 올려서 정의 */
private String url = "jdbc:oracle:thin:@192.168.219.103:1521/XE";
/* 반복되는 id, pwd를 올려서 정의 */
private String uid = "corini";
private String pwd = "111111";
/* 반복되는 Driver를 올려서 정의 */
private String driver = "oracle.jdbc.driver.OracleDriver";
/*List-> java.util import */
/*List<Notice>라는 목록으로 반환 */
public List<Notice> getList() throws ClassNotFoundException, SQLException{ //getList()함수를 가지고 반환을 하겠다.
//List<>라는 컬렉션에 담아서 준다.
String sql = "SELECT * FROM NOTICE WHERE hit >= 10";
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>();
/* Notice를 담을 수있는 그릇이 필요 -> 클래스 추가 entity 이름 Notice.java 추가(그릇, 서비스x)*/
while(rs.next()) {
int id = rs.getInt("ID");
String title = rs.getString("TITLE");
String writerId = rs.getString("WRITER_ID");
Date regDate = rs.getDate("REGDATE");
String content = rs.getString("CONTENT");
int hit = rs.getInt("hit");
/*files 추가 */
String files = rs.getString("FILES");
/*Notice라는 객체를 통해서 notice를 하나 만들면서 초기화하는 생성*/
Notice notice = new Notice(
/*Notice.java 생성자와 담을때 딱 맞게 객체를 만들어야 한다. */
id,
title,
writerId,
regDate,
content,
hit,
files //files 반환값 추가
);
/*notice라는 놈을 추가해서 목록에 하나하나 추가 */
list.add(notice);
}
rs.close();
st.close();
con.close();
return list;
}
public int insert(Notice notice) throws ClassNotFoundException, SQLException {
/*title, writerId, content, files를 정의*/
/* Notice클래스 내의 함수 호출 */
String title = notice.getTitle();
String writerId =notice.getWriterId();
String content = notice.getContent();
String files = notice.getFiles(); //Notice수정해서 files 추가
/*INSERT SQL문 */
String sql = "INSERT INTO notice ("
+ " title,"
+ " writer_id,"
+ " content,"
+ " files"
+ ") VALUES (?,?,?,?)"; //PreparedStatement를 통한 전달
Class.forName(driver);
Connection con = DriverManager.getConnection(url, uid, pwd);
/*PreparedStatement도구 사용*/
PreparedStatement st = con.prepareStatement(sql);
//sql문 prepareStatement를 이용해서 st에 저장
/*preparedStatement이용, setString(인덱스값[1부터], 변수)로 저장 */
st.setString(1, title);
st.setString(2, writerId);
st.setString(3, content);
st.setString(4, files);
/*preparedStatement를 사용 할때에는 sql문을 전달하지 않는다! 이미 전달 되어있다.*/
int result = st.executeUpdate();
//excuteUpdate는 int로 실행된 row값을 result에 전달
st.close();
con.close();
return result;
}
public int upate(Notice notice) throws ClassNotFoundException, SQLException {
/*title, content, files, id를 정의*/
/* get함수로 변경 */
String title = notice.getTitle();
String content = notice.getContent();
String files = notice.getFiles();
int id = notice.getId();
/*UPDATE SQL문 */
String sql = "UPDATE notice "
+ "SET title=?,"
+ " content=?,"
+ " files=?"
+ "WHERE id=?"; //PreparedStatement를 통한 전달
Class.forName(driver);
Connection con = DriverManager.getConnection(url, uid, pwd);
/*PreparedStatement도구 사용*/
PreparedStatement st = con.prepareStatement(sql);
//sql문 prepareStatement를 이용해서 st에 저장
/*preparedStatement 저장 */
st.setString(1, title);
st.setString(2, content);
st.setString(3, files);
st.setInt(4, id); //id는 int형이기 때문에 setInt로 저장
/*preparedStatement를 사용 할때에는 sql문을 전달하지 않는다! 이미 전달 되어있다.*/
int result = st.executeUpdate();
//excuteUpdate는 int로 실행된 row값을 result에 전달
st.close();
con.close();
return 0;
}
public int delete(int id) throws ClassNotFoundException, SQLException {
/*DELETE SQL문 */
String sql = "DELETE notice WHERE id=?"; //?:PreparedStatement를 통한 전달
Class.forName(driver);
Connection con = DriverManager.getConnection(url, uid, pwd);
/*PreparedStatement도구 사용*/
PreparedStatement st = con.prepareStatement(sql);
//sql문 prepareStatement를 이용해서 st에 저장
/*preparedStatement 저장 */
st.setInt(1, id); //id는 int형이기 때문에 setInt로 저장
/*preparedStatement를 사용 할때에는 sql문을 전달하지 않는다! 이미 전달 되어있다.*/
int result = st.executeUpdate();
//excuteUpdate는 int로 실행된 row값을 result에 전달
st.close();
con.close();
return result;
}
}
Notice.java
/* Notice.java*/
package com.newlecture.app.entity;
import java.sql.Date;
public class Notice {
/* 값을 담을 수 있는 그룹화된 그릇*/
/* 기본적인 속성만 정의 */
/* 구조가 노출 되지 않도록 private 보호모드 추가*/
private int id;
private String title;
private String writerId;
private Date regDate;
private String content;
private int hit;
private String files;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
/*여러가지 값으로 초기화를 하면서 값을 채워야 하기 때문에 생성자를 만든다 Crtl+Space*/
/* 매개변수 */
public Notice() {
// TODO Auto-generated constructor stub
}
/* 필드를 가지고 있는 생성자도 필요 source */
/* files가 빠졌기 때문에 지우고 Notice 다시 추가 */
public Notice(int id, String title, String writerId, Date regDate, String content, int hit, String files) {
this.id = id;
this.title = title;
this.writerId = writerId;
this.regDate = regDate;
this.content = content;
this.hit = hit;
this.files = files;
}
/*값들을 셋팅 할수있도록 getter setter 생성*/
public void setTitle(String title) {
this.title = title;
}
public String getWriterId() {
return writerId;
}
public void setWriterId(String writerId) {
this.writerId = writerId;
}
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;
}
/* files getters and setters 추가 */
public String getFiles() {
return files;
}
public void setFiles(String files) {
this.files = files;
}
}
반응형
댓글
공지사항