Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Marca {
- private long id;
- private String nome;
- public long getId() {
- return id;
- }
- public void setId(long id) {
- this.id = id;
- }
- public String getNome() {
- return nome;
- }
- public void setNome(String nome) {
- this.nome = nome;
- }
- public class Produto {
- private int id;
- private String nome;
- private double preco;
- private Marca marca;
- public Produto(String nome, Marca marca) {
- this.nome = nome;
- this.marca = marca;
- }
- public Produto() {
- }
- public long getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getNome() {
- return nome;
- }
- public void setNome(String nome) {
- this.nome = nome;
- }
- public Marca getMarca() {
- return marca;
- }
- public void setMarca(Marca marca) {
- this.marca = marca;
- }
- public List<Produto> consultarTodos() throws ClassNotFoundException, SQLException {
- Connection con = Conexao.getConnection();
- List<Produto> todosProdutos = new ArrayList<>();
- try (
- PreparedStatement ps = con.prepareStatement("SELECT a.nome,b.idProdutos,b.nomeProdutos FROM marca as A , produtos as B where a.id = b.idmarca")) {
- try (ResultSet rs = ps.executeQuery()) {
- while (rs.next()) {
- Produto p = new Produto();
- p.setId(rs.getInt("idProdutos"));
- p.getMarca().setNome(rs.getString("nome"));
- todosProdutos.add(p);
- }
- } catch (SQLException ex) {
- System.out.println("Erro: " + ex.getMessage());
- // ATENÇÃO: Comer exceções só dando um System.out.println nelas é uma má prática de programação!
- }
- return todosProdutos;
- }
- }
- <c:forEach var="prod" items="${produtos}" >
- <tbody>
- <tr>
- <ul class="list-group">
- <th><li class="list-group-item">${prod.nome}</li></th>
- <th><li class="list-group-item">${prod.marca.nome}</li></th>
- </ul>
- <th><td><input type="button" value="Atualizar" onclick="javascript: setIDAtualizar(${prod.id});"></td></th>
- <th><td><input type="button" value="Excluir" onclick="javascript: setIDExclusao(${prod.id});"></td></th>
- </tr>
- </tbody>
- </form>
- </thead>
- </c:forEach>
Add Comment
Please, Sign In to add comment