tomcat配置jndi数据源(mysql)...
时间:2010-08-14 来源:linwei_1029
1. tomcat中添加jndi数据源,修改server.xml文件中<Context>标签的内容,如下所示
Xml代码- <Context docBase="TestDevon" path="/TestDevon"
- reloadable="true" source="org.eclipse.jst.j2ee.server:TestDevon">
- <Resource auth="Container" name="jdbc/mysql" type="javax.sql.DataSource" />
- <ResourceParams name="jdbc/mysql">
- <parameter>
- <name>maxWait</name>
- <value>5000</value>
- </parameter>
- <parameter>
- <name>maxActive</name>
- <value>20</value>
- </parameter>
- <parameter>
- <name>factory</name>
- <value>
- org.apache.commons.dbcp.BasicDataSourceFactory
- </value>
- </parameter>
- <parameter>
- <name>url</name>
- <!-- value>jdbc:derby://localhost:1527/devonoffice?characterEncoding=euckr</value-->
- <value>
- jdbc:mysql://localhost:3306/testdevon?autoReconnect=true
- </value>
- </parameter>
- <parameter>
- <name>driverClassName</name>
- <value>
- com.mysql.jdbc.Driver
- </value>
- </parameter>
- <parameter>
- <name>maxIdle</name>
- <value>10</value>
- </parameter>
- <parameter>
- <name>username</name>
- <value>root</value>
- </parameter>
- <parameter>
- <name>password</name>
- <value>lgcnschina</value>
- </parameter>
- </ResourceParams>
- </Context>
<Context docBase="TestDevon" path="/TestDevon" reloadable="true" source="org.eclipse.jst.j2ee.server:TestDevon"> <Resource auth="Container" name="jdbc/mysql" type="javax.sql.DataSource" /> <ResourceParams name="jdbc/mysql"> <parameter> <name>maxWait</name> <value>5000</value> </parameter> <parameter> <name>maxActive</name> <value>20</value> </parameter> <parameter> <name>factory</name> <value> org.apache.commons.dbcp.BasicDataSourceFactory </value> </parameter> <parameter> <name>url</name> <!-- value>jdbc:derby://localhost:1527/devonoffice?characterEncoding=euckr</value--> <value> jdbc:mysql://localhost:3306/testdevon?autoReconnect=true </value> </parameter> <parameter> <name>driverClassName</name> <value> com.mysql.jdbc.Driver </value> </parameter> <parameter> <name>maxIdle</name> <value>10</value> </parameter> <parameter> <name>username</name> <value>root</value> </parameter> <parameter> <name>password</name> <value>lgcnschina</value> </parameter> </ResourceParams> </Context>
2. 确认MySql驱动是否存放在tomcat下的common\lib文件夹中
3. 测试数据源的配置是否成功
(1) 在web.xml中添加如下代码
Xml代码- <resource-ref>
- <description>DataSource</description>
- <res-ref-name>jdbc/mysql</res-ref-name>
- <res-type>javax.sql.DataSource</res-type>
- <res-auth>Container</res-auth>
- </resource-ref>
<resource-ref> <description>DataSource</description> <res-ref-name>jdbc/mysql</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref>
(2) 测试数据源
Java代码- <html>
- <head>
- <title>test jndi</title>
- </head>
- <body>
- <%
- Context initCtx = new InitialContext();
- Context ctx = (Context) initCtx.lookup("java:comp/env"); DataSource ds = (DataSource) ctx.lookup("jdbc/mysql");
- Connection conn = ds.getConnection();
- Statement stmt = conn.createStatement();
- ResultSet rs = stmt.executeQuery("select * from user_info");
- while (rs.next()) {
- %>
- <%=rs.getInt(1)%>
- <%
- }
- rs.close();
- stmt.close();
- conn.close();
- %>
- </body>
- </html>
相关阅读 更多 +