1、web项目数据源可以在web容器中配置,通过添加%Tomcat_Home%/conf/server.xml文件
<Context path="/myWebSite" docBase="F:\java\myWebSite">
<Resource name="shunping"
driverClassName="com.microsoft.jdbc.sqlserver.SQLServerDriver"
url="jdbc:sqlserver://localhost:1433;
databaseName=spdb"
username="sa"
password="sa"
auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"/>
</Context>
然后在项目的web.xml文件中添加
<resource-ref>
<description>DB Connection</description>
<res-ref-name>shunping</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
再把驱动.jar包放到tomcat目录下lib文件夹中
2、Context配置有二种方式
第一种: 在%Tomcat_Home%/conf/server.xml 文件中host标签中配置<Contextpath="/Project"docBase="D:\Workspaces2\Project\WebRoot"reloadable="true"/>其中:
path:指定访问该Web应用的URL入口。
docBase:指定Web应用的文件路径,可以给定绝对路径,也可以给定相对于<Host>的appBase属性的相对路径,如果Web应用采用开放目录结构,则指定Web应用的根目录,如果Web应用是个war文件,则指定war文件的路径。(指定项目所在地址)
reloadable:如果这个属性设为true,tomcat服务器在运行状态下会监视在WEB-INF/classes和WEB-INF/lib目录下class文件的改动,如果监测到有class文件被更新的,服务器会自动重新加载Web应用。
Jndi 数据源或其他资源可直接定在里面,如:
<Context path="/frame" docBase="C:\Java\jboss-4.0.1sp1\server\default\deploy\webroot.war" debug="0"reloadable="false">
<Resource name="UserTransaction"
auth="Container"
type="javax.transaction.UserTransaction"
factory="org.objectweb.jotm.UserTransactionFactory"
jotm.timeout="60" />
<Resource name="sharkdb"
type="javax.sql.DataSource"
password="helloworld"
driverClassName="com.microsoft.jdbc.sqlserver.SQLServerDriver"
maxIdle="2"
maxWait="5000"
username="sa"
url="jdbc:microsoft:sqlserver://192.168.0.193:1433;DatabaseName=test_cyber3;SelectMethod=cursor"
maxActive="4"/>
<Resource name="cyberwaydb"
type="javax.sql.DataSource"
password="helloworld"
driverClassName="com.microsoft.jdbc.sqlserver.SQLServerDriver"
maxIdle="2"
maxWait="5000"
username="sa"
url="jdbc:microsoft:sqlserver://192.168.0.193:1433;DatabaseName=test_cyber3"
maxActive="4"/>
</Context>
在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>
第二种:
在你的工程目录下有个META-INF目录:在其新建一个文件context.xml。该文件中还可以配置数据源。(若同时配置两种则第二种被第一种覆盖)
<?xml version="1.0" encoding="UTF-8"?>
<Context reloadable="true" crossContext="true">
<!-- Default set of monitored resources -->
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<!-- Uncomment this to disable session persistence across Tomcat restarts -->
<!--
<Manager pathname="" />
-->
<Resource name="jiafeng" auth="Container"
type="javax.sql.DataSource"
driverClassName="oracle.jdbc.OracleDriver"
url="jdbc:oracle:thin:@127.0.0.1:1521:mysid"
username="scott"
password="tiger"
maxActive="20"
maxIdle="10"
maxWait="-1"/>
<Logger className="org.apache.catalina.logger.FileLogger" prefix="localhost_DBTest_log." suffix=".txt" timestamp="true" />
<Valve className="org.apache.catalina.valves.AccessLogValve" prefix="localhost_access_log." suffix=".txt" pattern="common" />
</Context>
配置上面之后,把数据库的驱动包放到tomcat的lib目录下,添加项目中web.xml文件
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jiafeng</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
3、使用数据源
Context ct=new InitialContext();
DataSource ds=(DataSource)ct.lookup("java:comp/env/jiafeng");
conn=ds.getConnection();