출처:http://cafe.naver.com/oraclesql.cafe?iframe_url=/ArticleRead.nhn%3Farticleid=73
* tomcat 5.5에 oracle db access 가능하게 connection pool 설정하기
1. server.xml에 추가
<Resource name="jdbc/myoracle" auth="Container"
type="javax.sql.DataSource" driverClassName="oracle.jdbc.OracleDriver"
url="jdbc:oracle:thin:@호스트IP:포트:SID입력"
username="scott" password="tiger" maxActive="20" maxIdle="10"
maxWait="-1"/>
주의) 호스트IP에 127.0.0.1 이라고 하지 말 것!!!
2. web.xml에 추가 (안해도 잘됨)
<resource-ref>
<description>Oracle Datasource example</description>
<res-ref-name>jdbc/myoracle</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>
3. context.xml에 추가
<ResourceLink global="jdbc/myoracle" name="jdbc/myoracle" type="javax.sql.DataSource"/>
4. 톰캣홈/common/lib 에 ojdbc14.jar 를 복사한다.
5. tomcat 다시 구동
6. jsp 소스 테스트
1) poll 이용
<%@ page import="java.sql.*, javax.naming.*, javax.sql.*"%>
<%
Context initCtx = new InitialContext();
/*
Context envContext = (Context)initContext.lookup("java:/comp/env");
DataSource ds = (DataSource)envContext.lookup("jdbc/myoracle");
*/
DataSource ds = (DataSource)initCtx.lookup("java:comp/env/jdbc/myoracle");
Connection conn = ds.getConnection();
Statement stmt = conn.createStatement();
String query = "select to_char(sysdate,'yyyy/mm/dd hh24:mi:ss') from dual"; // 현재 시간을 구하는 query
ResultSet rs = stmt.executeQuery(query);
if(rs.next()) {
out.println(rs.getString(1));
}
rs.close();
stmt.close();
conn.close();
%>
======================================
2) 소스에서 바로
<%@ page import="java.sql.*, javax.naming.*, javax.sql.*"%>
<%
Connection conn = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection("jdbc:oracle:thin:@호스트ip:포트:SID", "아이디", "비밀번호");
Statement stmt = conn.createStatement();
String query = "select to_char(sysdate,'yyyy/mm/dd hh24:mi:ss') from dual"; // 현재 시간을 구하는 query
ResultSet rs = stmt.executeQuery(query);
if(rs.next()) {
out.println(rs.getString(1));
}
} catch (ClassNotFoundException ne) {
System.out.println("**** ClassNotFountException from DBManager.java ****");
System.out.println(" " + ne.toString());
} catch (SQLException se) {
System.out.println("**** SQLException from DBManager.java ****");
System.out.println(" " + se.toString());
}
if( conn != null ) {
System.out.println("Coonection Success..");
}else{
}
try{
if(conn != null) {
conn.close();
System.out.println("Connection Closed");
}
}catch(Exception e){
e.printStackTrace();
}
%>