FileDocCategorySizeDatePackage
DbServletResult.javaAPI DocExample2187Wed Jul 23 09:14:20 BST 2003com.jspservletcookbook

DbServletResult

public class DbServletResult extends HttpServlet

Fields Summary
DataSource
pool
Constructors Summary
Methods Summary
public voiddoGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)

    
        String sql = "select * from athlete";
        
        try{
        
        Result jspResult = select(sql);
        
        HttpSession session = request.getSession();
        
        session.setAttribute("javax.servlet.jsp.jstl.sql.Result",jspResult);
        
        RequestDispatcher dispatcher = request.getRequestDispatcher("/useResult.jsp");
        
        dispatcher.forward(request,response);
        
            } catch (SQLException sqle){ throw new ServletException(sqle.getMessage());}
 
     
public voidinit()

        
         Context env = null;
        
       try{
           
             env = (Context) new InitialContext().lookup("java:comp/env");
             pool  = (DataSource) env.lookup("jdbc/oracle-8i-athletes");
             if (pool == null)
             throw new ServletException("'oracle-8i-athletes' is an unknown DataSource");
             
        } catch (NamingException ne) { 
        
           throw new ServletException(ne);

        }
      

    
public javax.servlet.jsp.jstl.sql.Resultselect(java.lang.String sql)


    if (sql == null || sql.equals(""))
        throw new SQLException("Invalid parameter in select method");
        
    ResultSet rs = null;
    Connection conn = null;
    Result res = null;
    
    conn = pool.getConnection();
    
    if (conn == null )
        throw new SQLException("Invalid Connection in select method");
        
    PreparedStatement stmt = conn.prepareStatement(sql);
    
    rs = stmt.executeQuery();
    
    res=ResultSupport.toResult(rs);
    
    rs.close();
    conn.close();
    return res;