09.게시글 개수 구하기
게시글 개수 구하기
전체 게시글 개수를 구해야 한다
package ex1;
import java.sql.SQLException;
import com.newlecture.app.console.Noticeconsole;
public class Program5 {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Noticeconsole console = new Noticeconsole();
// 방법2:현재 목록을 구성:console 콘솔이 목록를 가지고 있는것도 가능 (main이목록을 가질건지 콘솔이 가질건지 확인)
//Noticeconsole이 상태값을가지고 있는것이 적합하다
int page; //방법1:페이지를 기억하는 상태 변수가 하나 필요하다
//종료메뉴를 클릭할때까지는 계속돈다
EXIT:
while(true) {
// console.printNoticeList(page); 목록을 알 수 있게 page를 넣을 수도 있다
console.printNoticeList();
int menu = console.inputNoticeMenu();
switch(menu) {
case 1: //상세조회
break;
case 2: // 이전- 현재 페이지가 이전페이지인지 다음 페이지인지 알아야한다
// page--;
console.movePrevList();
break;
case 3: //다음
// page++;
console.moveNextList();
break;
case 4: //글쓰기
break;
case 5 : //종료
System.out.println("bye~");
break EXIT;
default: ////만약 범위를 벗어난 것을 입력하면?
System.out.println("<사용방법> 메뉴는 1~4까지만 입력 할 수 있습니다");
break;
}
}
}
}
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;
private int page;
private int count;
public Noticeconsole() {
service = new NoticeService();
page=1;
count=0; // 기본값은 0으로 설정
}
public void printNoticeList() throws ClassNotFoundException, SQLException {
List<Notice> list = service.getList(page); // 페이지 1 넣어보기
count = service.getcount(); // 목록을 출력하고자하면 얻어와야한다
System.out.println("----------------------------");
System.out.printf("<공지사항> 총%d게시글\n",count); /// 카운트는 리스트를 구할때마다 그값이 달라지기때문에 이렇게 하면
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() {
Scanner scan = new Scanner(System.in);
System.out.printf("1.상세조회/2.이전/3.다음/4.글쓰기>");
String menu_ = scan.nextLine();// 정수가 아닌 다른 것이 눌렸을 경우 에러가 발생한다 다음입력에 영향을 줄 수 있다
// 메뉴를 임시변수에다가 담는다
int menu = Integer.parseInt(menu_); // 메뉴를 정수형으로 바꾼다
return menu;
}
public void movePrevList() {
if(page == 1) {
System.out.println("이전페이지가 없습니다");
return;
}
page --;
}
public void moveNextList() {
// if(page == ?) { // 마지막 페이지가 몇인지 알 수 ㅇ없음
// System.out.println("이전페이지가 없습니다");
// return;
}
}
package com.newlecture.app.service;
import java.beans.Statement;
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; // 1,11,21,31
int end = 10* page; //10,20,30,40
String sql = "select * from NOTICE_VIEW 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;
}
// 단일값을 얻어오는 것 scalar/////////
public int getcount() throws ClassNotFoundException, SQLException {
int count = 0;
String sql = "select count (id) count 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);
// resultset으로 받는것은 단일값 하나로 가지는 레코드를 가진다
// 값을 꺼낼때는 그 결과집합에 대한 컬럼을 가져오게되는데 실행하면 컬럼명이 count(id)로 나오게 된다
// 별칭을 줘야한다 select count (id) count from NOTICE;
if(rs.next())
//위 처럼 id에 별칭을 주므로 count를 id로 쓸 수있다
count = rs.getInt("count");
rs.close();
st.close();
con.close();
return count;
}
//뒤의 쿼리문들 삭제함
댓글남기기