修改/webapps/opencms/WEB-INF/config/opencms.properties文件,首先添加对Northwind数据库的描述
即修改
# Declaration of database pools db.pools=default
为
# Declaration of database pools db.pools=default,Northwind
然后添加对Northwind的描述。如下:
# # Configuration of the default database pool ################################################################################# # name of the JDBC driver db.pool.Northwind.jdbcDriver=net.sourceforge.jtds.jdbc.Driver # URL of the JDBC driver db.pool.Northwind.jdbcUrl=jdbc:jtds:sqlserver://localhost:1433/Northwind # optional parameters for the URL of the JDBC driver db.pool.Northwind.jdbcUrl.params= # user name to connect to the database db.pool.Northwind.user=sa # password to connect to the database db.pool.Northwind.password=123qwe # the URL to make the JDBC DriverManager return connections from the DBCP pool db.pool.Northwind.poolUrl=opencms:Northwind # the maximum number of objects that can be borrowed from the pool db.pool.Northwind.maxActive=25 # the maximum amount of time before throwing an exception when the pool is exhausted db.pool.Northwind.maxWait=2000 # the minimum number of objects that will kept connected db.pool.Northwind.minIdle=3 # the maximum number of objects that can sit idled in the pool db.pool.Northwind.maxIdle=10 # action to take when the pool is exhausted {grow|block|fail} db.pool.Northwind.whenExhaustedAction=block # connections will be validated before they are borrowed from the pool db.pool.Northwind.testOnBorrow=false # connections will be validated by evictor thread db.pool.Northwind.testWhileIdle=true # number of milliseconds to sleep between runs of the evictor thread # -1 means no idle connection evictor thread will be run db.pool.Northwind.timeBetweenEvictionRuns=3600000 # number of connections tested in a run of the evictor thread db.pool.Northwind.numTestsPerEvictionRun=3 # minimum amount of time in milliseconds a connection may be idle in the pool before it is eligable for eviction db.pool.Northwind.minEvictableIdleTime=1800000 # the query to validate connections #db.pool.default.testQuery=SELECT STRUCTURE_ID FROM CMS_OFFLINE_STRUCTURE WHERE RESOURCE_PATH = '/' db.pool.Northwind.testQuery= # number of attempts to connect to the database during startup (default = 10) db.pool.Northwind.connects=10 # sleep time between two attempts to connect to the database during startup in ms (default = 5000) db.pool.Northwind.wait=5000
这样就完成了对Northwind的connection pool的定义。
引用这个connection pool的实例jsp如下:
<%@page import="java.sql.*,org.opencms.db.*,org.opencms.main.*"%> <% java.sql.Connection con = OpenCms.getSqlManager().getConnection("Northwind"); Statement stmt = con.createStatement(); ResultSet rs; rs = stmt.executeQuery("SELECT LastName, FirstName FROM Employees"); while (rs.next()) { %> <%= rs.getString(1) %> <%= rs.getString(2) %><br><% } rs.close(); stmt.close(); con.close(); %>