String title = "TEST2";
String writerId = "newlec";
String content = "hahaha";
String files = "";
String sql = "INSERT INTO notice (" + " id," + " title," + " writer_id," + " content," + " files"
+ ") VALUES (notice_id_seq.nextval,?,?,?,?)";
Class.forName(driver);
Connection con = DriverManager.getConnection(url, user, password);
//Statement st = con.createStatement();
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();
System.out.println(result);
String sql = "UPDATE NOTICE"
+ " SET"
+ " TITLE=?,"
+ " CONTENT=?,"
+ " FILES=?"
+ "WHERE ID=?";
Class.forName(driver);
Connection con = DriverManager.getConnection(url, user, password);
//Statement st = con.createStatement();
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();
System.out.println(result);
st.close();
con.close();
String sql = "DELETE NOTICE WHERE ID=?";
Class.forName(driver);
Connection con = DriverManager.getConnection(url, user, password);
//Statement st = con.createStatement();
PreparedStatement st = con.prepareStatement(sql);
st.setInt(1, id);
?. CRUD란?
<Notice.java>
package com.newlecture.app.entity;
import java.sql.Date;
public class Notice {
private int id;
private String title;
private String writedId;
private Date regDate;
private String content;
private int hit;
public Notice() {
}
public Notice(int id, String title, String writedId, Date regDate, String content, int hit) {
this.id = id;
this.title = title;
this.writedId = writedId;
this.regDate = regDate;
this.content = content;
this.hit = hit;
}
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 getWritedId() {
return writedId;
}
public void setWritedId(String writedId) {
this.writedId = writedId;
}
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;
}
}
<getList()>
public List<Notice> getList() throws ClassNotFoundException, SQLException{
String driver = "oracle.jdbc.driver.OracleDriver";
String url = "jdbc:oracle:thin:@localhost:1521/xepdb1";
String user ="NEWLEC";
String password = "**********"; // 비밀번호 가림
String sql = "SELECT * FROM NOTICE";
Class.forName(driver);
Connection con = DriverManager.getConnection(url, user, password);
Statement st = con.createStatement();
ResultSet rs = st.executeQuery(sql);
List<Notice> list = new ArrayList<Notice>();
while(rs.next()) {
int hit = rs.getInt("HIT");
/*
if(hit<10)
continue;*/
int id=rs.getInt("ID");
String title = rs.getString("TITLE");
String writedId = rs.getString("WRITER_ID");
String content = rs.getString("CONTENT");
Date regDate = rs.getDate("REGDATE");
Notice notice = new Notice(
id,
title,
writedId,
regDate,
content,
hit
);
list.add(notice);
}
rs.close();
st.close();
con.close();
return list;
}
SELECT * FROM
(SELECT ROWNUM NUM, N.* FROM
(SELECT * FROM NOTICE ORDER BY REGDATE DESC) N)
WHERE NUM BETWEEN 2 AND 3;
서브쿼리가 2개가 필요하다.
하지만 위의 SQL쿼리를 코드로 입력하기엔 좀 길다고 느껴질 수도 있다.
여기서 사용할 수 있는것이 바로 VIEW이다.
위의 쿼리를 뷰로 생성하고 이를 조회하게하면 코드가 굉장히 간단하게된다.
CREATE VIEW NOTICE_VIEW
AS
SELECT * FROM
(SELECT ROWNUM NUM, N.* FROM
(SELECT * FROM NOTICE ORDER BY REGDATE DESC) N);