回 帖 发 新 帖 刷新版面

主题:tomcate4.1.29+mysql 数据库连接池配置问题(请各位老大指点一二,小弟不胜感激!)

server.xml文件有关连接池配置信息:  
<Context  path=""  docBase="ROOT"  debug="0">  
                                             
                   <Resource  name="jdbc/MysqlDB"  auth="Container"  
                                             
                   type="javax.sql.DataSource"/>    
                                                           
                   <ResourceParams  name="jdbc/MysqlDB">  
                                   <parameter>  
                                                                         
                   <name>factory</name>  
                                                                         
                   <value>org.apache.commons.dbcp.BasicDataSourceFactory</value>  
                                                           
                       </parameter>  
                                                           
                       <parameter>  
                                                                         
                   <name>driverClassName</name>  
                                                                         
                   <value>org.gjt.mm.mysql.Driver</value>  
                                                           
                       </parameter>  
                                                           
                       <parameter>  
                                                                         
                   <name>url</name>  
                                                                         
                   <value>jdbc:mysql://localhost:3306/mytest</value>  
                                                           
                       </parameter>  
                                                           
                       <parameter>  
                                                                         
                   <name>username</name>  
                                                                         
                   <value>root</value>  
                                                           
                       </parameter>  
                                                           
                       <parameter>  
                                                                         
                   <name>password</name>  
                                                                         
                   <value></value>  
                                                           
                       </parameter>  
                                                           
                       <parameter>  
                                                                         
                   <name>maxActive</name>  
                                                                         
                   <value>20</value>  
                                                           
                       </parameter>  
                                                           
                       <parameter>  
                                                                         
                   <name>maxIdle</name>  
                                                                         
                   <value>10</value>  
                                                           
                       </parameter>  
                                                           
                       <parameter>  
                                                                         
                   <name>maxWait</name>  
                                                                         
                   <value>10000</value>  
                                                           
                       </parameter>  
                                                           
                   </ResourceParams>  
                                             
                   </Context>  

在我web工程下的web.xml文件中增加了以下信息:  
<resource-ref>  
   <description>Oracle  Datasource  example</description>  
   <res-ref-name>jdbc/MysqlDB</res-ref-name>  
   <res-type>javax.sql.DataSource</res-type>  
   <res-auth>Container</res-auth>  
</resource-ref>  

mysql的驱动我也放到了工程的lib目录下,以下是在java类中的代码:  
                       Context  initCtx  =  new  InitialContext();  
                       Context  ctx  =  (Context)  initCtx.lookup("java:comp/env");  
                       //获取连接池对象  
                       Object  obj  =  (Object)  ctx.lookup("jdbc/MysqlDB");  
                       //类型转换  

                       javax.sql.DataSource  ds  =  (javax.sql.DataSource)  obj;  
                       conn  =  ds.getConnection();  

而执行这段代码的时候总是报这样的错:org.apache.commons.dbcp.SQLNestedException:  Cannot  create  JDBC  driver  of  class  ''  for  connect  URL  'null',  cause:    
java.sql.SQLException:  No  suitable  driver 

回复列表 (共3个回复)

沙发

Context  initCtx  =  new  InitialContext();  
                       Context  ctx  =  (Context)  initCtx.lookup("java:comp/env");  
                       //获取连接池对象  
                       Object  obj  =  (Object)  ctx.lookup("jdbc/MysqlDB");  
                       //类型转换  

                       javax.sql.DataSource  ds  =  (javax.sql.DataSource)  obj;  
                       conn  =  ds.getConnection();  


改为:
try
{
Context initCtx = new InitialContext();
if(initCtx==null)
{
  throw new Exception("没有找到数据库资源");
}
javax.sql.DataSource ds=(DataSource)initCtx.lookup("java:comp/env/jdbc/MysqlDB");
if(ds==null)
{
   throw new Exception("没有找到匹配的数据库 ");
}
conn=ds.getConnection();
}
catch(Exception e)
{
.........
}

..........

板凳

首先谢谢你给我回复,但这样只是做了一个错误处理呀,实际上还是没有解决问题的。

3 楼

改天我把我配置好的mysql的数据库连接池直接发给你!

我来回复

您尚未登录,请登录后再回复。点此登录或注册