tomcat配置數據源

修改Tomcat_Home/conf/server.xml,在GlobalNamingResources中加入:

Xml代碼

   1. <Resource name="jdbc/DataSource" auth="Container"
   2. type="javax.sql.DataSource" driverClassName="oracle.jdbc.OracleDriver"
   3. url="jdbc:oracle:thin:@127.0.0.1:1521:orcl"
   4. username="user" password="password" maxActive="20" maxIdle="10"
   5. maxWait="-1"/>

 <Resource name="jdbc/DataSource" auth="Container"
  type="javax.sql.DataSource" driverClassName="oracle.jdbc.OracleDriver"
  url="jdbc:oracle:thin:@127.0.0.1:1521:orcl"
  username="user" password="password" maxActive="20" maxIdle="10"
  maxWait="-1"/>

 再在Context中加入引用:

Xml代碼

   1. <ResourceLink name="jdbc/DataSource" global="jdbc/DataSource" type="javax.sql.DataSource"/>

 <ResourceLink name="jdbc/DataSource" global="jdbc/DataSource" type="javax.sql.DataSource"/>

 如果不加,則在使用的時候會出現:Cannot create JDBC driver of class '' for connect URL 'null' 錯誤

 

在web.xml定義:

Xml代碼

   1. <resource-ref>
   2. <description>popuserDataSource</description>
   3. <res-ref-name>jdbc/DataSource</res-ref-name>
   4. <res-type>javax.sql.DataSource</res-type>
   5. <res-auth>Container</res-auth>
   6. </resource-ref>

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

 在Spring中引用:

Xml代碼

   1. <bean id="DataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
   2. <property name="jndiName" value="java:comp/env/jdbc/DataSource"/>
   3. <property name="expectedType" value="javax.sql.DataSource"/>
   4. </bean>

  <bean id="DataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
        <property name="jndiName" value="java:comp/env/jdbc/DataSource"/>
        <property name="expectedType" value="javax.sql.DataSource"/>
    </bean>

 完成。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章