[Tomcat] Tomcat 6 JNDI 설정

2010. 5. 28. 11:12Was/Tomcat


<Resource name="jdbc/OracleTest"
auth="Container"
type="oracle.jdbc.pool.OracleDataSource"
driverClassName="oracle.jdbc.driver.OracleDriver"
factory="oracle.jdbc.pool.OracleDataSourceFactory"
url="jdbc:oracle:thin:@localhost:1521:XE"
user="hr"
password="hr"
maxActive="20"
maxIdle="10"
maxWait="-1" />

위 형식으로 다음 두 군데 중 하나에 설정
  1. context.xml의 <Context>태그사이
  2. server.xml의 아래쪽 <Context>태그사이

jsp 페이지로 테스트

<?xml version="1.0" encoding="UTF-8" ?>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" import="java.sql.*, javax.sql.*, javax.naming.*"%>
    
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>JNDI Test</title>
</head>
<body>
<%
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
    
    try {    
        Context initCtx = new InitialContext();
        Context envCtx = (Context)initCtx.lookup("java:/comp/env");
        DataSource ds = (DataSource)envCtx.lookup("jdbc/OracleTest");
        
        conn = ds.getConnection();
        out.write("<p>DB Connection..</p>");
        
        String sQuery = "SELECT * FROM EMPLOYEES";
        stmt = conn.createStatement();
        rs = stmt.executeQuery(sQuery);
        
        while (rs.next()) {
            out.write(rs.getString(1) + "<br />");
        }
    } catch(Exception ex) {
        ex.printStackTrace();
    } finally {
        if (rs != null) try {rs.close(); } catch (Exception ex2) {}
        if (stmt != null) try {stmt.close(); } catch (Exception ex3) {}
        if (conn != null) try {conn.close(); } catch (Exception ex4) {}
    }
%>
</body>
</html>

출처 : http://soriwa.tistory.com/78