<b1,所需要的jar文件:commons-pool.jar, commons-dbcp.jar</b
<b2,server.xml的配置:</b
<Resource name="jdbc/hellohibernate" scope="Shareable" auth="Container"
type="javax.sql.DataSource"/
<ResourceParams name="jdbc/hellohibernate"
<parameter
<namefactory</name
<valueorg.apache.commons.dbcp.BasicDataSourceFactory</value
</parameter
<parameter
<nameremoveAbandoned</name
<valuetrue</value
</parameter
<parameter
<namelogAbandoned</name
<valuetrue</value
</parameter
<!-- DBCP database connection settings --
<parameter
<nameurl</name
<valuejdbc:mysql://localhost:3306/test</value
</parameter
<parameter
<namedriverClassName</name<!--<valuecom.mysql.jdbc.Driver</value--
<valueorg.gjt.mm.mysql.Driver</value
</parameter
<parameter
<nameusername</name
<valueroot</value
</parameter
<parameter
<namepassword</name
<value</value
</parameter
<!-- DBCP connection pooling options --
<parameter
<namemaxWait</name
<value3000</value
</parameter
<parameter
<namemaxIdle</name
<value100</value
</parameter
<parameter
<namemaxActive</name
<value10</value
</parameter
</ResourceParams
<b3,程序中的调用:</b
Context ctx = new InitialContext();
if (ctx == null)
throw new Exception("Boom - No Context");
DataSource ds =
(DataSource) ctx.lookup(
"java:comp/env/jdbc/hellohibernate");
if (ds != null) {
Connection conn = ds.getConnection();
if (conn != null) {
foo = "Got Connection " + conn.toString();
Statement stmt = conn.createStatement();
ResultSet rst =
stmt.executeQuery(
"select username,id from user");
if (rst.next()) {
foo = rst.getString(1);
bar = rst.getInt(2);
}
conn.close();
}
}