文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>tomcat配置jndi数据源(mysql)...

tomcat配置jndi数据源(mysql)...

时间:2010-08-14  来源:linwei_1029

1. tomcat中添加jndi数据源,修改server.xml文件中<Context>标签的内容,如下所示

Xml代码
  1. <Context docBase="TestDevon" path="/TestDevon"  
  2.                     reloadable="true" source="org.eclipse.jst.j2ee.server:TestDevon">  
  3.                     <Resource auth="Container" name="jdbc/mysql" type="javax.sql.DataSource" />  
  4.                     <ResourceParams name="jdbc/mysql">  
  5.                         <parameter>  
  6.                             <name>maxWait</name>  
  7.                             <value>5000</value>  
  8.                         </parameter>  
  9.                         <parameter>  
  10.                             <name>maxActive</name>  
  11.                             <value>20</value>  
  12.                         </parameter>  
  13.                         <parameter>  
  14.                             <name>factory</name>  
  15.                             <value>  
  16.                                 org.apache.commons.dbcp.BasicDataSourceFactory   
  17.                             </value>  
  18.                         </parameter>  
  19.                         <parameter>  
  20.                             <name>url</name>  
  21.                             <!-- value>jdbc:derby://localhost:1527/devonoffice?characterEncoding=euckr</value-->  
  22.                             <value>  
  23.                                 jdbc:mysql://localhost:3306/testdevon?autoReconnect=true  
  24.                             </value>  
  25.                         </parameter>  
  26.                         <parameter>  
  27.                             <name>driverClassName</name>  
  28.                             <value>  
  29.                                 com.mysql.jdbc.Driver   
  30.                             </value>  
  31.                         </parameter>  
  32.                         <parameter>  
  33.                             <name>maxIdle</name>  
  34.                             <value>10</value>  
  35.                         </parameter>  
  36.                         <parameter>  
  37.                             <name>username</name>  
  38.                             <value>root</value>  
  39.                         </parameter>  
  40.                         <parameter>  
  41.                             <name>password</name>  
  42.                             <value>lgcnschina</value>  
  43.                         </parameter>  
  44.                     </ResourceParams>  
  45.                 </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代码
  1. <resource-ref>  
  2.     <description>DataSource</description>  
  3.     <res-ref-name>jdbc/mysql</res-ref-name>  
  4.     <res-type>javax.sql.DataSource</res-type>  
  5.     <res-auth>Container</res-auth>  
  6. </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代码
  1. <html>   
  2.     <head>   
  3.         <title>test jndi</title>   
  4.     </head>   
  5.     <body>   
  6. <%    
  7. Context initCtx = new InitialContext();     
  8. Context ctx = (Context) initCtx.lookup("java:comp/env"); DataSource ds = (DataSource) ctx.lookup("jdbc/mysql");    
  9. Connection conn = ds.getConnection();    
  10. Statement stmt = conn.createStatement();    
  11. ResultSet rs = stmt.executeQuery("select * from user_info");    
  12. while (rs.next()) {   
  13. %>   
  14.      <%=rs.getInt(1)%>   
  15. <%   
  16. }    
  17. rs.close();    
  18. stmt.close();    
  19. conn.close();    
  20. %>    
  21.     </body>   
  22. </html>  
相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载