* eclipse 에서 jndi로 설정해서 화면에 데이터 출력
환경 |
eclipse (Helios), tomcat 6.0 |
|
날짜 |
2014 / 11 / 20 |
|
설정사항 |
tomcat( context.xml , 실행jsp파일 ) |
|
1) 소스 ( context.xml )
* 원본소스에서 주석은 제거함
* 빨간색 부분은 모두 추가한 부분입니다. |
<?xml version="1.0" encoding="UTF-8"?> <Context>
<WatchedResource>WEB-INF/web.xml</WatchedResource> <Environment name="SYSTEM_NAME" type="java.lang.String" value="real"></Environment> <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:@서버아이피:1521:orcl" user="kcms" password="kcms" maxActive="20" maxIdle="10" maxWait = "-1"/> </Context> |
2) jsp 소스파일
*빨간색 부분은 모두 추가한 부분입니다. |
<%@page import="javax.naming.InitialContext"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="java.sql.*, javax.sql.*" %> <%@ page import="javax.naming.*" %> <% 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"); String name = (String)envCtx.lookup("SYSTEM_NAME"); conn = ds.getConnection(); out.write("name</br>"+ name+"</br>");
String sQuery = "select * from 테이블명 where rownum < 2"; stmt = conn.createStatement(); rs = stmt.executeQuery(sQuery); while(rs.next()){ out.write(rs.getString(1)); } }catch(Exception e ){ e.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){ } } }
%> |
3) 결과