ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • JDBC. List 예시
    자바 초보개발/JDBC 2023. 11. 3. 17:16

    package ch07.oracle.board;

    /*

    * select no,title,writer,writeDate,hit

    * from board order by no desc

    *

    * */

     

    import java.sql.Connection;

    import java.sql.DriverManager;

    import java.sql.PreparedStatement;

    import java.sql.ResultSet;

    import java.util.ArrayList;

    import java.util.List;

     

    public class BoardListMain {

    public static void main(String[] args) {

    System.out.println("BoardListMain.main()--------");

     

    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"; //id

    String upw = "java"; // pw

     

    // 리스트 DB 처리

     

    try {

     

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

     

    Class.forName(driver);

    //1.드라이버 확인

    //정상처리

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

     

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

    System.out.println("2.DB연결 성공");

    //2. 연결 확인

     

    //3. 실행할 sql문 작성

     

    String sql = "select no,title,writer,to_char(writeDate, 'yyyy/mm/dd') writeDate,hit "

    +" from board order by no desc";

    System.out.println("3.BoardListMain.main().sql :" + sql);

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

     

    pstmt = con.prepareStatement(sql);

    System.out.println("4.실행 객체 생성 & 데이터세팅 성공");

    //실행 객체(인터페이스) 선언.

     

     

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

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

    rs = pstmt.executeQuery();

    System.out.println("5. 실행 완료 : rs - " + rs);

     

     

     

     

    //6. 저장 또는 출력

     

    //항목명 출력

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

    System.out.println("번호 / 제목 /작성자 / 작성일 / 조회수");

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

     

     

     

    //rs의 데이터 개수는 여러개.0개이상 : 반복문 사용

    //view 는 데이터가 1개여서 if안에 res.next()를 작성해도 된다.

    List<BoardVO> list = null;

    if(rs != null) {

    ///rs.next() : 여러개의 데이터가 들어있을때 현재 작업위치의

    //다음 데이터가 있으면 true 리턴. 위치도 다음데이터로 이동

    while(rs.next()) {

    //System.out.println(rs.getLong("no")+" / " +

    //rs.getString("title"));

    // System.out.print(" " + rs.getLong("no"));

    // System.out.print(" / " + rs.getString("title"));

    // System.out.print(" / " + rs.getString("writer"));

    // System.out.print(" / " + rs.getString("writeDate"));

    // System.out.print(" / " + rs.getString("hit"));

    // System.out.println();

    //

    //반복문 안에서 선언시 반복문을 빠져나가면 알아서 삭제되고 또 생성된다

    if(list == null) list = new ArrayList<BoardVO>();

     

    BoardVO vo = new BoardVO();

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

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

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

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

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

     

    list.add(vo);

    //리스트 데이터르 저장할 컬렉션 List 선언한다.

    //List<BoardVO> - 제네릭 선언: 클래스를 만들 때 List안에

    //데이터를 저장하는 배열 변수타입을 BoardVO로 만들자

     

    }

    }//end of if

    else {

    System.out.println("데이터가 존재하지 않습니다");

    }

     

    //list 데이터를 출력

    if(list == null) System.out.println("데이터가 존재하지 않습니다");

    else for(BoardVO vo:list) {

     

    System.out.print(" " + vo.getNo());

    System.out.print(" / " + vo.getTitle());

    System.out.print(" / " + vo.getWriter());

    System.out.print(" / " + vo.getWriteDate());

    System.out.print(" / " + vo.getHit());

    System.out.println();

    }

    System.out.println(list);

     

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

    System.out.println("6. 데이터 출력 또는 저장 완료");

     

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

    } catch(Exception e) { //예외처리 한꺼번에, 개발자를 위한 코드

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

    e.printStackTrace();

     

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

    }finally { //반드시 - 닫기

    try { //닫기 정상처리

    //7.닫기

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

     

    if(con !=null) con.close();

    if(pstmt !=null) pstmt.close();

    if(rs !=null) rs.close();

     

    System.out.println("7.닫기 성공 !!!!!!!!!!!!!!!!!!");

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

    }// finally try end

    catch(Exception e) { //예외처리

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

    //예외출력

    e.printStackTrace();

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

    } //finally catch end

     

    }// finally end

     

     

    System.out.println("main()끝-----");

    }//main end

    }//class end

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

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