Issue
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";
}
}
Solution
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.
Answered By - Elliott Frisch
Answer Checked By - Senaida (JavaFixing Volunteer)