2009/06/10

Using connection pool in java bean

Now I am using Netbean 6.5 as an IDE, and Glassfish 3 Prelude as a server app.

I tried to use the connection pool provided by Netbean in my java beans, following the help document "Accessing a Connection Pool from a Java Class " in Netbean, but it says no to me ...

I have n0 time to figure it out since my boss is pushing me, so I borrowed the connection pool code from http://java.sun.com/developer/onlineTraining/Programming/JDCBook/conpool.html . There are totally 3 java files that is needed to download:


The interface you need is the first class: JDCConnectionDriver

Here's a demonstration code fragment for connecting to the database:

-------------------------------------------------------------------------

new JDCConnectionDriver("com.mysql.jdbc.Driver", "jdbc:mysql://localhost:3306/weblcc", "root", "password");

Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/weblcc");

-------------------------------------------------------------------------

now the conn could be used for database operation.

Notice: There might be some problems when you run your code unless you uncomment the 3 lines of code:

// if (!url.startsWith(URL_PREFIX)) {

// return null;

// }

in JDCConnectionDriver.connect(String url, Properties props) method, or change the JDCConnectionDriver.URL_PREFIX from "jdbc:jdc:" to "jdbc:". It depends on your URL.

I have tested the code, and plug it in my web apps. Hope it works well  :-D

No comments:

Post a Comment