티스토리 뷰

Programming/JDBC

[JDBC] DAO

Allg 2017. 7. 3. 13:58


DAO - Data Access Object

1.개념

  • DAO(Data Access Object-데이터 접근 객체)는 데이터베이스에 관련된 작업을 전문적으로 담당하는 객체입니다.

2. DAO.java

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

public class DAO {

    /*
     * DAO(Data Access Object - 데이터 접근 객체) 데이터베이스에 관련된 작업을 전문적으로 담당하는 객체이다.
     * 
     */
    private Connection con;
    private Statement stmt;
    private ResultSet rs;

    private static final String DRIVER = "com.mysql.jdbc.Driver";
    private static final String URL = "jdbc:mysql://localhost:3306/test_db";
    private static final String USER = "root";
    private static final String PW = "admin1214";

    public DAO() {
        try {
            Class.forName(DRIVER);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    public void select1() {

        String sql = "select * from customers";

        try {
            con = DriverManager.getConnection(URL, USER, PW);
            stmt = con.createStatement();
            rs = stmt.executeQuery(sql);

            while (rs.next()) {
                System.out.print(rs.getString("id") + " ");
                System.out.print(rs.getString("pass") + " ");
                System.out.print(rs.getString("name") + " ");
                System.out.print(rs.getString("phone") + " ");
                System.out.println(rs.getString("email") + " ");
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            close(con, stmt, rs);
        }
    }

    public ArrayList<Info> select2() {

        ArrayList<Info> list = new ArrayList<Info>();
        String sql = "select * from customers";
        try {
            con = DriverManager.getConnection(URL, USER, PW);
            stmt = con.createStatement();
            rs = stmt.executeQuery(sql);


            while (rs.next()) {
                Info info = new Info();
                info.setId(rs.getString("id"));
                info.setPass(rs.getString("pass"));
                info.setName(rs.getString("name"));
                info.setPhone(rs.getString("phone"));
                info.setEmail(rs.getString("email"));
                list.add(info);
            }
            return list;

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            close(con, stmt, rs);
        }
        return null;
    }

    public void close(Connection con, Statement stmt, ResultSet rs) {

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

}

3. Info.java - select 결과 중 row를 저장하는 Class(필드는 DB 테이블의 칼럼으로 구성)


public class Info {

    private String id;
    private String pass;
    private String name;
    private String phone;
    private String email;

    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getPass() {
        return pass;
    }
    public void setPass(String pass) {
        this.pass = pass;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getPhone() {
        return phone;
    }
    public void setPhone(String phone) {
        this.phone = phone;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }


}

4. DaoTest.java

import java.util.ArrayList;

public class DaoTest {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        print();
    }

    public static void print() {

        DAO dao = new DAO();

        //select1()
        dao.select1();

        //select2();
        ArrayList<Info> list = dao.select2();

        if (list != null) {
            for (Info i : list) {
                System.out.print(i.getId() + " ");
                System.out.print(i.getPass() + " ");
                System.out.print(i.getName() + " ");
                System.out.print(i.getPhone() + " ");
                System.out.println(i.getEmail() + " ");
            }
        } else {
            System.out.println("list null!");
        }
    }

}


'Programming > JDBC' 카테고리의 다른 글

[JDBC] Delete 문 실행  (0) 2017.06.30
[JDBC] Update 문 실행  (2) 2017.06.30
[JDBC] Insert 문 실행  (0) 2017.06.27
[JDBC] Select 문 실행  (6) 2017.06.26
[JDBC] JDBC 드라이버 로딩과 Connection 생성  (3) 2017.06.26
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/11   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
글 보관함