DBCP连接池的问题
在server.xml中配置如下:
<Context path= "/Register " docBase= "Register " debug= "0 "
crosscontext= "true " reloadable= "true ">
<Resource name= "jdbc/Register_db " auth= "Container "
type= "javax.sql.DataSource "/>
<ResourceParams name= "jdbc/Register_db ">
<parameter>
<name> factory </name>
<value> org.apache.commons.dbcp.BasicDataSourceFactory </value>
</parameter>
<parameter>
<name> username </name>
<value> root </value>
</parameter>
<parameter>
<name> password </name>
<value> root </value>
</parameter>
<parameter>
<name> driverClassName </name>
<value> com.mysql.jdbc.Driver </value>
</parameter>
<parameter>
<name> url </name>
<value> jdbc:mysql://localhost:3306/Register_db?useUnicode = ture&characterEncoding = gb2312 </value>
</parameter>
<parameter>
<name> maxActive </name>
<value> 20 </value>
</parameter>
<parameter>
<name> maxIdle </name>
<value> 5 </value>
</parameter>
<parameter>
<name> maxWait </name>
<value> 10000 </value>
</parameter>
</ResourceParams>
</Context>
在工程中的WEB.XML中配置如下:
<resource-ref>
<description> JNDI JDBC DataSource of Register </description>
<res-ref-name> jdbc/Register_db </res-ref-name>
<res-type> javax.sql.DataSource </res-type>
<res-auth> Container </res-auth>
</resource-ref>
然后在JAVA类中使用如下语句使用连接:
Context initContext = new InitialContext();
Context envContext = (Context)initContext.lookup( "java:/comp/env "); //这里是第38行
DataSource ds = (DataSource)initContext.lookup( "jdbc/Register_db ");
con = ds.getConnection();
return con;
报错:
Exception in thread "main "
javax.naming.NoInitialContextException: Need to specify class name in env