728x90

Tomcat 5.0.28에서 DataSource를 설정하여 DB Connection을 얻어오는데

해당 Exception이 발생하였습니다. (^_^);;

일단 DataSource를 설정하기 우해서

server.xml에 해당 내용을 추가해 주어야 합니다.

=======================================================================================

server.xml

<!-- Global JNDI resources -->
<GlobalNamingResources>
<!-- Test entry for demonstration purposes -->
<Environment name="simpleValue" type="java.lang.Integer" value="30"/>
<!-- Editable user database that can also be used by UserDatabaseRealm to authenticate users -->
<Resource name="UserDatabase" auth="Container" type="org.apache.catalina.UserDatabase" description="User database that can be updated and saved">
</Resource>
<ResourceParams name="UserDatabase">
<parameter>
<name>factory</name>
<value>org.apache.catalina.users.MemoryUserDatabaseFactory</value>
</parameter>
<parameter>
<name>pathname</name>
<value>conf/tomcat-users.xml</value>
</parameter>
</ResourceParams>
<!-- Added DataSource -->
<Resource name="WangDB" type="javax.sql.DataSource"/>
<ResourceParams name="WangDB">
<parameter>
<name>validationQuery</name>
<value>select * from emp</value>
</parameter>
<parameter>
<name>maxWait</name>
<value>5000</value>
</parameter>
<parameter>
<name>maxActive</name>
<value>4</value>
</parameter>
<parameter>
<name>password</name>
<value>tiger</value>
</parameter>
<parameter>
<name>url</name>
<value>jdbc:oracle:thin:@10.132.3.90:1521:ORCL</value>
</parameter>
<parameter>
<name>driverClassName</name>
<value>oracle.jdbc.driver.OracleDriver</value>
</parameter>
<parameter>
<name>maxIdle</name>
<value>2</value>
</parameter>
<parameter>
<name>username</name>
<value>scott</value>
</parameter>
</ResourceParams>
</GlobalNamingResources>

=======================================================================================

web.xml

<!-- Added DataSource -->

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

---------------------------------------------------------------------------------------

위의 방법은 http://jakarta.apache.org/tomcat/tomcat-5.0-doc/jndi-datasource-examples-howto.html 에서 참고하실 수 있습니다.

하지만, 이렇게 하시고 실제로 DataSource를 불러오시면

Cannot create JDBC driver of class '' for connect URL 'null'

라는 Exception이 발생합니다.

이는 JDBC Driver를 연결한 URL을 찾을 수 없다는 이야기 인데

server.xml에

해당 Context를 추가해 주고

여기에 Resource를 링크해 주면 됩니다.

=======================================================================================

server.xml

<Host name="localhost" debug="0" appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false">

<!-- 여기서 부터 ADD -->
<Context className="org.apache.catalina.core.StandardContext"
cachingAllowed="true"
charsetMapperClass="org.apache.catalina.util.CharsetMapper"
cookies="true"
crossContext="false"
debug="0"
displayName="Welcome to Tomcat"
docBase="C:\Project\CustomsSR\SR"
mapperClass="org.apache.catalina.core.StandardContextMapper"
path="/SR"
privileged="false"
reloadable="false"
swallowOutput="false"
useNaming="true"
wrapperClass="org.apache.catalina.core.StandardWrapper">
<ResourceLink name="WangDB" global="WangDB" type="javax.sql.DataSource"/>
</Context>

---------------------------------------------------------------------------------------

TEST Page는 이렇게 만드시면 됩니다.

=======================================================================================

test.jsp

<%@ page contentType="text/html;charset=MS949"%>
<%@ page import="javax.naming.Context"%>
<%@ page import="javax.naming.InitialContext"%>
<%@ page import="javax.naming.NamingException"%>
<%@ page import="javax.sql.DataSource"%>
<%@ page import="java.sql.Connection"%>
<%@ page import="java.sql.Statement"%>
<%@ page import="java.sql.ResultSet"%>
<%@ page import="java.sql.SQLException"%>
<%
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try{
Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
DataSource ds = (DataSource) envCtx.lookup("WangDB");
conn = ds.getConnection();
stmt = conn.createStatement();
rs = stmt.executeQuery("select count(*) from emp");
if(rs.next()) {
out.println("Value : ");
out.println(rs.getString(1));
out.println("<BR>");
}
}catch(NamingException ne){
out.println(ne.toString());

}catch(SQLException se){
out.println(se.toString());

}finally{
try { if (rs != null) rs.close(); } catch (Exception e) {}
try { if (stmt != null) stmt.close(); } catch (Exception e) {}
try { if (conn != null) conn.close(); } catch (Exception e) {}
}
%>

[출처] Cannot create JDBC driver of class '' for connect URL 'null' |작성자 여우아저씨

위의 경우에도 해결이 되지 않을경우에는

context.xml 에

resource 부분을 넣어보자

<Resource name="jdbc/db" auth="Container"
type="javax.sql.DataSource" driverClassName="oracle.jdbc.driver.OracleDriver"
url="jdbc:oracle:thin:@localhost:1521:orcl"
username="db"
password="db"
maxActive="5"
maxIdle="3"
maxWait="-1"/>

728x90

'WEB' 카테고리의 다른 글

HTML SELECT OPTION 추가  (0) 2012.07.29
HTML SELECT OPTION  (0) 2012.07.29
HTML TEXTAREA 글자수 제한  (0) 2012.07.29
스트러츠 아스키코드 html태그로 입력받기  (0) 2012.07.29
css !important  (0) 2012.07.29

+ Recent posts