blogjava's web log

blogjava's web log
...

tomcat配置jndi数据源小记

 

1.在server.xml中加入。
这可以登陆后台配置。。

  <!--   configure DataSource. Add the following code into server.xml   -->  
 
 
<  Context  path  = " /test "   docBase  = "test "   debug  = " 0 "  
reloadable 
= " true "     >  
 
 
   
<  Resource  name  = " jdbc/first "  
               auth 
= " Container "  
               type 
= " javax.sql.DataSource "     />  
 
   
<  ResourceParams  name  = " jdbc/first "   >  
     
<  parameter  >  
       
<  name  >  factory  </  name  >  
       
<  value  >  org.apache.commons.dbcp.BasicDataSourceFactory  </  value  >  
     
</  parameter  >  
 
     
<!--   Maximum number of dB connections in pool. Make sure you
         configure your mysqld max_connections large enough to handle
         all of your db connections. Set to  0   for  no limit.
          
-->  
     
<  parameter  >  
       
<  name  >  maxActive  </  name  >  
       
<  value  >  100  </  value  >  
     
</  parameter  >  
 
     
<!--   Maximum number of idle dB connections to retain in pool.
         Set to  0   for  no limit.
          
-->  
     
<  parameter  >  
       
<  name  >  maxIdle  </  name  >  
       
<  value  >  30  </  value  >  
     
</  parameter  >  
 
     
<!--   Maximum time to wait  for  a dB connection to become available
         in ms, in  this  example  10  seconds. An Exception is thrown  if 
          this  timeout is exceeded.  Set to  - 1  to wait indefinitely.
        Maximum time to wait  for  a dB connection to become available
         in ms, in  this  example  10  seconds. An Exception is thrown  if 
          this  timeout is exceeded.  Set to  - 1  to wait indefinitely.
          
-->  
     
<  parameter  >  
       
<  name  >  maxWait  </  name  >  
       
<  value  >  10000  </  value  >  
     
</  parameter  >  
 
     
<!--   MySQL dB username and password  for  dB connections    -->  
     
<  parameter  >  
      
<  name  >  username  </  name  >  
      
<  value  >  dbuser  </  value  >  
     
</  parameter  >  
     
<  parameter  >  
      
<  name  >  password  </  name  >  
      
<  value  >  1234  </  value  >  
     
</  parameter  >  
 
     
<!--   Class name  for  mm.mysql JDBC driver   -->  
     
<  parameter  >  
        
<  name  >  driverClassName  </  name  >  
        
<  value  >  com.mysql.jdbc.Driver  </  value  >  
     
</  parameter  >  
 
     
<!--   The JDBC connection url  for  connecting to your MySQL dB.
         The autoReconnect = true  argument to the url makes sure that the
         mm.mysql JDBC Driver will automatically reconnect  if  mysqld closed the
         connection.  mysqld by  default  closes idle connections after  8  hours.
          
-->  
     
<  parameter  >  
       
<  name  >  url  </  name  >  
       
<  value  >  jdbc:mysql: // localhost:3306/test?autoReconnect=true </ value >  
      
</  parameter  >  
   
</  ResourceParams  >  
 
 
</  Context  >  

在工程web-xml中加入以下代码

    
<resource-ref>
          
<description>DB Connection</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 import="java.util.*"%>
<%@ page import="java.sql.*"%>
<%@ page import="javax.sql.*"%>
<%@ page import="javax.naming.*"%>
<%@ page import="com.mysql.jdbc.Connection"%>

访问代码


//建立数据库连接
Context ctx = new InitialContext();
DataSource ds 
=(DataSource)ctx.lookup("java:comp/env/jdbc/test");
con 
= ds.getConnection();
//创建一个JDBC声明
stmt = con.createStatement();

//有了连接。。就可以做很多事了。。


注意把mysql驱动拷到comcat common/lib下面..


目标:一天整理一点东西。。虽然简单。。但记下来还是有好处的。。

posted on 2006-05-12 10:29 record java and net 阅读(595) 评论(0)  编辑  收藏 所属分类: Jsp&&Web


只有注册用户登录后才能发表评论。


网站导航:
 

导航

常用链接

留言簿(44)

新闻档案

2.动态语言

3.工具箱

9.文档教程

友情链接

搜索

最新评论