ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • JDBC.View (조회수+ 뷰 예시)
    자바 초보개발/JDBC 2023. 11. 3. 17:17

    package ch07.oracle.board;

     

    import java.sql.Connection;

    import java.sql.DriverManager;

    import java.sql.PreparedStatement;

    import java.sql.ResultSet;

    import java.util.Scanner;

     

    /*

    * 게시판 글보기 - 글번호 select no, title, content, writer, writeDate,

    * hit from board where no = ?

    * */

    public class BoardViewMain {

     

    @SuppressWarnings("resource")

    public static void main(String[] args) {

    System.out.println("-- [[ BoardViewMain.main() ]] -----");

    //필요한 객체 선언 - select

     

    Connection con = null;

    PreparedStatement pstmt = null;

    ResultSet rs = null;

     

    String driver = "oracle.jdbc.OracleDriver";

    String url = "jdbc:oracle:thin:@localhost:1521/xe";

    String uid = "java";

    String upw = "java";

     

    Scanner scanner = new Scanner(System.in);

    //번호를 받는다

     

    //처리결과 저장

    BoardVO vo = null;

     

    System.out.print("보여줄 글 번호 입력 :");

    long no = 0;

    //보여줄 글번호 선언

    //오류날줄 알면 예외처리

    try {

    //보여줄 글번호 입력처리, 숫자 아니면 예외처리와 프로그램 종료

    no = Long.parseLong(scanner.nextLine());

    }catch(Exception e) {

    e.printStackTrace();

    System.out.println("숫자만 입력해 주세요");

     

    //비정상 종료임을 알려준다.

    System.out.println("프로그램을 종료합니다.");

    System.exit(1);

    }

     

     

    try {//정상처리

    System.out.println("정상처리 시작>>>");

    Class.forName(driver);

    System.out.println("1.드라이버 확인 끝");

    //1.드라이버 확인

     

    con = DriverManager.getConnection(url, uid,upw);

    System.out.println("2.연결 확인 끝");

    //2. 연결 확인

     

    String sql = "update board set hit = hit + 1 where no = ?";

    System.out.println("sql :" + sql);

    System.out.println("3.sql 확인 끝");

    //3. 실행할 sql문 작성

     

     

    //4. 실행 객체 &데이터 생성

    pstmt = con.prepareStatement(sql);

    pstmt.setLong(1, no);

    //pstmt.set타입(?의 위치번호,데이터)

     

    System.out.println("4.실행객체,데이터 생성 확인");

     

    //5. 실행 - select : ResultSet , insert/update/delete - integer

    int result = pstmt.executeUpdate();

    if(result == 1)

    System.out.println("5. 조회수 1 증가 실행 성공");

    else {

    System.out.println("5. 실행 성공, 조회수 1 증가 실패.글번호 없음");

     

    throw new Exception("없는 글번호를 입력하셨습니다");

     

    }

     

    //------- 데이터 가져오기----------

     

    // 3.sql

    sql = "select no,title,content,writer, to_char(writeDate, 'yyyy/mm/dd' ) writeDate, hit from board where no = ?";

    // 4. 실행 객체& 데이터 세팅

    pstmt = con.prepareStatement(sql);

    pstmt.setLong(1, no);

    // 5. 실행

     

    rs = pstmt.executeQuery();

     

    //6. 저장 또는 출력

    if(rs != null && rs.next()) {

    vo = new BoardVO();

    vo.setNo(rs.getLong("no"));

    vo.setTitle(rs.getString("title"));

    vo.setContent(rs.getString("content"));

    vo.setWriter(rs.getString("writer"));

    vo.setWriteDate(rs.getString("writeDate"));

    vo.setHit(rs.getLong("hit"));

    }

     

     

    // select = pstmt.executeQuery()로 실행

    System.out.println("연결 확인 끝");

     

    System.out.println("정상처리 끝>>>");

    }catch(Exception e) {

    System.out.println("예외처리 시작------");

    e.printStackTrace();

    System.out.println("예외처리 끝-----");

    }finally {

    try {

    System.out.println("finally 정상처리 시작>>>");

     

     

    System.out.println("finally 정상처리 끝>>>");

    } catch (Exception e) {

    System.out.println("finally 예외처리 시작------");

    e.printStackTrace();

    System.out.println("finally 예외처리 끝-----");

    } //finally catch end

    } //finally end

    //가져온 데이터 출력

    System.out.println("==================================");

    System.out.println("글번호 : " + vo.getNo());

    System.out.println("제목 : " + vo.getTitle());

    System.out.println("내용 : " + vo.getContent());

    System.out.println("작성자 : " + vo.getWriter());

    System.out.println("작성일 : " + vo.getWriteDate());

    System.out.println("조회수 : " + vo.getHit());

    System.out.println("==================================");

     

    scanner.close();

    }//main end

     

    }//class end

    '자바 초보개발 > JDBC' 카테고리의 다른 글

    jdbc.5  (0) 2023.11.08
    jdbc 홈페이지 제작 관리 코드 모듈  (0) 2023.11.08
    jdbc. 데이터 입력 - INSERT INTO 예시  (0) 2023.11.06
    JDBC. List 예시  (0) 2023.11.03
    JDBC.시작  (0) 2023.11.03
Designed by Tistory.