Skip to Main Content

Java Database Connectivity (JDBC)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to call connection pool servlet from another servlet?

843859Oct 31 2005 — edited Oct 31 2005
I have created this servlet shown below for creating a connection pool. I now want to be able
to use this pool to provide my other servlets with a database connection, so I am attempting
to use a line like this to create the connection:


Connection ocon = ConPoolInit.getConnection("java:comp/env/jdbc/CraigsList");

But it does not compile. Can somebody help me with this code? Any suggestions are
greatly appreciated.

Here is the connection pool servlet:

*****************************
package CraigsClasses;

import javax.servlet.*;
import javax.servlet.jsp.*;
import java.net.*;
import java.io.*;
import javax.servlet.http.*;
import java.sql.*;
import javax.naming.*;
import javax.sql.DataSource;

public class ConPoolInit extends HttpServlet {

public void init(ServletConfig config) throws ServletException {
super.init(config);
}

public static Connection getConnection(String lookup) throws NamingException, SQLException {
Context context = new InitialContext();
DataSource ds = (DataSource)context.lookup(lookup);
Connection ocon = ds.getConnection();
context.close();
return ocon;
} // private Connection getConnection(String lookup) throws NamingException, SQLException {

} // public class ConPoolInit extends HttpServlet {
=========================================================
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 28 2005
Added on Oct 31 2005
2 comments
257 views