블로그   |    태그로그   |    방명록
민준이
전체 (120)
공부 (49)
포트폴리오 (70)
공지사항 (0)
자유게시판 (1)
«   2010/03   »
  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      
월별보기
datasource 설정 및 예제(Tomcat)
Category : 공부/JAVA Date : 2009/11/04 20:28
2009/11/04 20:28 2009/11/04 20:28
1. 첨부한 zip 파일 압축을 푼 후 jar파일을 라이브러리에 추가



================================

2. web.xml

================================

<resource-ref>
<description>Oraclejava DB Connection</description>
<!-- 다음이 바로 리소스의 이름 -->
<res-ref-name>jdbc/oraclejava</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>





=================================

3. Context XML파일의 설정예(Tomcat의 경우 Topmcat_Home/conf/catalina/localhost 아래에 컨텍스트이름.xml)

=================================

Context명이 web이라고 할때



<Context path="/web" reloadable="true" docBase="C:\java2\project\web" workDir="C:\java2\project\web\work">

<Resource name="jdbc/oraclejava"
auth="Container"
type="javax.sql.DataSource"
username="scott"
password="tiger"
driverClassName="oracle.jdbc.driver.OracleDriver"
url="jdbc:oracle:thin:@192.168.1.131:1521:ORCL"/>



</Context>



==================

4. datasource.jsp

==================



<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<%@ page import="javax.naming.*" %>
<%@ page import="java.sql.*" %>
<%@ page import="javax.sql.*" %>
<%!
        public String nvl(String param) {
                return param != null? param:"";
        }
%>
<html>
    <head>
        <title></title>
        <meta http-equiv="content-type" content="text/html; charset=euc-kr">
    </head>
    <body>
<%
        // 데이터베이스 접속
        InitialContext    ctx = null;
        DataSource        ds = null;
        Connection        con = null;
        PreparedStatement pstmt = null;
        ResultSet         rs = null;
        String            sql="SELECT * FROM TAB ";
       
        try {
                ctx = new InitialContext();
        ds = (DataSource) ctx.lookup("java:comp/env/jdbc/oraclejava");
        con = (Connection) ds.getConnection();
           
        pstmt = con.prepareStatement(sql);
        rs = pstmt.executeQuery();
       
        out.println("<table>");
        while(rs.next()) {
            out.println("<tr>\n");
            for (int i = 1; i < 3; i++)
                    out.println("<td>" + nvl(rs.getString(i)) + "</td>");
            out.println("</tr>\n");
        }
        out.println("</table>");
        } catch (NamingException e) {
                e.printStackTrace();
        } catch (NullPointerException e) {
                e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (Exception e) {
                e.printStackTrace();
        } finally {
                if (rs != null)
                        try {
                                rs.close();
                        } catch (Exception e) {
                                // quit;
                        }
                       
                if (pstmt != null)
                        try {
                                pstmt.close();
                        } catch (Exception e) {
                                // quit
                        }
                       
                if (con != null)
                        try {
                                con.close();
                        } catch (Exception e) {
                                // quit
                        }
        }
%>
    </body>
</html>

상위로
엮인글 쓰기 1 | 덧글 쓰기 0 | 목록 열기
http://webpro2004.com/trackback/121
From : nixxnrtx 2010/02/08 14:37 DELETE
제목: nixxnrtx
nixxnrtx
[로그인][오픈아이디란?]
Creative Commons License 블로그 내에 모든 저작물은 크리에이티브 커먼즈코리아 저작자표시 - 비영리 - 변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.
최근 올라온 글
블로그 최근 덧글
최근 엮인글
Phentermine 37.5 mg online...
 Cheapest phentermine online.
Buy cheap tramadol mg table...
 Cheap tramadol prescription...
How much tickets are sold f...
 Air flight tickets.
Valtrex.
 Valtrex medication.
Buy cheap phentermine.
 Cheap phentermine cod.
즐겨찾기
Today : 14
Yesterday : 43
Total : 13717
Powered by Textcube 1.7.1 : Risoluto
Skin by mulder21c
RSS 주소보기 E-Mail 보내기