首先在application中的WEB-INF/web.xml中添加这样的代码段
<context> ................................ <resource-ref> <res-ref-name>jdbc/hsql</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref> </context>
然后在应用的META-INF文件中添加context.xml文件:
<context> <!-- the connection pool will be bound into JNDI with the name "java:comp/env/jdbc/hsql"--> <Resource name="jdbc/hsql" username="sa" password="" url="jdbc:hsqldb:file:/hsqldb/petsho" auth="Container" defaultAutoCommit="false" driverClassName="org.hsqldb.jdbcDriver" maxActive="20" timeBetweenEvictionRunsMillis="60000" type="javax.sql.DataSource" /> </context>
web.xml和context.xml文件中的配置保持一致.
Context initContext=new InitialContext(); COntext envContext=(Context) initContext.lookup("java:/comp/env"); DataSource ds=(DataSource)envContext.lookup("jdbc/hsql"); Connection conn=ds.getConnection(); ..................................