package com.newlecture.app.service;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import com.newlecture.app.entity.Notice;
public class NoticeService {
String url ="jdbc:oracle:thin:@localhost:1521/orcl";
private String uid ="eunji";
private String pwd="1234";
public List<Notice> getList() throws ClassNotFoundException, SQLException{
String sql = "SELECT*FROM NOTICE";
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection(url,uid,pwd);
java.sql.Statement st =con.createStatement();
ResultSet rs = st.executeQuery(sql);
List<Notice> list = new ArrayList<Notice>();
// notice라는 그릇이 필요하다
while(rs.next()) {
int id = rs.getInt("ID");
String title = rs.getString("TITLE");
String writerld = rs.getString("WRITER_ID");
String content = rs.getString("CONTENT");
Date regDate=rs.getDate("REGDATE");
int hit = rs.getInt("hit");
String files = rs.getString("FILES");
Notice notice = new Notice( // 반환하는 녀석들~
id,
title,
writerld,
content,
regDate,
hit,
files
);
//notice를 add해서 추가하게되면 목록에 하나하나씩 출력하게된다
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.getWriterld();
String content = notice.getContent();
String files = notice.getFiles();
String url ="jdbc:oracle:thin:@localhost:1521/orcl";
String sql = "INSERT INTO notice ("
+ " title,"
+ " writer_id,"
+ " content,"
+ " files"
+ ") VALUES (?,?,?,?)";
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection(url,uid,pwd);
// sql.Statement st =con.createStatement();
//st.execute query,update를 실행 할때 넘겼는데
PreparedStatement st=con.prepareStatement(sql); //prepare 미리 준비해서 실행하라 statement자체가 쿼리문을 가지고 있다
st.setString(1, title);
st.setString(2, writerId);
st.setString(3, content);
st.setString(4, files);
//prepare쓸때는 sql을 쓰면 안된다
int result = st.executeUpdate();
// System.out.println(result); 출력되는 문장은 지워도 된다
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 sql = "UPDATE notice "
+ "SET" // 덧셈으 하고 나면은 결과가 notice set이 되므로 결과가
+ " title = ?,"
+ " content=?,"
+ " files = ?"
+ "WHERE id =?";
Class.forName("oracle.jdbc.driver.OracleDriver");
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);
//prepare쓸때는 sql을 쓰면 안된다
int result = st.executeUpdate();
st.close();
con.close();
return result;
}
public int delete(int id) throws SQLException, ClassNotFoundException {
// int id = 3; 인자로 받기 때문에 지워준다
String url ="jdbc:oracle:thin:@localhost:1521/orcl";
String sql = "DELETE NOTICE WHERE ID=?";
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection(url,uid,pwd);
// sql.Statement st =con.createStatement();
//st.execute query,update를 실행 할때 넘겼는데
PreparedStatement st=con.prepareStatement(sql); //prepare 미리 준비해서 실행하라 statement자체가 쿼리문을 가지고 있다
st.setInt(1, id);
//prepare쓸때는 sql을 쓰면 안된다
int result = st.executeUpdate();
st.close();
con.close();
return result;
}
}
댓글남기기