2
respostas

JAVA SERVLET COMO CHAMAR ESSA CONEXAO NA CLASSE DAO?

public class WebTstDB1 extends HttpServlet {
    private static final long serialVersionUID = 1L;
    private static final boolean DEBUG1 = true;
    private static final int TB_NONE = 0;
    private DataSource ds = null;

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {
        doPost(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {
        PrintWriter pwOut = null;
        try{
            response.setContentType("text/html");
            response.setCharacterEncoding("UTF-8");

            String strServletPath = request.getServletPath();
            String strPathInfo = request.getPathInfo();
            if (strServletPath.equals("/index") && strPathInfo == null) {
                pwOut = response.getWriter();
                outputHeader(request, pwOut);
                outputBody(pwOut);
                wmShowIndex(request, response, pwOut);
                outputFooter(request, response, pwOut, TB_NONE);
                return;
            }
            if (strServletPath.equals("/db")){
                if (strPathInfo.equals("/ShowPagina")) {
                    pwOut = response.getWriter();
                    outputHeader(request, pwOut);
                    outputBody(pwOut);
                    wmShowPagina(request, response, pwOut);
                    outputFooter(request, response, pwOut, TB_NONE);
                    return;
                }
                if (strPathInfo.equals("/Pagina")) {
                    Enumeration<?> pnames = request.getParameterNames();
                    String pn = null;
                    while(pnames.hasMoreElements()){
                        pn = (String) pnames.nextElement();
                        System.out.println("PN=" + pn);
                    }

                    if(request.getParameter("forw") != null){
                        wmIndexForward(request, response, pwOut);
                    }else{
                        wmIndexRedirect(request, response, pwOut);
                    }
                    return;
                }
            }
            pwOut = response.getWriter();
            outputHeader(request, pwOut);
            outputBody(pwOut);
            wmDebugInfo1(request, response, pwOut);
            pwOut.println("<h1>Requested page does not exist</h1>");
            outputFooter(request, response, pwOut, TB_NONE);
        } catch (Exception excp) {
            if (response.isCommitted()) {
                ShowException.outputException(request, response, pwOut, excp);
            } else {
                request.getSession().setAttribute("exception", excp);
                response.sendRedirect(response.encodeRedirectURL(request.getContextPath() + "/ShowException"));
            }
        }
    }


    private void wmShowIndex(HttpServletRequest request, HttpServletResponse response, PrintWriter locOut)
    throws SQLException {
        Connection conn = null;
        PreparedStatement stmt1 = null;
        ResultSet rs1 = null;
        PreparedStatement stmt2 = null;
        ResultSet rs2 = null;
        int rec1 = 0;
        int rec2 = 0;

        // Allocate and use a connection from the pool
        conn = ds.getConnection();
        stmt1 = conn.prepareStatement("select count(*) from tabtest1");
        stmt2 = conn.prepareStatement("select count(*) from tabtest2");

        rs1 = stmt1.executeQuery();
        while(rs1.next()){
            rec1 = rs1.getInt(1);
        }
        rs1.close();

        rs2 = stmt2.executeQuery();
        while(rs2.next()){
            rec2 = rs2.getInt(1);
        }
        rs2.close();

        request.removeAttribute("help");
        wmDebugInfo1(request, response, locOut);

        locOut.println("Hallo meine Liebe - das Index DB!");
        locOut.println("<br><br><br>Es gibt " + rec1 + " records in table 1");
        locOut.println("<br><br><br>Es gibt " + rec2 + " records in table 2");

        stmt1.close();
        stmt2.close();
        conn.close();
    }

    private void wmShowPagina(HttpServletRequest request, HttpServletResponse r
2 respostas

Oi Julien, tudo bem?

Você quer configurar um DataSource para qual banco de dados? Você vai subir sua aplicação num Tomcat, JBoss...? Poderia informar qual é o curso que você está enfrentando esse problema?

Basicamente, para configurar um DataSource você precisa informar:

  • connection-url
  • driver-class
  • user-name
  • password

Abraço!

Eu vou subir minha aplicacao num tomcat,os dados do banco estao num CONTEXT.XML.