AW: [opencms-dev] REPOST: How to use DB connections/prepared stat ements in OpenCMS?

Andreas Schouten Andreas.Schouten at framfab.de
Mon May 14 11:35:59 CEST 2001


Hi,

please have a look into the sourcecode of the news-module. It uses the
opencms-connection pool:

http://www.opencms.com/cgi-bin/cvsweb.cgi/opencms/modules/news/src/com/openc
ms/modules/homepage/news/NewsChannelContentDefinition.java?rev=1.3&content-t
ype=text/x-cvsweb-markup


You can use it like this:

Connection con = null;
PreparedStatement statement = null;
ResultSet res = null;
String poolname = "jdbc:opencmspool:mysql";
try {
	con = DriverManager.getConnection(poolname);
	statement = con.prepareStatement("select * from test");
	res = statement.executeQuery();
	if(res.next()){
		// read the items from the resultset
		int res1 = res.getInt(1);
	}
}catch(SQLException e) {
	// exception-handling
}finally{
	try{
		res.close();
	}catch (Exception exc) {
		// ignore this exception
	}
	try{
		statement.close();
	} catch(Exception exc) {
		// ignore the exception
	}
	try{
		con.close();
	} catch(Exception exc) {
		// ignore the exception
	}
}

You are using the pool with the name mysql. Therefor you need some
properties in opencms.properties:

# Parameters for the mysql connection-pool
pool.mysql.driver = org.gjt.mm.mysql.Driver
pool.mysql.url = jdbc:mysql://localhost:3306/opencms42
pool.mysql.user = root
pool.mysql.password = 
pool.mysql.minConn = 10
pool.mysql.maxConn = 20
pool.mysql.increaseRate = 5
pool.mysql.timeout = 120
pool.mysql.maxage = 360

That's it! You are using the opencms-pool for your statement.

Regards,
Andreas



More information about the opencms-dev mailing list