위로 아래

데이터베이스 연결 매니저 생성

public class DBConnector {
	private static DBConnector dbconn = null;
	private DBConnector() {}
	
}

 

데이터베이스 연결 매니저 DBConnector 클래스를 생성하고, dbconn 객체를 private로 선언하고 초기화해준다.

그리고 디폴트 생성자를 선언한다.

DBConnector 객체 생성은 private로 두고 메소드를 통해서 관리한다.

객체를 생성하는 메소드를 작성한다.

public static DBConnector getInstance() {
    if(dbconn==null)
        dbconn = new DBConnector();

    return dbconn;
}

 

DataSource를 연결하는 getConnection 메소드를 작성한다.

public Connection getConnection() throws NamingException, SQLException {
    Context initCtx = new InitialContext();
    Context envCtx = (Context) initCtx.lookup("java:comp/env");
    DataSource ds = (DataSource)envCtx.lookup("jdbc/javaBoard");
    return ds.getConnection();
}

 

 

 

Servers > context.xml 설정

서버부터 분리해준다

아래쪽 Server탭 우클릭 -> new -> 서버 생성

 

Servers > BoardJava-config > context.xml 파일에 데이터베이스 연결 정보 등록

    <Resource auth="Container" 
          name="jdbc/javaBoard" 
          driverClassName="oracle.jdbc.driver.OracleDriver" 
          type="javax.sql.DataSource" 
          url="jdbc:oracle:thin:@127.0.0.1:1521:xe" 
          username="아이디"
          password="비밀번호" 
          loginTimeout="10" 
          maxActive="50" 
          maxIdle="20"
          maxWait="5000" 
          testOnBorrow="true" />

 

 

 

web.xml 설정

web.xml 문서에 resource-ref 작성

<resource-ref>
    <description>javaBoard DB</description>
    <res-ref-name>jdbc/javaBoard</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
</resource-ref>

 

 

 

 


전체 코드

더보기
//DBConnector 클래스
package model;

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 DBConnector {
	private static DBConnector dbconn = null;
	private DBConnector() {}
	
	public static DBConnector getInstance() {
		if(dbconn==null)
			dbconn = new DBConnector();
		return dbconn;
	}
	
	public Connection getConnection() throws NamingException, SQLException{
		Context initCtx = new InitialContext();
		Context envCtx = (Context) initCtx.lookup("java:comp/env");
		DataSource ds = (DataSource)envCtx.lookup("jdbc/javaBoard");
		return ds.getConnection();
	}
}
<!-- web.xml 파일 -->
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">
  <display-name>JavaBoard</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>index.htm</welcome-file>
  </welcome-file-list>
	
	<servlet>
		<servlet-name>Controller</servlet-name>
		<servlet-class>controller.Controller</servlet-class>
		<init-param>
			<param-name>propertyConfig</param-name>
			<param-value>/Users/_o_h_y_o_/Documents/coding/eclipse/JavaBoard/src/main/webapp/WEB-INF/Command.properties</param-value>
		</init-param>
	</servlet>
	
	<servlet-mapping>
    	<servlet-name>Controller</servlet-name>
    	<url-pattern>*.do</url-pattern>
    </servlet-mapping>
    
    <resource-ref>
    	<description>javaBoard DB</description>
    	<res-ref-name>jdbc/javaBoard</res-ref-name>
    	<res-type>javax.sql.DataSource</res-type>
    	<res-auth>Container</res-auth>
    </resource-ref>

	<filter>
	    <filter-name>Encoding Filter</filter-name>
	    <filter-class>filter.CommonEncodingFilter</filter-class>
	    <init-param>
	        <param-name>encoding</param-name>
	        <param-value>UTF-8</param-value>
	    </init-param>
	</filter>
	<filter-mapping>
	    <filter-name>Encoding Filter</filter-name>
	    <url-pattern>/*</url-pattern>
	</filter-mapping>

</web-app>
<!-- Servers > context.xml 파일 -->
<?xml version="1.0" encoding="UTF-8"?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
--><!-- The contents of this file will be loaded for each web application --><Context>

    <!-- Default set of monitored resources. If one of these changes, the    -->
    <!-- web application will be reloaded.                                   -->
    <WatchedResource>WEB-INF/web.xml</WatchedResource>
    <WatchedResource>WEB-INF/tomcat-web.xml</WatchedResource>
    <WatchedResource>${catalina.base}/conf/web.xml</WatchedResource>
    <Resource auth="Container" 
      name="jdbc/JavaBoard" 
      driverClassName="com.mysql.jdbc.Driver" 
      type="javax.sql.DataSource" 
      url="jdbc:mysql://localhost/board" 
      username="root"
      password="70657065" 
      loginTimeout="10" 
      maxActive="50" 
      maxIdle="20"
      maxWait="5000" 
      testOnBorrow="true" />

    <!-- Uncomment this to disable session persistence across Tomcat restarts -->
    <!--
    <Manager pathname="" />
    -->
</Context>