Note for WebSphere: The creation of a WebSphere datasource "ds1" creates a JNDI name of "jdbc/ds1" for the datasource. The name with the "jdbc" prefix must be used when accessing the sample servlets.
The following sample servlet is suitable for all supported application servers. It demonstrates how to access a previously created Connection Pool using Data Source default username and password.
import java.sql.*;
import javax.sql.*;
import java.io.*;
import javax.naming.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class SampleJdbcServlet extends HttpServlet
{
public void doGet(HttpServletRequest req,HttpServletResponse res)
throws ServletException, IOException
{
String datasource = req.getParameter("datasource");
res.setContentType("text/html");
PrintWriter out = res.getWriter();
out.println("<html><body><pre>");
try
{
Context ctx = new InitialContext();
out.println("Looking up datasource " + datasource);
DataSource ds = (DataSource) ctx.lookup(datasource);
out.println("Establishing connection...");
Connection con = ds.getConnection();
out.println("Connection obtained is: " + con);
con.close();
out.println("Connection.isClosed returns: " + con.isClosed());
}
catch (SQLException ex)
{
out.println("*** SQLException caught ***");
while (ex != null)
{
out.println("Message: " + ex.getMessage ());
out.println("SQLState: " + ex.getSQLState ());
out.println("ErrorCode: " + ex.getErrorCode ());
ex.printStackTrace (out);
ex = ex.getNextException ();
}
}
catch (java.lang.Exception ex)
{
out.println("*** Exception caught ***");
ex.printStackTrace (out);
}
out.println("SampleJdbcServlet finished.");
out.println("</pre></body></html>");
out.close();
}
}
The following sample servlet is suitable for all supported application servers, except JBoss Fuse, Tomcat, and WebLogic. It demonstrates how to access a previously created Connection Pool with username and password passed as parameters.
import java.sql.*;
import javax.sql.*;
import java.io.*;
import javax.naming.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class SampleJdbcServlet extends HttpServlet
{
public void doGet(HttpServletRequest req,HttpServletResponse res)
throws ServletException, IOException
{
String user = req.getParameter("user");
String password = req.getParameter("password");
String datasource = req.getParameter("datasource");
res.setContentType("text/html");
PrintWriter out = res.getWriter();
out.println("<html><body><pre>");
try
{
Context ctx = new InitialContext();
out.println("Looking up datasource " + datasource);
DataSource ds = (DataSource) ctx.lookup(datasource);
out.println("Establishing connection...");
out.println("User: " + user + "");
out.println("Password: " + password);
Connection con = ds.getConnection(user, password);
out.println("Connection obtained is: " + con);
con.close();
out.println("Connection.isClosed returns: " + con.isClosed());
}
catch (SQLException ex)
{
out.println("*** SQLException caught ***");
while (ex != null)
{
out.println("Message: " + ex.getMessage ());
out.println("SQLState: " + ex.getSQLState ());
out.println("ErrorCode: " + ex.getErrorCode ());
ex.printStackTrace (out);
ex = ex.getNextException ();
}
}
catch (java.lang.Exception ex)
{
out.println("*** Exception caught ***");
ex.printStackTrace (out);
}
out.println("SampleJdbcServlet finished.");
out.println("</pre></body></html>");
out.close();
}
}