programming/jsp

JSP - DAO, BEAN 기본적 구현

히연쓰 2021. 5. 25. 17:39

0123
hublot - 오라클

 

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package com.heeyeon.model;
 
public class HublotBean {
    
    private int no;
    private String category;
    private String title;
    private String depth;
    private String price;
    private String link;
    private String hublotFile;
    private String hublotRealFile;
    
    public HublotBean() {}
    
    public HublotBean(int no, String category, String title, String depth, String price, String link, String hublotFile,
            String hublotRealFile) {
        super();
        this.no = no;
        this.category = category;
        this.title = title;
        this.depth = depth;
        this.price = price;
        this.link = link;
        this.hublotFile = hublotFile;
        this.hublotRealFile = hublotRealFile;
    }
 
    public int getNo() {
        return no;
    }
    public void setNo(int no) {
        this.no = no;
    }
    public String getCategory() {
        return category;
    }
    public void setCategory(String category) {
        this.category = category;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getDepth() {
        return depth;
    }
    public void setDepth(String depth) {
        this.depth = depth;
    }
    public String getPrice() {
        return price;
    }
    public void setPrice(String price) {
        this.price = price;
    }
    public String getLink() {
        return link;
    }
    public void setLink(String link) {
        this.link = link;
    }
    public String getHublotFile() {
        return hublotFile;
    }
    public void setHublotFile(String hublotFile) {
        this.hublotFile = hublotFile;
    }
    public String getHublotRealFile() {
        return hublotRealFile;
    }
    public void setHublotRealFile(String hublotRealFile) {
        this.hublotRealFile = hublotRealFile;
    }
 
    @Override
    public String toString() {
        return "HublotBean [no=" + no + ", category=" + category + ", title=" + title + ", depth=" + depth + ", price="
                + price + ", link=" + link + ", hublotFile=" + hublotFile + ", hublotRealFile=" + hublotRealFile + "]";
    }
    
}
 
cs

hublotBean 파일인데, 이 파일을 작성하는 방법은

https://heeyeonlife.tistory.com/27 

 

jsp - BEAN 작성 쉽게하기

jsp를 만들 때 이런식으로 package를 만든 후 ReplyBoardBean과 같은 class를 작성한다. package com.heeyeon.model; public class ReplyBoardBean { private int no; private String name; private String email..

heeyeonlife.tistory.com

이 글을 참고하면 될 것 같다.

 

DAO 파일을 기본 구성은

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package com.heeyeon.model;
 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
 
public class HublotBeanDao {
    String driver = "oracle.jdbc.OracleDriver";
    String url = "jdbc:oracle:thin:@localhost:1521:xe";
    String id = "test";
    String pw = "1234";
 
    Connection con = null;
    PreparedStatement pstmt = null;
    ResultSet rs = null;
 
    private void getconnection() {
 
        try {
            Class.forName(driver);
            con = DriverManager.getConnection(url, id, pw);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
 
    private void close(ResultSet rs, PreparedStatement pstmt, Connection con) {
 
        try {
            if (rs != null)
                rs.close();
            if (pstmt != null)
                pstmt.close();
            if (con != null)
                con.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
 
    private void close(PreparedStatement pstmt, Connection con) {
 
        try {
            if (pstmt != null)
                pstmt.close();
            if (con != null)
                con.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
 
}
cs

이렇게 될 것이다.

 

insert ,  delete, update 는 con, pstmt만 사용하고

select는 rs,con, pstmt 다 사용한다.

 

예를들어

1 ) insert의 경우

public int insertHublot(HublotBean hublotBean) {
		int result = 0;
		try {
			getConnection();
			String sql = "INSERT INTO HUBLOT VALUES (HUBLOT_SEQ.NEXTVAL,?,?,?,?,?,?,?)";
			pstmt = con.prepareStatement(sql);
			pstmt.setString(1, hublotBean.getCategory());
			pstmt.setString(2, hublotBean.getTitle());
			pstmt.setString(3, hublotBean.getDepth());
			pstmt.setString(4, hublotBean.getPrice());
			pstmt.setString(5, hublotBean.getLink());
			pstmt.setString(6, hublotBean.getHublotFile());
			pstmt.setString(7, hublotBean.getHublotRealFile());
			result = pstmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			close(pstmt,con);
		}
		return result;​

 

2) select의 경우

public ArrayList<HublotBean> selectAllHublot() {
		ArrayList<HublotBean> hublotList = new ArrayList<HublotBean>();
		try {
			getConnection();
			String sql = "SELECT * FROM HUBLOT";
			pstmt = con.prepareStatement(sql);
			rs = pstmt.executeQuery();
			while(rs.next()) {
				HublotBean hublotBean = new HublotBean();
				hublotBean.setNo(rs.getInt("no"));
				hublotBean.setCategory(rs.getString("category"));
				hublotBean.setTitle(rs.getString("title"));
				hublotBean.setDepth(rs.getString("depth"));
				hublotBean.setPrice(rs.getString("price"));
				hublotBean.setLink(rs.getString("link"));
				hublotBean.setHublotFile(rs.getString("hublotFile"));
				hublotBean.setHublotRealFile(rs.getString("hublotRealFile"));
				hublotList.add(hublotBean);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			close(rs,pstmt,con);
		}
		return hublotList;
	}

 

결과적으로 DAO의 코드는 이렇게 들어올 것이다.

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package com.heeyeon.model;
 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
 
public class HublotDao {
    String driver = "oracle.jdbc.OracleDriver";
    String url = "jdbc:oracle:thin:@localhost:1521:xe";
    String id = "test";
    String pw = "1234";
    
    Connection con = null;
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    
    private void getConnection() {
        try {
            Class.forName(driver);
            con = DriverManager.getConnection(url, id, pw);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    private void close(ResultSet rs, PreparedStatement pstmt, Connection con) {
        try {
            if(rs!=null) rs.close();
            if(pstmt!=null) pstmt.close();
            if(con!=null) con.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    private void close(PreparedStatement pstmt, Connection con) {
        try {
            if(pstmt!=null) pstmt.close();
            if(con!=null) con.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    public int insertHublot(HublotBean hublotBean) {
        int result = 0;
        try {
            getConnection();
            String sql = "INSERT INTO HUBLOT VALUES (HUBLOT_SEQ.NEXTVAL,?,?,?,?,?,?,?)";
            pstmt = con.prepareStatement(sql);
            pstmt.setString(1, hublotBean.getCategory());
            pstmt.setString(2, hublotBean.getTitle());
            pstmt.setString(3, hublotBean.getDepth());
            pstmt.setString(4, hublotBean.getPrice());
            pstmt.setString(5, hublotBean.getLink());
            pstmt.setString(6, hublotBean.getHublotFile());
            pstmt.setString(7, hublotBean.getHublotRealFile());
            result = pstmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            close(pstmt,con);
        }
        return result;
    }
    public ArrayList<HublotBean> selectAllHublot() {
        ArrayList<HublotBean> hublotList = new ArrayList<HublotBean>();
        try {
            getConnection();
            String sql = "SELECT * FROM HUBLOT";
            pstmt = con.prepareStatement(sql);
            rs = pstmt.executeQuery();
            while(rs.next()) {
                HublotBean hublotBean = new HublotBean();
                hublotBean.setNo(rs.getInt("no"));
                hublotBean.setCategory(rs.getString("category"));
                hublotBean.setTitle(rs.getString("title"));
                hublotBean.setDepth(rs.getString("depth"));
                hublotBean.setPrice(rs.getString("price"));
                hublotBean.setLink(rs.getString("link"));
                hublotBean.setHublotFile(rs.getString("hublotFile"));
                hublotBean.setHublotRealFile(rs.getString("hublotRealFile"));
                hublotList.add(hublotBean);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            close(rs,pstmt,con);
        }
        return hublotList;
    }
    
}
 
 
cs