Tomcat数据源配置中, 查资料都说要在WEB-INF/web.xml 加下面一段 ……
<resource-ref>
<description> DB Connection </description>
<res-ref-name> jdbc/TestDB </res-ref-name>
<res-type> javax.sql.DataSource </res-type>
<res-auth> Container </res-auth>
</resource-ref>
…… 可是我发现不加也可以,到底加不加有什么区别???
看网上只说最好是加上,但是没说为什么,谁帮我解答一下啊
<resource-ref>
<description> DB Connection </description>
<res-ref-name> jdbc/TestDB </res-ref-name>
<res-type> javax.sql.DataSource </res-type>
<res-auth> Container </res-auth>
</resource-ref>
…… 可是我发现不加也可以,到底加不加有什么区别???
看网上只说最好是加上,但是没说为什么,谁帮我解答一下啊
Context intitCtx = new InitialContext();
Context envCtx = (Context)intitCtx.lookup("java:comp/env");//从JNDI取出java环境上下文对象
DataSource ds = (DataSource)envCtx.lookup("jdbc/pro");//取数据源
Connection conn = ds.getConnection();//从连接池获取连接对象 下面是解释,网上找的。
resource-ref元素用于指定对外部资源的servlet引用的声明。
<!ELEMENT resource-ref (description?, res-ref-name,
res-type, res-auth, res-sharing-scope?)>
<!ELEMENT description (#PCDATA)>
<!ELEMENT res-ref-name (#PCDATA)>
<!ELEMENT res-type (#PCDATA)>
<!ELEMENT res-auth (#PCDATA)>
<!ELEMENT res-sharing-scope (#PCDATA)>
resource-ref子元素的描述如下:
● res-ref-name是资源工厂引用名的名称。该名称是一个与java:comp/env上下文相对应的JNDI名称,并且在整个Web应用中必须是惟一的。
● res-auth表明:servlet代码通过编程注册到资源管理器,或者是容器将代表servlet注册到资源管理器。该元素的值必须为Application或Container。
● res-sharing-scope表明:是否可以共享通过给定资源管理器连接工厂引用获得的连接。该元素的值必须为Shareable(默认值)或Unshareable。
<description> DB Connection </description>
<res-ref-name> jdbc/TestDB </res-ref-name>
<res-type> javax.sql.DataSource </res-type>
<res-auth> Container </res-auth>
</resource-ref>
这正是第二种。3.通过写一个数据库访问类连接数据库。
如果你是通过读取配置文件来得到数据库连接那就一定要在WEB.XML中配置,如果你使用 的是第一种方法只需要在TOMCAT中配置就好了
<resource-ref>
<description> DB Connection </description>
<res-ref-name> jdbc/TestDB </res-ref-name>
<res-type> javax.sql.DataSource </res-type>
<res-auth> Container </res-auth>
</resource-ref>
这正是第二种。
那么这个第二种方式具体是如何操作的呢。读取的配置文件时什么呢?这个文件写在哪里!