Create a Java ID search with Mysql

Asked

Viewed 47 times

0

Hello, I am creating a simple sales system. I am developing a DAO class to work with the products. I did the methods of the bank, but when I go to the methods of listing, a following question arises. My table makes relationship with two other, one of group and the other of manufacturers. The problem is that since these fields in the Products class are of the Int type, when I have the query run below, it returns that it should be using String.

chunk Java code:

rs = stmt.executeQuery("select p.idprodutos, p.descricao, g.descricao, "
                                    + "f.descricao, p.custo, p.venda, p.estoque "
                                    + "from produtos p inner join grupoproduto g on (p.grupoProduto = g.idgrupoproduto) "
                                    + "inner join fabricante f on (p.fabricante = f.idfabricante) "
                                    + "where idprodutos = "+idproduto);
            
            if(rs.next()) {
                produto.setIdProduto(rs.getInt("idprodutos"));
                produto.setDescricao(rs.getString("descricao"));
                produto.setGrupoProduto(rs.getInt("grupoProduto"));
                produto.setFabricante(rs.getInt("fabricante"));
                produto.setCusto(rs.getDouble("custo"));
                produto.setVenda(rs.getDouble("venda"));
                produto.setEstoque(rs.getInt("estoque"));
            }

As the tables communicate by PK with FK, I always created the class with Int type parameters in this case. And it really makes sense for him to make that mistake, but how could I get around this situation?

  • After asking the question, I broke my head here and managed to solve it as follows: I added two attributes in the String type products class. After that, instead of my resultset returning the linking fields, I returned only the fields that should actually appear. I was reluctant if it would be the best solution and I don’t even know if it still is, but it solved my problem.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.