On Wed, 16 Jan 2002, Randy Speh wrote:

> Date: Wed, 16 Jan 2002 06:46:53 -0800 (PST)
> From: Randy Speh <[EMAIL PROTECTED]>
> Reply-To: Jakarta Commons Developers List <[EMAIL PROTECTED]>
> To: Jakarta Commons Developers List <[EMAIL PROTECTED]>
> Subject: Re: cvs commit:
>     jakarta-commons/dbcp/src/java/org/apache/commons/dbcp
>     BasicDataSource.java BasicDataSourceFactory.java
>
> Could you please provide a small example of how to use
> the BasicDataSource?
>

Sure.  It follows a fairly universal design pattern for Commons components
that it is a JavaBean -- there is a no-arguments constructor that lets
anonymous instances of the class be created, and then everything is
configured via bean properties.

The initialization code might look like this (I am using literal values,
but BasicDataSource doesn't care where you get them from -- in Struts, for
example, they will come out of the struts-config.xml file):

  BasicDataSource bds = new BasicDataSource();
  bds.setDriverClassName("org.postgresql.Driver");
  bds.setMaxActive(4);
  bds.setPassword("mypassword");
  bds.setUrl("jdbc:postgresql://localhost/mydatabase");
  bds.setUsername("myusername");
  ... The "bds" object is now available for use ...

To use this (or any) implementation of javax.sql.DataSource in your
application, the pattern goes like this:

  Connection conn = bds.getConnection();
  ... do JDBC calls on this connection ...
  conn.close();

The last statement doesn't really close the connection -- it simply
returns the connection to the pool.

(You will need the "JDBC 2.0 Optional Package" download to get the APIs
for the "javax.sql" package, and you will need to put "jdbc2_0-stdext.jar"
on your CLASSPATH along with "commons-dbcp.jar", "commons-pool.jar", and
"commons-collections.jar".)

> Thank you,
> Randy Speh

Craig


--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to