19.05.01 JSP와 데이터베이스 연동 (22강~25강)

Back-End/JSP 2019. 5. 1. 16:41
728x90
반응형

-데이터베이스를 연동하는 3가지 방법-

 

 

 

  1.JSP내에서 데이터베이스 연동하는 방법


  2.JSP로 받아서 자바(DAO)클래스를 이용해서 데이터베이스에 넣고 빼는 방법
   (가장 많이 사용하는 방법)

 

  3. 커넥션풀을 이용해서 데이터베이스에 자료를 집어넣는 방법

 

 

 

Ojdbc14.jar : 자바와 데이터베이스를 연결하기 위한 파일

ojdbc6.jar

 

 

 

 

-예제 및 출력 결과-

 

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
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 
<center>
    <h2>회원 가입</h2>
    <body>
        <form action="MemberJoinProc.jsp" method="post">
            <table width="500" border="1">
 
                <tr height="50">
                    <td width="150" align="center">아이디</td>
                    <td width="350" align="center"><input type="text" name="id"
                        size="40" placeholder="id를 넣으세요."></td>
                <tr height="50">
                    <td width="150" align="center">패스워드</td>
                    <td width="350" align="center"><input type="password"
                        name="pass1" size="40" placeholder="비밀번호는 영문과 숫자만 넣어주세요."></td>
                <tr height="50">
                    <td width="150" align="center">패스워드 확인</td>
                    <td width="350" align="center"><input type="password"
                        name="pass2" size="40"></td>
                <tr height="50">
                    <td width="150" align="center">이메일</td>
                    <td width="350" align="center"><input type="email"
                        name="email" size="40" placeholer></td>
                <tr height="50">
                    <td width="150" align="center">전화번호</td>
                    <td width="350" align="center"><input type="tel" name="tel"
                        size="40"></td>
                <tr heigth="50">
                    <td width="150" align="center">당신의 관심분야</td>
                    <td width="350" align="center"><input type="checkbox"
                        name="hobby" value="캠핑">캠핑 &nbsp;&nbsp; <input
                        type="checkbox" name="hobby" value="등산">등산 &nbsp;&nbsp; <input
                        type="checkbox" name="hobby" value="영화">영화 &nbsp;&nbsp; <input
                        type="checkbox" name="hobby" value="독서">독서 &nbsp;&nbsp;</td>
                </tr>
                <tr heigth="50">
                    <td width="150" align="center">당신의 직업은</td>
                    <td width="350" align="center"><select name="job">
                            <option value="교사">교사</option>
                            <option value="변호사">변호사</option>
                            <option value="의사">의사</option>
                            <option value="기술사">기술사</option>
                    </select></td>
                </tr>
                <tr height="50">
                    <td width="150" align="center">당신의 연령은</td>
                    <td width="350" align="center"><input type="radio" name="age"
                        value="10">10대 &nbsp;&nbsp; <input type="radio" name="age"
                        value="20">20대&nbsp;&nbsp; <input type="radio" name="age"
                        value="30">30대&nbsp;&nbsp; <input type="radio" name="age"
                        value="40">40대&nbsp;&nbsp;</td>
                </tr>
 
                <tr height="50">
                    <td width="150" align="center">하고싶은말</td>
                    <td width="350" align="center"><textarea rows="5" cols="40"
                            name="info"></textarea></td>
                </tr>
 
                <tr height="50">
                    <td align="center" colspan="2"><input type="submit"
                        value="회원 가입"> <input type="reset" value="취소">
            </table>
        </form>
</center>
</body>
</html>
 
 
cs

 

 

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
package model;
 
public class MemberBean { //자바빈 클래스
    
    private String id;
    private String pass1;
    private String pass2;
    private String email;
    private String tel;
    private String hobby;
    private String job;
    private String age;
    private String info;
    
    
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getPass1() {
        return pass1;
    }
    public void setPass1(String pass1) {
        this.pass1 = pass1;
    }
    public String getPass2() {
        return pass2;
    }
    public void setPass2(String pass2) {
        this.pass2 = pass2;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public String getTel() {
        return tel;
    }
    public void setTel(String tel) {
        this.tel = tel;
    }
    public String getHobby() {
        return hobby;
    }
    public void setHobby(String hobby) {
        this.hobby = hobby;
    }
    public String getJob() {
        return job;
    }
    public void setJob(String job) {
        this.job = job;
    }
    public String getAge() {
        return age;
    }
    public void setAge(String age) {
        this.age = age;
    }
    public String getInfo() {
        return info;
    }
    public void setInfo(String info) {
        this.info = info;
    }
    
    
    
}
 
cs

 

 

 

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
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<body>
 
    <% 
    request.setCharacterEncoding("EUC-KR"); //한글 처리
    
    //취미 부분은 별도로 읽어들여 다시 빈클래스에 저장
    
    String [] hobby = request.getParameterValues("hobby");
    //배열에 있는 내용을 하나의 스트림으로 저장
    String texthobby = "";
    
    for(int i =0; i< hobby.length; i++)
    {
        texthobby += hobby[i]+" "//취미를 배열을 사용해 하나씩 출력해서 texthobby에 넣는다.
    }
%>
 
    <!-- useBean을 이용하여 한꺼번에 데이터를 받아옴 -->
    <!-- 맵핑 시키시오 -->
    <jsp:useBean id="mbean" class="model.MemberBean">
        <jsp:setProperty name="mbean" property="*" />
    </jsp:useBean>
 
    <% 
    mbean.setHobby(texthobby); //기존 취미는 주소번지가 저장되기에 위에 배열의 내용을 하나의 스트림으로 저장한 변수를 다시 입력
    
    //오라클에 접속하는 소스를 작성
    String id = "system";
    String pass = "123456";
    String url = "jdbc:oracle:thin:@localhost:1521:XE";  //자신의 컴퓨터에 DB가 있으면 localhost 사용,다른 컴퓨터면 ip주소 적기
                                                        //오라클의 포트 1521, 전역데이터베이스 XE 적기
                                                        //접속 URL
                                                        
    try
    {
        //1.해당 데이터베이스를 사용한다고 선언 (클래스를 등록 = 오라클을 사용)
        Class.forName("oracle.jdbc.driver.OracleDriver");
        //2. 해당 데이터 베이스에 접속
        Connection con = DriverManager.getConnection(url,id,pass);
        //3. 접속후 쿼리준비하여 쿼리를 사용하도록 설정
        String sql = "insert into member values(?,?,?,?,?,?,?,?)"//8개의 값이 들어간다.
        //쿼리를 사용하도록 설정
        PreparedStatement pstmt = con.prepareStatement(sql); //jsp 에서 쿼리를 사용하도록 설정
        // ?에 맞게 데이터를 맵핑
        pstmt.setString(1, mbean.getId()); //SQL의 insert to values에 넣을 값들.
        pstmt.setString(2, mbean.getPass1());
        pstmt.setString(3, mbean.getEmail());
        pstmt.setString(4, mbean.getTel());
        pstmt.setString(5, mbean.getHobby());
        pstmt.setString(6, mbean.getJob());
        pstmt.setString(7, mbean.getAge());
        pstmt.setString(8, mbean.getInfo());
        //4. 오라클에서 쿼리를 실행 하시오.
        pstmt.executeUpdate(); //insert, update, delete 시 사용하는 메소드
        
        //5. 자원 반납
        con.close();
        
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
 
    
%>
 
    오라클에 접속 완료 ~
 
</body>
</html>
cs

 

 

 

728x90
반응형
: