14. DBCP
DBCP
* TOMCAT 5.0 이상은 등록되어 있음. (라이브러리) 이전 버전은 사이트에서 다운받아 셋팅.
* 아파치 사이트로 이동. ( http://www.apache.org/ )
- tomcat - document - tomcat7.0 - user Guide - 9) JDBC DataSoureces 에서 오라클 context사용 예 사용.
* -project
- WebContent
- MEMA-INF
- context.xml (파일 생성)
- WEB-INF
- web.xml (내용 추가)
* 설치된 oracle에서 ojdbc6.jar ( 테스트 버전 oracle XE 11.) 를 복사하여 JAVA 설치된 곳의
..\Java\jre1.8.0_121\lib\ext
..\Java\jdk1.8.0_121\jre\lib\ext
두곳에 복사한다.
* context.xml
: url, username, password 등 기본 사항 변경.
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Resource name="jdbc/myoracle" auth="Container"
type="javax.sql.DataSource"
driverClassName="oracle.jdbc.OracleDriver"
url="jdbc:oracle:thin:@127.0.0.1:1521:xe"
username="scott"
password="tiger"
maxActive="20"
maxIdle="10"
maxWait="-1"/>
</Context>
* web.xml
; jdbc/myoracle는 동일해야 한다.
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>day06_dbcp</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<!-- ///////////// DBCP설정 //////////////////////// -->
<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>
[[ test.jsp ]]
<%@page import="test.dbcp.DbcpBean"%>
<%@page import="java.sql.Connection"%>
<%@page import="javax.sql.DataSource"%>
<%@page import="javax.naming.InitialContext"%>
<%@page import="javax.naming.Context"%>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>
</head>
<body>
<%
Context initContext = new InitialContext();
Context envContext = (Context)initContext.lookup("java:/comp/env");
DataSource ds = (DataSource)envContext.lookup("jdbc/myoracle");
Connection conn = ds.getConnection();
%>
conn:<%=conn %><br/>
<%
DbcpBean dbcp=new DbcpBean();
Connection con=dbcp.getCon();
%>
con:<%=con %><br/>
</body>
</html>
[[ DbcpBean.java ]]
package test.dbcp;
import java.sql.Connection;
import java.sql.SQLException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
public class DbcpBean {
private DataSource ds;
public DbcpBean(){
try{
Context initContext = new InitialContext();
Context envContext = (Context)initContext.lookup("java:/comp/env");
ds = (DataSource)envContext.lookup("jdbc/myoracle");
}catch(NamingException ne){
System.out.println(ne.getMessage());
}
}
public Connection getCon(){
Connection con=null;
try{
con = ds.getConnection();
return con;
}catch(SQLException se){
System.out.println(se.getMessage());
return null;
}
}
}