Interconnection of tables in Mysql in Java program

Asked

Viewed 27 times

2

Good afternoon ladies and gentlemen, I am developing a project for study with 2 tables and these tables need to be connected in a certain value. In my package DAO, have 2 classes Stockpile and Stockpiling and they correspond to a product table (containing code, description, make and observing) and other with the product-specific information previously registered (cod_gra, cod_est, cod_bar, value, quantity and size). I need the values of code_stock (column of the second table) and code (column of the first table) assume the same value.

my method:

public List<EstoqueGrade> read() {
        Connection con = ConnectionFactory.abrirConexao();
        PreparedStatement stmt = null;
        ResultSet rs = null;

        List<EstoqueGrade> estoqueGrade = new ArrayList<>();
        try {
            stmt = con.prepareStatement("SELECT * FROM estoque_grade");
            rs = stmt.executeQuery();

            while (rs.next()) {
                EstoqueGrade eg = new EstoqueGrade();
                eg.setCod_gra(rs.getString("cod_gra"));
                eg.setCod_est(rs.getString("cod_est"));
                eg.setCod_bar(rs.getString("cod_bar"));
                eg.setTamanho(rs.getString("tamanho"));
                eg.setValor(rs.getString("valor"));
                eg.setQtd(rs.getString("qtd"));
                estoqueGrade.add(eg);
            }
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, "Erro ao ler dados: \n" + ex);
        } finally {
            ConnectionFactory.fecharConexao(con, stmt, rs);
        }
        return estoqueGrade;
    }

    public ArrayList retornarEstoqueGrade() {
        ArrayList estoqueGrade = new ArrayList();
        Connection con = ConnectionFactory.abrirConexao();
        PreparedStatement stmt = null;
        ResultSet rs = null;

        try {
            stmt = con.prepareStatement("SELECT * FROM estoque_grade");
            rs = stmt.executeQuery();
                        
            if (rs.next()) {
                do {
                    estoqueGrade.add(new Object[]{
                        rs.getString("cod_gra"),
                        rs.getString("cod_est"),
                        rs.getString("cod_bar"),
                        rs.getString("tamanho"),
                        rs.getString("valor"),
                        rs.getString("qtd")
                    });
                } while (rs.next());
            }
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, "Erro ao mostrar dados: \n" + ex);
        }
        return estoqueGrade;
    } 

I did not pass the method of the other table, but it is the same thing with the different variables.

I can’t use foreign key for this case, I have to work only with data manipulation.

If you need more information, I’ll let you know, but I don’t think that’ll be necessary. Remember that I just need to solve this, the registration is working for both (separately).

PS: I started learning Object Orientation, Java and Mysql less than 2 months ago, sorry if I’m asking obvious questions...

  • Uses JPA and simpler, currently JPA and the best solution for java programmers

No answers

Browser other questions tagged

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