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”>
<Context path=”” docBase=”X:\myAPP\source\jsp\FHMS\WEB_ROOT” debug=”0″ reloadable=”true”>
<!– FHMS DB Setting –>
<Resource name=”jdbc/fhms” auth=”Container” type=”javax.sql.DataSource”/>
<ResourceParams name=”jdbc/fhms“>
<parameter>
<name>username</name>
<value>webuser</value>
</parameter>
<parameter>
<name>password</name>
<value>adlf@reawt*d*</value>
</parameter>
<parameter>
<name>driverClassName</name>
<value>com.microsoft.sqlserver.jdbc.SQLServerDriver</value>
</parameter>
<parameter>
<name>url</name>
<value>jdbc:sqlserver://192.168.0.2:1433;databasename=fhms;SelectMethod=cursor;</value>
</parameter>
<parameter>
<name>removeAbandoned</name>
<value>true</value>
</parameter>
<parameter>
<name>removeAbandonedTimeout</name>
<value>60</value>
</parameter>
<parameter>
<name>logAbandoned</name>
<value>true</value>
</parameter>
<parameter>
<name>maxActive</name>
<value>25</value>
</parameter>
<parameter>
<name>maxIdle</name>
<value>10</value>
</parameter>
<parameter>
<name>maxWait</name>
<value>-1</value>
</parameter>
<parameter>
<name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter>
</ResourceParams>
<ResourceLink name=”jdbc/fhms” global=”jdbc/fhms” type=”javax.sql.DataSource”/>
</Context>
</Host>
SQL Server 2000 드라이버를 사용할 경우 위 내용 중 driverClassName와 url을 다음과 같이 수정
<name>driverClassName</name>
<value>com.microsoft.jdbc.sqlserver.SQLServerDriver</value>
</parameter>
<parameter>
<name>url</name>
<value>jdbc:microsoft:sqlserver://192.168.0.2:1433;databasename=fhms;SelectMethod=cursor;</value>
</parameter>
*. 참고: 아래 사항은 <Context></Context> 사이에 넣지 않고, <GlobalNamingResources>안에 넣어도 됨
….
</ResourceParams>
3. 사용 예
xxx.jsp의 소스 코드
<%
try
{
Context ctx = new InitialContext();
DataSource ds = (DataSource)ctx.lookup(“java:comp/env/jdbc/fhms“);
Connection conn = ds.getConnection();
String sid = “A3DE42F”;
PreparedStatement pstmt = conn.prepareStatement(“SELECT * FROM sensor WHERE sid = ?”);
pstmt.setString(1, sid );
ResultSet rs = pstmt.executeQuery();
while (rs.next())
{
out.println(rs.getString(“name”));
}
if (pstmt != null)
pstmt.close();
if (rs != null)
rs.close();
if (conn != null)
conn.close();
}
catch(Exception e)
{
out.println(e);
}
4. 에러 메시지에 따른 문제 해결 방법
org.apache.commons.dbcp.SQLNestedException: Cannot load JDBC driver class ‘com.microsoft.sqlserver.jdbc.SQLServerDriver’
-> sqljdbc.jar을 c:\Program Files\tomcat\common\lib\에 복사
javax.naming.NameNotFoundException: Name jdbc is not bound in this Context
-> server.xml
<Context path=”” docBase=”X:\myAPP\source\jsp\FHMS\WEB_ROOT” debug=”0″ reloadable=”true”>
<ResourceLink name=”jdbc/fhms” global=”jdbc/fhms” type=”javax.sql.DataSource”/>
</Context>
참고 웹페이지
1. http://tong.nate.com/bbottlejo/43172210
2. http://kin.naver.com/detail/detail.php?d1id=1&dir_id=10106&eid=Ppn9AJtqrH2Aj0jRlvKNHj1k9CIND1EQ&qb=bG9va3VwKCJqYXZhOg==
3. http://annehouse.tistory.com/249
4. http://blog.naver.com/jyoung96?Redirect=Log&logNo=110001673012