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(int page) 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 ("
+ "select ROWNUM NUM, N.* from ("
+ " SELECT * FROM NOTICE ORDER BY REGDATE DESC "
+ " )N "
+ ")"
+ "where NUM between ? AND ?";
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection(url,uid,pwd);
PreparedStatement st =con.prepareStatement(sql); // 여기서 sql을 먼저 전달하기 때문에
st.setInt(1, start);
st.setInt(2, end);
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;
}
}
```java
package com.newlecture.app.console;
import java.sql.SQLException;
import java.util.List;
import java.util.Scanner;
import com.newlecture.app.entity.Notice;
import com.newlecture.app.service.NoticeService;
public class Noticeconsole {
private NoticeService service;
public Noticeconsole() {
service = new NoticeService();
}
public void printNoticeList() throws ClassNotFoundException, SQLException {
List<Notice> list = service.getList(1); // 페이지 1 넣어보기
//데이터필요
System.out.println("----------------------------");
System.out.printf("<공지사항> 총%d게시글\n",12);
System.out.println("----------------------------");
for(Notice n :list) {
System.out.printf("%d. %s / %s / %s\n",
n.getId(),
n.getTitle(),
n.getWriterld(),
n.getRegDate());
}
System.out.println("----------------------------");
System.out.printf(" %d%d pages\n", 1, 2);
}
public int inputNoticeMenu() {
try (Scanner scan = new Scanner(System.in)) {
System.out.printf("1.상세조회/2.이전/3.다음/4.글쓰기>");
String menu_ = scan.nextLine();// 정수가 아닌 다른 것이 눌렸을 경우 에러가 발생한다 다음입력에 영향을 줄 수 있다
// 메뉴를 임시변수에다가 담는다
int menu = Integer.parseInt(menu_); // 메뉴를 정수형으로 바꾼다
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return 0;
}
}
댓글남기기