Notice.java
package webprj.entity;
import java.util.Date;
public class Notice {
private int id;
private String title;
private String writerId;
private Date regDate;
private String content;
private int hit;
private String files;
public Notice() {
}
public Notice(int id, String title, String writerId, Date regDate, String content, int hit, String files) {
super();
this.id = id;
this.title = title;
this.writerId = writerId;
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 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;
}
public String getFiles() {
return files;
}
public void setFiles(String files) {
this.files = files;
}
}
noticeService.java
package webprj.service;
import java.sql.Connection;
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.Date;
import java.util.List;
import webprj.entity.Notice;
public class NoticeService {
private String url = "jdbc:oracle:thin:@192.168.219.108:1521/xe";
private String uid = "codren";
private String pwd = "1234";
private String driver = "oracle.jdbc.driver.OracleDriver";
public List<Notice> getList(int page, String field, String query) throws ClassNotFoundException, SQLException{
int start = 1 + (page-1)*10; // 1, 11, 21, 31, ..
int end = 10*page; // 10, 20, 30, 40...
String sql = "SELECT * FROM NOTICE WHERE "+field+" LIKE ? AND ID BETWEEN ? AND ?";
Class.forName(driver);
Connection con = DriverManager.getConnection(url,uid, pwd);
PreparedStatement st = con.prepareStatement(sql);
st.setString(1, "%"+query+"%");
st.setInt(2, start);
st.setInt(3, end);
ResultSet rs = st.executeQuery();
List<Notice> list = new ArrayList<Notice>();
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");
String files = rs.getString("FILES");
Notice notice = new Notice(
id,
title,
writerId,
regDate,
content,
hit,
files
);
list.add(notice);
}
rs.close();
st.close();
con.close();
return list;
}
// Scalar
public int getCount() throws ClassNotFoundException, SQLException {
int count = 0;
String sql = "SELECT COUNT(ID) COUNT FROM NOTICE";
Class.forName(driver);
Connection con = DriverManager.getConnection(url,uid, pwd);
Statement st = con.createStatement();
ResultSet rs = st.executeQuery(sql);
if(rs.next())
count = rs.getInt("COUNT");
rs.close();
st.close();
con.close();
return count;
}
public int insert(Notice notice) throws SQLException, ClassNotFoundException {
String title = notice.getTitle();
String writerId = notice.getWriterId();
String content = notice.getContent();
String files = notice.getFiles();
String url = "jdbc:oracle:thin:@192.168.219.108:1521/xe";
String sql = "INSERT INTO notice ( " +
" title," +
" writer_id," +
" content," +
" files" +
") VALUES (?,?,?,?)";
Class.forName(driver);
Connection con = DriverManager.getConnection(url,uid, pwd);
//Statement st = con.createStatement();
//st.ex....(sql)
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 SQLException, ClassNotFoundException {
String title = notice.getTitle();
String content = notice.getContent();
String files = notice.getFiles();
int id = notice.getId();
String url = "jdbc:oracle:thin:@192.168.219.108:1521/xe";
String sql = "UPDATE NOTICE " +
"SET" +
" TITLE=?," +
" CONTENT=?," +
" FILES=?" +
"WHERE ID=?";
Class.forName(driver);
Connection con = DriverManager.getConnection(url,uid, pwd);
//Statement st = con.createStatement();
//st.ex....(sql)
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:@192.168.219.108:1521/xe";
String sql = "DELETE NOTICE WHERE ID=?";
Class.forName(driver);
Connection con = DriverManager.getConnection(url,uid, pwd);
//Statement st = con.createStatement();
//st.ex....(sql)
PreparedStatement st = con.prepareStatement(sql);
st.setInt(1, id);
int result = st.executeUpdate();
st.close();
con.close();
return result;
}
}