JNDI - Java Naming and Directory Interface
JNDI(Java Naming and Directory Interface) 란?Permalink
-
디렉토리 서비스에서 제공하는 데이터 및 객체를 발견하고 참고하기 위한 JAVA API
-
J2EE 플랫폼의 일부
-
여러대의 서버간에 JNDI를 이용하여 객체를 등록, 참조하여 이용
-
javax.naming 패키지 안에 존재
-
여러 웹서버(톰캣,웹로직,제우스 등) 에서 사용
-
기본 네임 스페이스 java:com/env이다.
아래 예제는
jdbc pooling 을 JNDI로 사용하는 예제이다.
톰캣 lib 폴더에 추가.
commons-collections-3.1.jar, commons-dbcp-1.2.1.jar, commons-pool-1.2.jar, ojdbc14.jar
톰캣 conf/Catalina/Context명.xml 에 추가
<Context ~~>
<Resource name="jdbc/JDBC명아무거나"
auth="Container"
type="javax.sql.DataSource"
username="아이디"
password="패스워드"
driverClassName="oracle.jdbc.driver.OracleDriver"
url="jdbc:oracle:thin:@localhost:orcl"
maxActive="100"
maxIdle="30"
loginTimeout="10"
maxWait="10000"/>
</Context>
웹문서 폴더에서 WEB-INF/web.xml 추가
<resource-ref>
<description>OracleDatasource</description>
<res-ref-name>jdbc/JDBC명아무거나</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
JSP 작성
<%@ page language="java" pageEncoding="UTF-8" contentType="text/html; charset=utf-8"%>
<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<%@ page import="javax.sql.*" %>
<%@ page import="javax.naming.*" %>
<%
/*************************************************************************************************************
* 디비연결 셋팅
*************************************************************************************************************/
//디비연결 변수[게시물]
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
PreparedStatement pstmt = null;
//디비연결 설정 [START]
String url = "jdbc:oracle:thin:@localhost:1521:orcl";
String user = "아이디";
String passwd = "패스워드";
//디비연결 설정 [END]
/*************************************************************************************************************
* 디비연결 및 쿼리 질의 [START]
*************************************************************************************************************/
try{
Context initCtx = new InitialContext();
Context envCtx = (Context)initCtx.lookup("java:/comp/env");
DataSource ds = (DataSource)envCtx.lookup("jdbc/orclTp");
conn = ds.getConnection();
//stmt = conn.createStatement();
//rs = stmt.executeQuery("select count(*) from dual");
pstmt = conn.prepareStatement("select count(*) from dual");
rs = pstmt.executeQuery();
while(rs.next()){ //레코드를 이동시킨다.
out.println("While Test");
}
} finally {
if (rs != null) try {rs.close(); }catch(SQLException ex) {}
if (stmt != null) try {stmt.close(); } catch(SQLException ex) {}
if (conn != null) try {conn.close(); }catch(SQLException ex) {}
}
%>