Guest User

Untitled

a guest
Nov 22nd, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.31 KB | None | 0 0
  1. public class Marca {
  2.  
  3. private long id;
  4. private String nome;
  5.  
  6. public long getId() {
  7. return id;
  8. }
  9.  
  10. public void setId(long id) {
  11. this.id = id;
  12. }
  13.  
  14. public String getNome() {
  15. return nome;
  16. }
  17.  
  18. public void setNome(String nome) {
  19. this.nome = nome;
  20. }
  21.  
  22. public class Produto {
  23.  
  24. private int id;
  25. private String nome;
  26. private double preco;
  27. private Marca marca;
  28.  
  29. public Produto(String nome, Marca marca) {
  30. this.nome = nome;
  31. this.marca = marca;
  32. }
  33.  
  34. public Produto() {
  35. }
  36.  
  37. public long getId() {
  38. return id;
  39. }
  40.  
  41. public void setId(int id) {
  42. this.id = id;
  43. }
  44.  
  45. public String getNome() {
  46. return nome;
  47. }
  48.  
  49. public void setNome(String nome) {
  50. this.nome = nome;
  51. }
  52.  
  53. public Marca getMarca() {
  54. return marca;
  55. }
  56.  
  57. public void setMarca(Marca marca) {
  58. this.marca = marca;
  59. }
  60.  
  61. public List<Produto> consultarTodos() throws ClassNotFoundException, SQLException {
  62.  
  63. Connection con = Conexao.getConnection();
  64. List<Produto> todosProdutos = new ArrayList<>();
  65. try (
  66. PreparedStatement ps = con.prepareStatement("SELECT a.nome,b.idProdutos,b.nomeProdutos FROM marca as A , produtos as B where a.id = b.idmarca")) {
  67.  
  68. try (ResultSet rs = ps.executeQuery()) {
  69.  
  70. while (rs.next()) {
  71. Produto p = new Produto();
  72. p.setId(rs.getInt("idProdutos"));
  73. p.getMarca().setNome(rs.getString("nome"));
  74. todosProdutos.add(p);
  75. }
  76. } catch (SQLException ex) {
  77. System.out.println("Erro: " + ex.getMessage());
  78. // ATENÇÃO: Comer exceções só dando um System.out.println nelas é uma má prática de programação!
  79. }
  80. return todosProdutos;
  81.  
  82. }
  83. }
  84.  
  85. <c:forEach var="prod" items="${produtos}" >
  86. <tbody>
  87. <tr>
  88. <ul class="list-group">
  89.  
  90. <th><li class="list-group-item">${prod.nome}</li></th>
  91. <th><li class="list-group-item">${prod.marca.nome}</li></th>
  92. </ul>
  93.  
  94. <th><td><input type="button" value="Atualizar" onclick="javascript: setIDAtualizar(${prod.id});"></td></th>
  95. <th><td><input type="button" value="Excluir" onclick="javascript: setIDExclusao(${prod.id});"></td></th>
  96. </tr>
  97. </tbody>
  98.  
  99. </form>
  100. </thead>
  101. </c:forEach>
Add Comment
Please, Sign In to add comment