tips-programming jndi SQL Server tomcat

1. jdbc 드라이버 파일 복사
SQL Server 2005용 jdbc 드라이버 파일 sqljdbc.jar를 c:\Program Files\tomcat\common\lib\에 복사
SQL Server 2000일 경우 msutil.jar, msbase.jar, mssqlserver.jar

2. server.xml 설정
파일 위치: c:\Program Files\tomcat\conf\server.xml

unpackWARs=”true” autoDeploy=”true”
xmlValidation=”false” xmlNamespaceAware=”false”>


   
    jdbc/fhms” auth=”Container” type=”javax.sql.DataSource”/>
    jdbc/fhms“>
    
        username
        webuser
    

    
        password
        adlf@reawt*d*
    

    
      driverClassName
      com.microsoft.sqlserver.jdbc.SQLServerDriver
    

    
      url
      jdbc:sqlserver://192.168.0.2:1433;databasename=fhms;SelectMethod=cursor;
    

    
      removeAbandoned
      true
    

    
      removeAbandonedTimeout
      60
    

    
      logAbandoned
      true
    

    
      maxActive
      25
    

    
      maxIdle
      10
    

    
      maxWait
      -1
    

    
        factory
        org.apache.commons.dbcp.BasicDataSourceFactory
    

   

    jdbc/fhms” global=”jdbc/fhms” type=”javax.sql.DataSource”/>

SQL Server 2000 드라이버를 사용할 경우 위 내용 중 driverClassName와 url을 다음과 같이 수정


  driverClassName
  com.microsoft.jdbc.sqlserver.SQLServerDriver


  url
  jdbc:microsoft:sqlserver://192.168.0.2:1433;databasename=fhms;SelectMethod=cursor;

<p>
  *. 참고: 아래 사항은 <Context></Context> 사이에 넣지 않고, <GlobalNamingResources>안에 넣어도 됨
</p>

<div style="padding: 10px; background-color: rgb(228, 228, 228);">
  <Resource name=&#8221;<span style="background-color: rgb(208, 255, 157);">jdbc/fhms</span>&#8221; auth=&#8221;Container&#8221; type=&#8221;javax.sql.DataSource&#8221;/><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &#8230;.<br /></ResourceParams>
</div>

<p>
  <span style="font-weight: bold;">3. 사용 예</span>
</p>

<p>
  xxx.jsp의 소스 코드
</p>

<div style="padding: 10px; background-color: rgb(228, 228, 228);">
  <%@ page import=&#8221;<span style="color: rgb(255, 0, 0);">java.sql.*, javax.naming.*, javax.sql.*, java.util.*</span>&#8221; contentType=&#8221;text/html;charset=euc-kr&#8221; %><br /><%<br />try<br />{<br /><span style="color: rgb(255, 0, 0);">&nbsp; &nbsp; Context ctx = new InitialContext();</span><br style="color: rgb(255, 0, 0);" /><span style="color: rgb(255, 0, 0);">&nbsp; &nbsp; DataSource ds = (DataSource)ctx.lookup(&#8220;java:comp/env/</span><span style="background-color: rgb(208, 255, 157); color: rgb(255, 0, 0);">jdbc/fhms</span><span style="color: rgb(255, 0, 0);">&#8220;);</span><br style="color: rgb(255, 0, 0);" /><span style="color: rgb(255, 0, 0);">&nbsp; &nbsp; Connection conn = ds.getConnection();</span></p> 
  
  <p>
    &nbsp; &nbsp; String sid = &#8220;A3DE42F&#8221;;<br />&nbsp; &nbsp; PreparedStatement pstmt = conn.prepareStatement(&#8220;SELECT * FROM sensor WHERE sid = ?&#8221;);<br />&nbsp; &nbsp; pstmt.setString(1,&nbsp; sid );
  </p>
  
  <p>
    &nbsp; &nbsp; ResultSet rs = pstmt.executeQuery();
  </p>
  
  <p>
    &nbsp; &nbsp; while (rs.next())<br />&nbsp; &nbsp; {<br />&nbsp; &nbsp; &nbsp; &nbsp; out.println(rs.getString(&#8220;name&#8221;));<br />&nbsp; &nbsp; }
  </p>
  
  <p>
    &nbsp; &nbsp; if (pstmt != null)<br />&nbsp; &nbsp; &nbsp; &nbsp; pstmt.close();<br />&nbsp; &nbsp; if (rs != null)<br />&nbsp; &nbsp; &nbsp; &nbsp; rs.close();<br />&nbsp; &nbsp; if (conn != null)<br />&nbsp; &nbsp; &nbsp; &nbsp; conn.close();<br />}<br />catch(Exception e)<br />{<br />&nbsp; &nbsp; out.println(e);<br />}</div> 
    
    <p>
      <span style="font-weight: bold;">4. 에러 메시지에 따른 문제 해결 방법</span>
    </p>
    
    <p>
      <span style="background-color: rgb(255, 218, 237);">org.apache.commons.dbcp.SQLNestedException: Cannot load JDBC driver class &#8216;com.microsoft.sqlserver.jdbc.SQLServerDriver&#8217; </span>
    </p>
    
    <blockquote>
      <p>
        -> sqljdbc.jar을 c:\Program Files\tomcat\common\lib\에 복사
      </p>
    </blockquote>
    
    <p>
      <span style="background-color: rgb(255, 218, 237);">javax.naming.NameNotFoundException: Name jdbc is not bound in this Context </span>
    </p>
    
    <blockquote>
      <p>
        -> server.xml<br />&nbsp; &nbsp; &nbsp; &nbsp; <Context path=&#8221;&#8221; docBase=&#8221;X:\myAPP\source\jsp\FHMS\WEB_ROOT&#8221; debug=&#8221;0&#8243; reloadable=&#8221;true&#8221;><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <ResourceLink name=&#8221;jdbc/fhms&#8221; global=&#8221;jdbc/fhms&#8221; type=&#8221;javax.sql.DataSource&#8221;/><br />&nbsp; &nbsp; &nbsp; &nbsp; </Context>
      </p>
    </blockquote>
    
    <p>
      참고 웹페이지<br />1. http://tong.nate.com/bbottlejo/43172210<br />2. http://kin.naver.com/detail/detail.php?d1id=1&dir_id=10106&eid=Ppn9AJtqrH2Aj0jRlvKNHj1k9CIND1EQ&qb=bG9va3VwKCJqYXZhOg==<br />3. http://annehouse.tistory.com/249<br />4. http://blog.naver.com/jyoung96?Redirect=Log&logNo=110001673012
    </p>
    
    <p>
    </p>