-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProductDAO.java
More file actions
150 lines (115 loc) · 4.07 KB
/
Copy pathProductDAO.java
File metadata and controls
150 lines (115 loc) · 4.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package daoLibrary;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
import modelsLibrary.Product;
public class ProductDAO {
public void insertProduto(Product produto) {
String sql = "INSERT INTO produto (nome,descricao,preco,idCategoria) VALUES (?,?,?,?)";
Connection conn = null;
PreparedStatement stm = null;
try {
conn = DBConnection.getConnection();
stm = conn.prepareStatement(sql);
stm.setString(1, produto.getNome());
stm.setString(2, produto.getDescricao());
stm.setBigDecimal(3, produto.getPreco());
stm.setInt(4, produto.getIdCategoria());
stm.executeUpdate();
} catch (SQLException ex) {
throw new RuntimeException("Erro ao inserir o Produto. SQL: "+ex.getMessage()+" Erro: "+ex);
}finally {
DBConnection.CloseConnection(conn, stm);
}
}
public List<Product> selectAllProduto(){
String sql = "SELECT * FROM produto";
Connection conn = null;
PreparedStatement stm = null;
ResultSet resul = null;
List<Product> produtos = new ArrayList<Product>();
try {
conn = DBConnection.getConnection();
stm = conn.prepareStatement(sql);
resul = stm.executeQuery();
while (resul.next()) {
Product produto = new Product();
produto.setIdProduto(resul.getInt("idProduto"));
produto.setNome(resul.getString("nome"));
produto.setDescricao(resul.getString("descricao"));
produto.setPreco(resul.getBigDecimal("preco"));
produto.setIdCategoria(resul.getInt("idCategoria"));
produtos.add(produto);
}
} catch (SQLException ex) {
throw new RuntimeException("Erro ao Listar Produtos. SQL: "+ex.getMessage()+" Erro: "+ex);
}finally {
DBConnection.CloseConnection(conn, stm, resul);
}
return produtos;
}
public List<Product> listarProdutosCategoria(int idCategoria){
String sql = "SELECT idProduto,nome,descricao,preco FROM produto WHERE idCategoria = ? ORDER BY nome ASC";
Connection conn = null;
PreparedStatement stm = null;
ResultSet resul = null;
List<Product> produtos = new ArrayList<Product>();
try {
conn = DBConnection.getConnection();
stm = conn.prepareStatement(sql);
stm.setInt(1, idCategoria);
resul = stm.executeQuery();
while(resul.next()) {
Product produto = new Product();
produto.setIdProduto(resul.getInt("idProduto"));
produto.setNome(resul.getString("nome"));
produto.setDescricao(resul.getString("descricao"));
produto.setPreco(resul.getBigDecimal("preco"));
produtos.add(produto);
}
} catch (SQLException ex) {
throw new RuntimeException("Erro ao listar Produtos com base na Categoria. Erro: "+ex.getMessage());
}finally {
DBConnection.CloseConnection(conn, stm, resul);
}
return produtos;
}
public void updateProduto(Product produto) {
String sql = "UPDATE produto SET nome = ?, descricao = ?, preco = ?, idCategoria = ? WHERE idProduto = ?";
Connection conn = null;
PreparedStatement stm = null;
try {
conn = DBConnection.getConnection();
stm = conn.prepareStatement(sql);
stm.setString(1, produto.getNome());
stm.setString(2, produto.getDescricao());
stm.setBigDecimal(3, produto.getPreco());
stm.setInt(4, produto.getIdCategoria());
stm.setInt(5, produto.getIdProduto());
stm.executeUpdate();
} catch (SQLException ex) {
JOptionPane.showMessageDialog(null, "Erro ao atualizar o Produto. Erro: "+ex);
}finally {
DBConnection.CloseConnection(conn, stm);
}
}
public void deleteProduto (Product produto) {
String sql = "DELETE FROM produto WHERE idProduto = ?";
Connection conn = null;
PreparedStatement stm = null;
try {
conn = DBConnection.getConnection();
stm = conn.prepareStatement(sql);
stm.setInt(1, produto.getIdProduto());
stm.executeUpdate();
} catch (SQLException ex) {
JOptionPane.showMessageDialog(null, "Erro ao deletar Produto. Erro: "+ex.getMessage());
}finally {
DBConnection.CloseConnection(conn, stm);
}
}
}