본문 바로가기

JAVA/JDBC

JDBC : DAO 클래스 실습 (ArrayList)

DeptDAO Class

//DAO 실습

public class DeptDAO {

        //칼럼의 데이터형과 맞춰도 되지만 String으로 해도 상관없음

        private String deptno;

        private String dname;

        private String loc;

       

        public String getDeptno() {

               return deptno;

        }

        public void setDeptno(String deptno) {

               this.deptno = deptno;

        }

        public String getDname() {

               return dname;

        }

        public void setDname(String dname) {

               this.dname = dname;

        }

        public String getLoc() {

               return loc;

        }

        public void setLoc(String loc) {

               this.loc = loc;

        }

}

 

 

 

실행 Class

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.Statement;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.SQLException;

import java.util.ArrayList;

//jdbc : DAO실습

 

public class jdbcEx06 {

        public static void main(String[] args) {

              

               Connection conn = null;

               PreparedStatement pstmt = null;      

               ResultSet rs = null;

              

               //DeptDAO 여러개 담을 객체 : 테이블 내용 / select 전체 내용

               ArrayList<DeptDAO> depts = new ArrayList<>();

              

               try {

                       Class.forName("oracle.jdbc.driver.OracleDriver");

                       conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "scott", "tiger");

                      

                       String query = "select * from dept";

                       pstmt = conn.prepareStatement(query);

 

                       rs = pstmt.executeQuery();

                      

                       while(rs.next()){     

                              //한행 데이터

                              DeptDAO dao = new DeptDAO();

                              dao.setDeptno(rs.getString("deptno"));

                              dao.setDname(rs.getString("dname"));

                              dao.setLoc(rs.getString("loc"));

                             

                              depts.add(dao);

                       }

                      

                       for(int i = 0 ; i <depts.size() ; i++){

                              DeptDAO tmp = depts.get(i);

System.out.println(tmp.getDeptno() + "\t" + tmp.getDname() + "\t" + tmp.getLoc() + "\t");

                       }

                      

                      

               } catch (ClassNotFoundException e) {

                       e.printStackTrace();

               } catch (SQLException e) {

                       e.printStackTrace();

               } finally{

       if(pstmt != null) try{ pstmt.close();} catch(SQLException e){};                   

       if(rs != null) try{ rs.close();} catch(SQLException e){};

       if(conn != null) try{ conn.close();} catch(SQLException e){};

               }

 

        }

}