헤르메스 LIFE

[JNDI] 간단한 Tomcat + JSP + JNDI 테스트 본문

Spring Framework

[JNDI] 간단한 Tomcat + JSP + JNDI 테스트

헤르메스의날개 2022. 11. 21. 00:00
728x90

JNDITest.zip
0.93MB

개발환경

Eclipse Java EE IDE for Web Developers ( Eclipse IDE Neon 3 Packages x64 )
 
PostgreSQL 14.1
Tomcat 8.5.81
JDK 1.8.0_202

%CATALINA_HOME%/conf/server.xml

  <GlobalNamingResources>
<!--
    auth : 컨테이너를 자원 관리자로 기술
    name : JDBC이름, 변경 가능
    driverClassName : JDBC 드라이버
    type : 웹에서 이 리소스를 사용할 때 DataSource로 리턴됨
    username : 접속계정
    password : 접속할 계정 비밀번호
    
    maxActive : 최대 연결 가능한 Connection수 (기본 20개)
    maxIdle : Connection pool 유지를 위해 최대 대기 connection 숫자
    maxWait : 사용 가능한 커넥션이 없을 때 커넥션 회수를 기다리는 시간 (1000 = 1초)
    testOnBorrow : db에 test를 해볼 것인지
-->
    <Resource
        name="jdbc/SpringDB"
        auth="Container"
        type="javax.sql.DataSource"   
        driverClassName="org.postgresql.Driver"
        url="jdbc:postgresql://localhost:5432/springboot"
        username="hermeswing" 
        password="pass"  
        maxTotal="30"  
        maxIdle="30"
        maxWaitMillis="3000"              
        minIdle="10"
        testWhileIdle="true"    
        validationQuery="select 1"
        closeMethod="close" />
  </GlobalNamingResources>

WebContent\META-INF\context.xml

	<context>
	    <ResourceLink name="jdbc/SpringDB" global="jdbc/SpringDB" type="javax.sql.DataSource" />
	</context>

WebContent\WEB-INF\web.xml

<!--
    description : 설명
    res-ref-name : JDBC 이름, <Resource>의 name 부분과 동일하게 입력
    res-type : <Resource>의 type 부분과 동일하게 입력
    res-auth : <Resource>의 auth 부분과 동일하게 입력
-->
    <resource-ref>
	    <description></description>
	    <res-ref-name>jdbc/SpringDB</res-ref-name>
	    <res-type>javax.sql.DataSource</res-type>
	    <res-auth>Container</res-auth>
	</resource-ref>

DBtest.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.sql.*"%>
<%@ page import="db.utils.DBConnection"%> <!-- DB연결 클래스를 import한다. -->
<html>
 
<head>
</head>
<body>
    <center>
        <table border="3" bordercolor="skyblue">
        <tr bgcolor="skyblue"><td>이름<td>직업</tr>
        
        <%
        // 쿼리문
        String query="select * from member";
        
        // 커넥션 연결
        Connection conn = DBConnection.getConnection();
        
        // DB에 쿼리문을 보낸다.
        PreparedStatement pstmt = conn.prepareStatement(query);
        
        // 쿼리문의 결과값을 rs에 담는다.
        ResultSet rs = pstmt.executeQuery();
        
        // 결과값을 출력한다.
        while(rs.next()){
            out.println("<tr>");
            out.println("<td>"+rs.getString("user_id"));
            out.println("<td>"+rs.getString("user_name"));
            out.println("</tr>");
        }
        
        %>
        </table>
    </center>
</body>
</html>

 


http://localhost:8080/JNDITest/DBtest.jsp


참고

https://hermeslog.tistory.com/359

 

[Tomcat] Tomcat에서의 JNDI JDBC 설정 #1

필수 라이브러리 commons-dbcp.jar commons-collections.jar commons-pool.jar 방법1. server.xml 설정 factory org.apache.commons.dbcp.BasicDataSourceFactory url jdbc:oracle:thin:@localhost:1521:ORCL driverClassName oracle.jdbc.driver.OracleDriver use

hermeslog.tistory.com

 

728x90