处理获取数据库连接时的经典异常:Cannot create PoolableConnectionFactory

Exception:

从连接池中获取数据库连接发生异常 : Cannot create PoolableConnectionFactory (Illegal connection port value '3306:DB)

Solution:

1.在Tomcat中server.xml的<GlobalNamingResources> 节点里面添加如下代码:
    <Resource name="jdbc/mbss" auth="Container" type="javax.sql.DataSource"
    username="root"
    password="root"
    driverClassName="com.mysql.jdbc.Driver"
    maxIdle="10"
    maxWait="10000"
    maxActive="100"
    url="jdbc:mysql://localhost:3306/DB" />


2.在Tomcat中context.xml的</context>前添加如下代码:
    <ResourceLink name="jdbc/DB" global="jdbc/DB" type="javax.sql.DataSourcer"/>

3.在所建项目中web.xml的</web-app>前添加如下代码:

    <resource-ref>
        <description>DB Connection</description>
        <res-ref-name>jdbc/mbss</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
    </resource-ref>

4.在获得数据库连接池中这样写,如:

扫描二维码关注公众号,回复: 565553 查看本文章

       Context initCtx = new InitialContext();
       Context envCtx = (Context) initCtx.lookup("java:comp/env");
       DataSource ds = (DataSource) envCtx.lookup("jdbc/DB");
       con = ds.getConnection();

猜你喜欢

转载自kayonlife.iteye.com/blog/1995081