如果不使用spring注入的话,程序中可以这样取得数据源: Context ctx = new InitalContext(); DataSource ds = (DataSource)ctx.lookup("java:comp/env/DB2source"); //获得数据库连接对象: Connection con = ds.getConnection(); …………
1.tomcat5.5重装 2。在server.xml加上如下 <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" factory="org.apache.catalina.users.MemoryUserDatabaseFactory" pathname="conf/tomcat-users.xml" /> <!-- start --> <Resource name="jdbc/SIMDB" auth="Container" type="javax.sql.DataSource" driverClassName="oracle.jdbc.OracleDriver" url="jdbc:oracle:thin:@127.0.0.1:1521:xe" username="SIMDB" password="111111" maxActive="20" maxIdle="10" maxWait="-1"/>
<Resource name="jdbc/DB2source"
type="javax.sql.DataSource"
url="jdbc:db2://127.0.0.1:50000/db1"
username="xxxx"
password="xxxx"
driverClassName="com.ibm.db2.jcc.DB2Driver"
maxIdle="3"
maxWait="-1"
maxActive="50" />
</Context>以上是tomcat5.5及以后版本的写法,根据自己的项目改成适当的值。
在\tomcat-5.5.23\conf\Catalina\localhost下建一个XML文件,名字随意,内容的话,就是参考1楼的八爪鱼兄弟的。
用spring的话,还需要在spring配置文件中配置:
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="resourceRef">
<value>true</value>
</property>
<property name="jndiName">
<value>jdbc/DB2source</value>
</property>
</bean>
docBase="E:\workspace\proj/webroot" 是指:你开发的项目在你硬盘中的实际路径,注意要指项目的web目录
workDir="E:\workspace\proj\work"是指:你项目中的jsp文件的编译地址,这个不配置的话,默认是会指向tomcat安装目录的work下OK?
<Context path="/springDemo" reloadable="true" docBase="D:\crm\springDemo\web">
<Resource name="jdbc/DB2source"
type="javax.sql.DataSource"
url="jdbc:oracle:thin:@180.1.4.28:1521:camcdc"
username="app"
password="fenet"
driverClassName="oracle.jdbc.driver.OracleDriver"
maxIdle="3"
maxWait="-1"
maxActive="50" />
</Context>在配置文件里
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean" destroy-method="close">
<property name="resourceRef">
<value>true</value>
</property>
<property name="jndiName">
<value>jdbc/DB2source</value>
</property>
</bean>
这样配置,其他还需要设置什么吗?
看你的项目名称,应该是用了spring
Context ctx = new InitalContext();
DataSource ds = (DataSource)ctx.lookup("java:comp/env/DB2source");
//获得数据库连接对象:
Connection con = ds.getConnection();
…………
2。在server.xml加上如下
<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"
factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
pathname="conf/tomcat-users.xml" />
<!-- start -->
<Resource name="jdbc/SIMDB" auth="Container"
type="javax.sql.DataSource" driverClassName="oracle.jdbc.OracleDriver"
url="jdbc:oracle:thin:@127.0.0.1:1521:xe"
username="SIMDB" password="111111" maxActive="20" maxIdle="10"
maxWait="-1"/>
<!-- end -->
</GlobalNamingResources>