一
.把数据库JDBC驱动拷贝到
%TOMCAT_HOME%/common/lib
或
%TOMCAT_HOME%/webapps/yourweb/WEB-INF/lib 下;
2.修改tomcat安装目录下conf/server.xml,在Host元素下添加一个Context元素,示例:
< Context path="/TomcatPool" docBase="E:Tomcat 5.5webappsTomcatPool" debug="1" reloadable="true"> < Resource name="jdbc/ds" type="javax.sql.DataSource" driverClassName="com.microsoft.jdbc.sqlserver.SQLServerDriver" url="jdbc:microsoft:sqlserver://localhost:1433;databasename=pubs" username="sa" password="sa" maxWait="5000" maxActive="4" maxIdle="4"/> < /Context> |
< resource-ref> < description>sqlserverDB connection< /description> < res-ref-name>jdbc/ds< /res-ref-name> < res-type>javax.sql.DataSource< /res-type> < res-auth>Container< /res-auth> < /resource-ref> |
javax.naming.Context context = new javax.naming.InitialContext();
javax.sql.DataSource ds = (javax.sql.DataSource)context.lookup("java:comp/env/jdbc/ds");
java.sql.Connection conn = ds.getConnection();
对于Tomcat 6,步骤为: