Error after committing code in Git (JSP+servlets)

huangapple go评论65阅读模式
英文:

error after committing code in git (jsp+servlets)

问题

I have a java web project (jsp + servlets) that the local code works the property registry, if I commit to git and then give a git pull or git clone the property registry stops working, but none appear error, it simply does not record the data and only returns to the success page, this problem only occurs when I try to commit this version.

I tried to create a new repository and upload it as a new project but the problem continues.

This is the DAO class:

public boolean cadastrar(Imovel imovel) throws SQLException {

        Connection connection = ConnectionFactory.getConexao();

        //Endereço
        String INSERTIMOVEL = "INSERT INTO Imovel VALUES (DEFAULT,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
        String INSERTENDERECO = "INSERT INTO Endereco VALUES (DEFAULT,?,?,?,?,?,?,?)";

        smt = connection.prepareStatement(INSERTENDERECO, Statement.RETURN_GENERATED_KEYS);

        smt.setString(1, imovel.getEndereco().getLogradouro());
        smt.setString(2, imovel.getEndereco().getComplemento());
        smt.setInt(3, imovel.getEndereco().getNumero());
        smt.setString(4, imovel.getEndereco().getCidade());
        smt.setString(5, imovel.getEndereco().getCep());
        smt.setString(6, imovel.getEndereco().getBairro());
        smt.setString(7, imovel.getEndereco().getEstado());
        smt.execute();

        rs = smt.getGeneratedKeys();
        rs.next();

        //Imovel
        smt = connection.prepareStatement(INSERTIMOVEL);
        smt.setString(1, imovel.getTitulo());
        smt.setString(2, imovel.getDescricao());
        smt.setString(3, "Em Análise");
        smt.setString(4, "Ativo");
        smt.setDouble(5, imovel.getValor());
        smt.setDouble(6, imovel.getArea_total());
        smt.setDouble(7, imovel.getArea_edificada());
        smt.setInt(8, imovel.getComodos());
        smt.setInt(9, imovel.getVagas_garagem());
        smt.setInt(10, imovel.getBanheiros());
        smt.setTimestamp(11, timestamp);
        smt.setString(12, imovel.getDiretorio_imagem());
        smt.setString(13, imovel.getTipo_imovel());
        smt.setInt(14, imovel.getUsuario().getId_usuario());
        smt.setInt(15, rs.getInt(1));
        boolean rowInserted = smt.executeUpdate() > 0;

        rs.close();
        smt.close();
        connection.close();
        return rowInserted;
    }

And this is the controller (I use command and factory patterns):

@Override
    public String executar(HttpServletRequest request, HttpServletResponse response) {

        try {

            HttpSession usuarioLogado = request.getSession();
            Sessao sessao = (Sessao) usuarioLogado.getAttribute("usuarioLogado");

            Part filePart = request.getPart("uploadFile");
            String fileName = Paths.get(filePart.getSubmittedFileName()).getFileName().toString();
            InputStream fileContent = filePart.getInputStream();

            imovel.setDiretorio_imagem(sessao.getId_usuario() + File.separator + fileName);

            ByteArrayOutputStream os = new ByteArrayOutputStream();

            byte[] buffer = new byte[1024];
            int len;

            while ((len = fileContent.read(buffer)) != -1) {

                os.write(buffer, 0, len);
            }

            byte[] bytes = os.toByteArray();

            // Create the upload directory
            // This path is relative to the application directory
            ServletContext context = request.getServletContext();
            String uploadPath = context.getRealPath("/") + "Resources\\upload" + File.separator + sessao.getId_usuario();

            // If the directory doesn't exist, create it
            File uploadDir = new File(uploadPath);
            if (!uploadDir.exists()) {
                uploadDir.mkdir();
            }

            // Convert the byte array to a file and save it in the directory
            File f = new File(uploadPath + File.separator + fileName);
            try (FileOutputStream fos = new FileOutputStream(f)) {
                fos.write(bytes);
            }

            //Imovel Requests
            String titulo = request.getParameter("titulo");
            String descricao = request.getParameter("descricao");
            int comodos = Integer.parseInt(request.getParameter("comodos"));
            int banheiro = Integer.parseInt(request.getParameter("banheiro"));
            int garagem = Integer.parseInt(request.getParameter("garagem"));
            Double valor = Double.parseDouble(request.getParameter("valorimovel"));
            Double areatotal = Double.parseDouble(request.getParameter("areatotal"));
            Double areaedificada = Double.parseDouble(request.getParameter("areaedificada"));
            String tpimovel = request.getParameter("tpimovel");

            imovel.setTitulo(titulo);
            imovel.setDescricao(descricao);
            imovel.setComodos(comodos);
            imovel.setBanheiros(banheiro);
            imovel.setVagas_garagem(garagem);
            imovel.setValor(valor);
            imovel.setArea_total(areatotal);
            imovel.setArea_edificada(areaedificada);
            imovel.setTipo_imovel(tpimovel);
            imovel.getUsuario().setId_usuario(sessao.getId_usuario());

            //Endereço Requests
            String logradouro = request.getParameter("logradouro");
            int numero = Integer.parseInt(request.getParameter("numero"));
            String complemento = request.getParameter("complemento");
            String cidade = request.getParameter("cidade");
            String estado = request.getParameter("estado");
            String cep = request.getParameter("cep");
            String bairro = request.getParameter("bairro");

            //Endereço Set's
            imovel.getEndereco().setLogradouro(logradouro);
            imovel.getEndereco().setNumero(numero);
            imovel.getEndereco().setComplemento(complemento);
            imovel.getEndereco().setCidade(cidade);
            imovel.getEndereco().setEstado(estado);
            imovel.getEndereco().setCep(cep);
            imovel.getEndereco().setBairro(bairro);

            ImovelDAO dao = new ImovelDAO();

            if (dao.cadastrar(imovel)) {
                request.setAttribute("msg", "Seu imóvel foi cadastrado e passará por uma análise, fique de olho no seu email!");
                return "index.jsp";
            } else {
                request.setAttribute("msgerro", "Ocorreu um erro ao tentar cadastrar o imóvel, tente novamente");
                return "index.jsp";
            }
        } catch (SQLException | NumberFormatException | IOException | ServletException | MessagingException ex) {
            request.setAttribute("msgerro", ex.getMessage());
            return "index.jsp";
        }
    }
英文:

I have a java web project (jsp + servlets) that the local code works the property registry, if I commit to git and then give a git pull or git clone the property registry stops working, but none appear error, it simply does not record the data and only returns to the success page, this problem only occurs when I try to commit this version.

I tried to create a new repository and upload it as a new project but the problem continues

this is the DAO class

public boolean cadastrar(Imovel imovel) throws SQLException {
Connection connection = ConnectionFactory.getConexao();
//Endereço
String INSERTIMOVEL = "INSERT INTO Imovel VALUES (DEFAULT,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
String INSERTENDERECO = "INSERT INTO Endereco VALUES (DEFAULT,?,?,?,?,?,?,?)";
smt = connection.prepareStatement(INSERTENDERECO, Statement.RETURN_GENERATED_KEYS);
smt.setString(1, imovel.getEndereco().getLogradouro());
smt.setString(2, imovel.getEndereco().getComplemento());
smt.setInt(3, imovel.getEndereco().getNumero());
smt.setString(4, imovel.getEndereco().getCidade());
smt.setString(5, imovel.getEndereco().getCep());
smt.setString(6, imovel.getEndereco().getBairro());
smt.setString(7, imovel.getEndereco().getEstado());
smt.execute();
rs = smt.getGeneratedKeys();
rs.next();
//imovel
smt = connection.prepareStatement(INSERTIMOVEL);
smt.setString(1, imovel.getTitulo());
smt.setString(2, imovel.getDescricao());
smt.setString(3, "Em Análise");
smt.setString(4, "Ativo");
smt.setDouble(5, imovel.getValor());
smt.setDouble(6, imovel.getArea_total());
smt.setDouble(7, imovel.getArea_edificada());
smt.setInt(8, imovel.getComodos());
smt.setInt(9, imovel.getVagas_garagem());
smt.setInt(10, imovel.getBanheiros());
smt.setTimestamp(11, timestamp);
smt.setString(12, imovel.getDiretorio_imagem());
smt.setString(13, imovel.getTipo_imovel());
smt.setInt(14, imovel.getUsuario().getId_usuario());
smt.setInt(15, rs.getInt(1));
boolean rowInserted = smt.executeUpdate() > 0;
rs.close();
smt.close();
connection.close();
return rowInserted;
}

and this is the controller (i use command and factory patterns)

@Override
public String executar(HttpServletRequest request, HttpServletResponse response) {
try {
HttpSession usuarioLogado = request.getSession();
Sessao sessao = (Sessao) usuarioLogado.getAttribute("usuarioLogado");
Part filePart = request.getPart("uploadFile"); // 
String fileName = Paths.get(filePart.getSubmittedFileName()).getFileName().toString(); //
InputStream fileContent = filePart.getInputStream();
imovel.setDiretorio_imagem(sessao.getId_usuario() + File.separator + fileName);
ByteArrayOutputStream os = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len;
while ((len = fileContent.read(buffer)) != -1) {
os.write(buffer, 0, len);
}
byte[] bytes = os.toByteArray();
// cria o diretorio de upload
// esse caminho e relativo ao diretorio da aplicacao
ServletContext context = request.getServletContext();
String uploadPath = context.getRealPath("/") + "Resources\\upload" + File.separator + sessao.getId_usuario();
// caso o diretorio nao exista o bloco abaixo cria o mesmo
File uploadDir = new File(uploadPath);
if (!uploadDir.exists()) {
uploadDir.mkdir();
}
//converte o array de bytes em file e grava no diretorio
File f = new File(uploadPath + File.separator + fileName);
try (FileOutputStream fos = new FileOutputStream(f)) {
fos.write(bytes);
}
//Imovel Requests
String titulo = request.getParameter("titulo");
String descricao = request.getParameter("descricao");
int comodos = Integer.parseInt(request.getParameter("comodos"));
int banheiro = Integer.parseInt(request.getParameter("banheiro"));
int garagem = Integer.parseInt(request.getParameter("garagem"));
Double valor = Double.parseDouble(request.getParameter("valorimovel"));
Double areatotal = Double.parseDouble(request.getParameter("areatotal"));
Double areaedificada = Double.parseDouble(request.getParameter("areaedificada"));
String tpimovel = request.getParameter("tpimovel");
imovel.setTitulo(titulo);
imovel.setDescricao(descricao);
imovel.setComodos(comodos);
imovel.setBanheiros(banheiro);
imovel.setVagas_garagem(garagem);
imovel.setValor(valor);
imovel.setArea_total(areatotal);
imovel.setArea_edificada(areaedificada);
imovel.setTipo_imovel(tpimovel);
imovel.getUsuario().setId_usuario(sessao.getId_usuario());
//Endereço Requests
String logradouro = request.getParameter("logradouro");
int numero = Integer.parseInt(request.getParameter("numero"));
String complemento = request.getParameter("complemento");
String cidade = request.getParameter("cidade");
String estado = request.getParameter("estado");
String cep = request.getParameter("cep");
String bairro = request.getParameter("bairro");
//Endereço Set's
imovel.getEndereco().setLogradouro(logradouro);
imovel.getEndereco().setNumero(numero);
imovel.getEndereco().setComplemento(complemento);
imovel.getEndereco().setCidade(cidade);
imovel.getEndereco().setEstado(estado);
imovel.getEndereco().setCep(cep);
imovel.getEndereco().setBairro(bairro);
ImovelDAO dao = new ImovelDAO();
if (dao.cadastrar(imovel)) {
request.setAttribute("msg", "Seu imóvel foi cadastrado e passará por uma análise, fique de olho no seu email!");
return "index.jsp";
} else {
request.setAttribute("msgerro", "Ocorreu um erro ao tentar cadastrar o imóvel, tente novamente");
return "index.jsp";
}
} catch (SQLException | NumberFormatException | IOException | ServletException | MessagingException ex) {
request.setAttribute("msgerro", ex.getMessage());
return "index.jsp";
}
}

答案1

得分: 0

这一行smt = connection.prepareStatement(INSERTIMOVEL);关闭了之前的statement **和 ** ResultSet。在这行之前获取值。

int val = rs.getInt(1);
smt = connection.prepareStatement(INSERTIMOVEL);
// ...
smt.setInt(15, val);

如果这不是问题,请编辑您的问题以包括任何异常或与上一个工作版本的差异。

英文:

This line smt = connection.prepareStatement(INSERTIMOVEL); closes the previous statement and ResultSet. Get the value before that line.

int val = rs.getInt(1);
smt = connection.prepareStatement(INSERTIMOVEL);
// ...
smt.setInt(15, val);

If that is not the issue, please edit your question to include any exceptions or a diff with the last working version.

huangapple
  • 本文由 发表于 2020年8月12日 09:52:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/63368623.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定