本文转自 :http://blog.sina.com.cn/s/blog_4f925fc30102ed5g.html
jetty插件配置数据源,首先在src/main/resources目录下创建一个名为jetty-env.xml的文件,文件内容如下: <?xmlversion="1.0"?> <!DOCTYPE Configure PUBLIC "-//Mort BayConsulting//DTD Configure//EN""http://jetty.mortbay.org/configure.dtd">
<Configureclass="org.mortbay.jetty.webapp.WebAppContext"> <Newid="DataSource"class="org.mortbay.jetty.plus.naming.Resource"> <Arg> jdbc/ test</Arg>
<Arg> <Newclass="org.apache.commons.dbcp.BasicDataSource"> <Setname="driverClassName">com.mysql.jdbc.Driver</Set> <Setname="url">jdbc:mysql://localhost:3306/appfuse?createDatabaseIfNotExist =true&useUnicode=true&characterEncoding=utf-8</Set> <Setname="username">root</Set> <Setname="password">root</Set> <Setname="maxActive">50</Set> </New> </Arg> </New> </Configure>
tomcat插件配置数据,首先在src/main/resources目录下创建一个名为contextFile.xml的文件,内容如下: <Context> <Resourcename=" jdbc/ test" auth="Container" type="javax.sql.DataSource" username="root" password="root" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/appfuse"/> </Context>
web.xml中的内容如下: <resource-ref> <description>ConnectionPoolDataSourceReference</description> <res-ref-name> jdbc/ test</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref>
JSP测试代码如下: <%@ page contentType="text/html; charset=UTF-8"%> <%@ pageimport="javax.naming.InitialContext,javax.sql.DataSource"%> <html> <head> <title>JNDIdatasource test</title> </head> <body> <h2>数据源初始化成功!</h2> <% InitialContext context = new InitialContext(); DataSourcedataSource = (DataSource)context.lookup("java:comp/env/ jdbc/ test"); %> </body> </html>