[    JSP DB(데이터베이스)    ]




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
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.Connection"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
 
<%! // 선언부
    // 커넥션 객체 선언
    Connection conn = null;
    // 쿼리문에 대한 PreparedStatement 객체
    PreparedStatement pstmt = null;
    // 쿼리 실행 결과를 담고 있는 ResultSet 객체
    ResultSet rs = null;
     
    // 디비 접속 관련 정보
    String id = "scott"; // db 사용자 id
    String pw = "Dudfhd13"; // db 사용자 pw
    String driver = "oracle.jdbc.driver.OracleDriver"; // driver
    String url = "jdbc:oracle:thin:@localhost:1521:xe"; // db 접속 url -> localhost(주소 ip), xe-> DB이름
    String query = "insert into student(id,name,address) values(?,?,?)"; // preparedStatement를 이용할 땐 값은 ? 부분에 들어가게 됨(Statement보다 편리)
%>
 
<%
 
    try{
         
        // driver 로드
        Class.forName(driver);
        // DB Connection 객체 얻어오기
        conn = DriverManager.getConnection(url, id, pw);
        // transaction처리를 위해 자동으로 커밋되는 것을 false로 세팅
        conn.setAutoCommit(false);
        // PreparedStatement 객체 얻어오기
        PreparedStatement pstmt = conn.prepareStatement(query);
        // 위의 query에 ? 에 해당하는 곳에 값 대입 각 1,2,3은 첫번째 ? 두번째 ? 세번째 ?를 의미
        pstmt.setString(1, "firstId");
        pstmt.setString(2,"hyrName");
        pstmt.setString(3,"seoulAddress");
        // delete,update,insert 등의 작업시엔 executeUpdate()로 쿼리를 수행함 이때 처리된 튜플수가 반환됨
        // select문의 경우 executeQuery()메서드를 통해 쿼리를 수행
        int updateCnt = pstmt.executeUpdate();
        // commit
        conn.commit();
         
    }catch(Exception e){
        e.printStackTrace();
        // 처리에 실패한 경우 롤백
        conn.rollback();
    }finally{ // 자원 해제
        if ( rs != null ) rs.close();
        if ( pstmt != null ) pstmt.close();
        if ( conn != null ) conn.close();
    }
%>
 
<%
    // [ select 문으로 데이터 가져오기 ]
    try{
        Class.forName(driver);
        conn = DriverManager.getConnection(url,id,pw);
        conn.setAutoCommit(false);
        pstmt = conn.prepareStatement("select * from student");
        rs = pstmt.executeQuery();
        while(rs.next()){
            String id = rs.getString("id");
            String name = rs.getString("name");
            String addr = rs.getString("address");
            out.println("id : " + id + " name : " + name + " address : " + addr);
        }
        conn.commit();
    }catch(Exception e){
        e.printStackTrace();
        conn.rollback();
    }finally{
        if ( rs != null ) rs.close();
        if ( pstmt != null ) pstmt.close();
        if ( conn != null ) conn.close();
    }
%>


'스터디 > JSP' 카테고리의 다른 글

JSP 파일업로드  (2) 2017.08.16
JSP 커넥션 풀(Connection Pool)  (0) 2017.08.16
JSP 자바 빈즈(JAVA BEANS)  (0) 2017.08.14
JSP 에러페이지에 대하여...  (0) 2017.08.11
JSP 쿠키(cookie)에 대하여...  (0) 2017.08.11

+ Recent posts