1
resposta

Servlet

Estou desenvolvendo uma aplicacao servlet no estagio e a classe conexao e a seguinte, como faco para chamar essa conexao no Dao?:

//Conexao
    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"));
            }
        }
    }

Dao:

    public void inserir (Tabela1 t) {
        Connection con = ();
        PreparedStatement stmt = null;

        try {
            stmt =con.prepareStatement("INSERT INTO tabtest1 (col1,col2,col3,col4,col5) VALUES (?,?,?,?,?) ");
            stmt.setString(1, t.getCol1());
            stmt.setString(2, t.getCol2());
            stmt.setInt(3, t.getCol3());
            stmt.setDouble(4, t.getCol4());
            stmt.setDouble(5, t.getCol5());

            stmt.execute();
            stmt.close();

        }catch(SQLException ex) {
            Logger.getLogger(Operacoes.class.getName()).log(Level.SEVERE, null, ex);


        }


    }
1 resposta

Oi Julien,

Vc diz como abrir a conexao no DAO na linha que tá com Connection con = ();? Voce precisa usar o DriverManager.

Sugiro dar uma olhada nesse curso de JDBC. Logo na primeira aula ele mostra como abrir uma conexão: https://cursos.alura.com.br/course/jdbc/task/3075