Tomcat5.5数据源配置
发布: 2008-12-04 12:44 | 作者: webmaster | 来源: 本站原创 | 查看: 11次
1. Context configuration
tomcat\conf\server.xml文件配置
|
<Context path="/项目名称" docBase="项目实际路径" debug="5" reloadable="true" crossContext="true"> </Context> |
2. web.xml configuration
项目web.xml文件配置
| <resource-ref> <description>名称(任意)</description> <res-ref-name>JNDI名称(同上)</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref> |
3. Code example
获取连接类
| Context initContext = new InitialContext(); Context envContext = (Context)initContext.lookup("java:/comp/env"); //"java:/comp/env"Tomcat中在上下文目录中的定位 DataSource ds = (DataSource)envContext.lookup("JNDI名称(同上)"); Connection conn = ds.getConnection();//创建Connection连接 |
使用数据源的好处:
能够减少数据库打开和关闭的操作次数,节约资源












