import
javax.servlet.*;
import
javax.servlet.http.*;
import
java.io.*;
import
java.util.*;
import
javax.naming.*;
import
java.sql.*;
import
javax.sql.*;
public
class DataSourceServlet extends HttpServlet {
/**
*
Initialize global variables
*/
InitialContext
context = null;
DataSource
jdbcURL = null;
Connection
conn = null;
public
void init(ServletConfig config) throws ServletException
{
super.init(config);
//
Obtain connection using JNDI Lookup
try
{
context
= new InitialContext();
//
for none-pooled purpose
//
jdbcURL = (DataSource) context.lookup("jdbc/xzOracleCoreDS");
//
for pooled purpose
jdbcURL
= (DataSource) context.lookup("jdbc/xzOracleDS");
}
catch(NamingException
e)
{
throw
new ServletException("Error looking Data Source", e);
}
}
/**
*
Process the HTTP Get request
*/
public
void doGet(HttpServletRequest request, HttpServletResponse
response)
throws
ServletException, IOException {
String
myTitle="Getting DataSource via JNDI Lookup ";
response.setContentType("text/html");
PrintWriter
out = new PrintWriter (response.getOutputStream());
out.println("<html>");
out.println("<head><title>
" + myTitle + "</title></head>");
out.println("<body><H1>"
+ myTitle + "</H1>");
//
Connect to the database
try {
Connection
conn = jdbcURL.getConnection();
out.println("Connected
to database successfully ..");
Statement
stmt = conn.createStatement();
ResultSet
rs = stmt.executeQuery("SELECT ename FROM emp");
out.println("Results
of the query");
// Display
Results of the SQL Query
while
( rs.next() ) {
out.println(
"<br>" + rs.getString("ename") + "<br>");
}
conn.close();
}
catch(SQLException
e)
{
throw new ServletException("Error connecting to Database",
e);
}
out.println("</body></html>");
out.close();
}
/**
*
Get Servlet information
*
@return java.lang.String
*/
public
String getServletInfo() {
return
"DataSourceServlet Information";
}
}