웹(java)/회원가입 및 로그인

JSP에서 DB연동하기(JNDI,DBCP커넥션풀)

byeol2ing 2019. 6. 24. 11:11
반응형




참조 : https://all-record.tistory.com/104?category=733042


JNDI

외부에 있는 객체를 가져오기 위한 기술


DBCP

데이터베이스와 연결된 커넥션을 만들어, 저장해두었다가 필요할 때 저장된 공간에서 가져다 쓰고 반환하는 기법






  • 1. API 관련 jar 파일 설치 - lib폴더 (ojdbc6.jar, tomcat-dbcp.jar)

  • 2. DBCP 정보 설정 - context.xml

  • 3. JNDI 리소스 사용 설정 - web.xml

  • 4. 자바 혹은 JSP 페이지에서 사용




  • 1. API 관련 jar 파일 설치 - lib폴더 (ojdbc6.jar, tomcat-dbcp.jar)

    (참조사이트에 들어가보면, 설치파일제공)



    2. DBCP 정보 설정 - context.xml

     [Servers] - [Tomcat v6.0 Server at localhost-config] 에 있는 context.xml 파일을 연다.

     아래와 같이 Resource 부분을 작성해준다.


    ip주소 : portnumber:dbname

    username

    password

    등부분은 자신에 맞게 수정해주어야한다.



    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
    <?xml version="1.0" encoding="UTF-8"?>
    <!--
      Licensed to the Apache Software Foundation (ASF) under one or more
      contributor license agreements.  See the NOTICE file distributed with
      this work for additional information regarding copyright ownership.
      The ASF licenses this file to You under the Apache License, Version 2.0
      (the "License"); you may not use this file except in compliance with
      the License.  You may obtain a copy of the License at
          http://www.apache.org/licenses/LICENSE-2.0
      Unless required by applicable law or agreed to in writing, software
      distributed under the License is distributed on an "AS IS" BASIS,
      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      See the License for the specific language governing permissions and
      limitations under the License.
    --><!-- The contents of this file will be loaded for each web application --><Context>
     
        <!-- Default set of monitored resources. If one of these changes, the    -->
        <!-- web application will be reloaded.                                   -->
        <WatchedResource>WEB-INF/web.xml</WatchedResource>
        <WatchedResource>WEB-INF/tomcat-web.xml</WatchedResource>
        <WatchedResource>${catalina.base}/conf/web.xml</WatchedResource>
     
        <!-- Uncomment this to disable session persistence across Tomcat restarts -->
        <!--
        <Manager pathname="" />
        -->
        <Resource auth="Container" 
              name="jdbc/orcl" 
              driverClassName="oracle.jdbc.driver.OracleDriver" 
              type="javax.sql.DataSource" 
              url="jdbc:oracle:thin:@localhost:1521:orcl" 
              username="scott"
              password="1234" 
              loginTimeout="10" 
              maxActive="50" 
              maxIdle="20"
              maxWait="5000" 
              testOnBorrow="true" />
     
     
    </Context>
    cs


    3. JNDI 리소스 사용 설정 - web.xml



    web.xml을 생성한후, <resource-ref> 부분을 작성한다.


    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
             xmlns="http://java.sun.com/xml/ns/javaee" 
             xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
             xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
             id="WebApp_ID" version="2.5">
     <display-name>JSP_create</display-name>
     
    <resource-ref>
         <description>connection</description>
         <res-ref-name>jdbc/orcl</res-ref-name>
         <res-type>javax.sql.DataSource</res-type>
         <res-auth>Container</res-auth>
    </resource-ref>
     
    </web-app>
     
    cs


    반응형