Where The Streets Have No Name

[tomcat]Data Source를 사용해서 DB접속하기 본문

Developement/Java

[tomcat]Data Source를 사용해서 DB접속하기

highheat 2007. 6. 6. 17:59
출처 : http://hjazz.com/tt/blog/20

1. tomcat home/conf/server.xml 파일에서 다음과 같은 부분을 추가 해준다.
<Resource name="UserDatabase" auth="Container"
              type="org.apache.catalina.UserDatabase"
       description="User database that can be updated and saved"
           factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
          pathname="conf/tomcat-users.xml" />
<Resource auth="Container"
     driverClassName="oracle.jdbc.driver.OracleDriver" (오라클의 경우)
     maxActive="2"
     maxIdle="2"
     maxWait="5000"
     name="jdbc/myDatabase"
     password="password"
     type="javax.sql.DataSource"
     url="jdbc:oracle:thin:@ip address:port number:ora9"(예)
     username="username"/> (빨간부분을 설정한다)


2. tomcat home/conf/catalina/localhost에 있는 .xml파일을 열어서 Context 태그 내에 다음링크를 추가시킨다.
<ResourceLink global="jdbc/myDatabase" name="jdbc/myDatabase"
   type="javax.sql.DataSource" />

3. DAO 클래스에서 getConnection 메서드를 구현해서 사용한다. (예)
private static Connection getConnection() throws NamingException, SQLException{
  InitialContext ctx = new InitialContext();
  Context envCtx = (Context) ctx.lookup("java:comp/env");
  DataSource ds = (DataSource) envCtx.lookup("jdbc/myDatabase");
  return ds.getConnection();
 }