[Resolvido]Não consigo deletar, inserir dados via mysql

1 resposta
F

Alô, pessoal! Alguém aí consegue ver porque não consigo deletar nem inserir dados no banco com essa página abaixo? Eu recebo dados de um input, limpo o banco e insiro os novos dados, mas não está funcionando, porque depois eu acesso o banco via workbench e constato isso. O que está errado?

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
         pageEncoding="ISO-8859-1" %>

<%@ page import="java.sql.*" %>

<!DOCTYPE html>
<html>
<head>
    <meta charset="ISO-8859-1">
    <title>JSP_Five-validar</title>


</head>

<body bgcolor="yellow">
<%
    String URL = "jdbc:mysql://localhost:3306/new_db";
    String usr = "root";
    String psw = "123456";
    ResultSet rs = null;
    Statement statement = null;
    String query = "";
    Connection connection = null;
    String conexao = "";
    try {
        Class.forName("com.mysql.jdbc.Driver");

        connection = DriverManager.getConnection(URL, usr, psw);
        conexao = "conectado ao banco com sucesso.";
        statement = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
                ResultSet.CONCUR_UPDATABLE);
    } catch (ClassNotFoundException ex) {
%>
<%= ex.getMessage() %>
<%
} catch (SQLException ex) {
%>
<%= ex.getMessage() %>
<%
    }

    session.removeAttribute("select_cox");
    session.removeAttribute("quant_cox");
    session.removeAttribute("select_bq");
    session.removeAttribute("quant_bq");
%>
<%
    int i = 0;

    String selCox = request.getParameter("select_cox");
    String quantCox = request.getParameter("quant_cox");
    String selBq = request.getParameter("select_bq");
    String quantBq = request.getParameter("quant_bq");

%>

<%-- colocando na 'session' os campos com valores positivos --%>

<%

    if (selCox != null) {
        session.setAttribute("select_cox", selCox);
    } else {
        i++;
    }
    if (quantCox != "" || !quantCox.equals("")) {
        session.setAttribute("quant_cox", quantCox);
    } else {
        i++;
    }
    if (selBq != null) {
        session.setAttribute("select_bq", selBq);
    } else {
        i++;
    }
    if (quantBq != "" || !quantBq.equals("")) {
        session.setAttribute("quant_bq", quantBq);
    } else {
        i++;
    }

    if (i == 2 || i == 0) {
%>
<!--  aqui entra a rotina de 'delete' - para limpar os registros existentes -->

<%
    try {
        query = "DELETE FROM produtos";
        rs = statement.executeQuery(query);
    } catch (Exception ex) {
%>
<%= ex.getMessage() %>    <%
    }
%>
<!-- aqui entra a rotina de 'insert' -->
<%
    quantBq = String.valueOf("" + session.getAttribute("quant_bq"));
    quantCox = String.valueOf("" + session.getAttribute("quant_cox"));
    int qCox = Integer.parseInt(quantCox);

    try {
        query = "INSERT INTO produtos (codigo, descricao, quantidade) VALUES (2, 'coxinha', qCox)";
        rs = statement.executeQuery(query);
    } catch (Exception ex) {
%>
<%= ex.getMessage() %>
<%
        }
        response.sendRedirect("NewFile.jsp");

    } else if (i == 4) {
        response.sendRedirect("Branca.jsp");
    }
%>
<p>
<p>
</body>
</html>

1 Resposta

F

Ok, pessoal. Enquanto aguardava a resposta de alguém descobri algumas inconsistencias nesse arquivo jsp e estou refazendo-o. Obrigado.

Criado 27 de outubro de 2020
Ultima resposta 29 de out. de 2020
Respostas 1
Participantes 1