Professional Documents
Culture Documents
MOURA
MANIPULAO DE BANCO DE DADOS UTILIZANDO INTERFACE GRFICA
ORIENTADO A PROJETO: CONTROLE DE ESTOQUE
JOSIEL S. MOURA
38
INTRODUO
A ideia de padres foi apresentada por Christopher Alexander em 1977 no cont
exto de Arquitetura (de prdios e cidades).
Cada padro descreve um problema que ocorre repetidamente, de novo e de novo
, em nosso ambiente, e ento descreve a parte central da soluo para aquele problema
de uma forma que voc pode usar esta soluo um milho de vezes, sem nunca implement-la
duas vezes da mesma forma.
Produto
Movimento
Dao
Produto
55
57
58
59
60
62
stm.executeUpdate(sql); 56
Movimento
movimento(tipo,quantidade,data,produto_idProduto)"
13
+ "VALUES('" + m.getTipo() + "'," + m.getQuantidade() + ",'"
+ m.getData()
14
+ "'," + m.getProduto().getId() + ")";
15 stm.executeUpdate(sql);
16 return true; 17
}
18
return false; 19
}
20
21 private boolean atualizarEstoque(Movimento m) throws Exception {
22 ProdutoDao produtoDao = new ProdutoDao();
23 double estoque = produtoDao.getProduto(m.getProduto().getId()).getEstoque();
24 if (m.getTipo().equals("Entrada")) {
25 estoque += m.getQuantidade();
26 } else {
27 if (estoque > m.getQuantidade()) {
28 estoque -= m.getQuantidade();
29 } else {
30 return false; 31
}
32
}
33 sql = "UPDATE produto SET estoque=" + estoque + " WHERE idProduto=" + m.getPr
oduto().getId();
34 stm.executeUpdate(sql);
35 return true; 36
}
37 }
Formulrios Principal
Fornecedor
10
12
13
14
16
18
19
20
21
23
24
26
27
29
30
31
32
33
34
36
37
38
39
41
42
43
45
46
47
48
49
50 tfCnpj.setText(f.getCnpj()); 51
}
52
53
//Cdigo omitido 249
250 private void
btCadastrarActionPerformed(java.awt.event.ActionEvent evt) {
251 try {
252 getFornecedor();
253 fornecedorDao.cadastrar(fornecedor);
254 JOptionPane.showMessageDialog(null, "Cadastrado com sucesso!");
255 limparCampos();
256 } catch (Exception ex) {
257 JOptionPane.showMessageDialog(null, ex);
258
}
259
} 260
261 private void btFecharActionPerformed(java.awt.event.ActionEvent evt) {
262 dispose();
263
} 264
265 private void
btPesquisarActionPerformed(java.awt.event.ActionEvent evt) {
266 try {
267
268
269
270
271
272
273
275
276
277
278
279
fornecedores = fornecedorDao.pesquisar(tfNome.getText());
posicao = 0;
setFornecedor(fornecedores.get(posicao));
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, ex);
}
} 274
private void btProximoActionPerformed(java.awt.event.ActionEvent evt) {
if (posicao < fornecedores.size() - 1) {
posicao++;
setFornecedor(fornecedores.get(posicao));
}
280
282
if
283
286
285
posicao--;
284
289
290
292
291
294
posicao
295
297
setFornecedor(fornecedores.get(posicao));
296
299
300
303
fornecedorDao.alterar(fornecedor);
302
301
305
limparCampos();
304
306
}307
private
310
evt)
try
311
JOptionPane.showMessageDialog(null,
314
fornecedorDao.excluir(fornecedor.getId());
313
getFornecedor();
312
3209
81catch
87
88
93
98
08
(posicao
{{ void
= fornecedores.size()
(Exception
0;>btExcluirActionPerformed(java.awt.event.ActionEvent
btAnteriorActionPerformed(java.awt.event.ActionEvent
btPrimeiroActionPerformed(java.awt.event.ActionEvent
btUltimoActionPerformed(java.awt.event.ActionEvent
btAlterarActionPerformed(java.awt.event.ActionEvent
0) { ex) {
- 1; "Excluido
"Alterado com sucesso!");
ex);
315Variables
sucesso!");
316
317
318
319
321
320
evt)
322
323
325
324
352
351
353
354
355
356
357
358
359
360
361
362
//
363
btPrimeiro;
371
btPesquisar;
370
btNovo;
369
btFechar;
368
btExcluir;
367
btCadastrar;
366
btAnterior;
365
btAlterar;
364
javax.swing.JLabel
375
jButton10;
374
btUltimo;
373
javax.swing.JButton
372
jLabel1;
377
private
376
{ }});
private
limparCampos();
//Cdigo
/*
java.awt.EventQueue.invokeLater(new
public
try
new
JOptionPane.showMessageDialog(null,
jLabel2;
catch
Create
{btProximo;
FormularioFornecedor().setVisible(true);
declaration
void
void
(Exception
omitido
and
jLabel3;
javax.swing.JLabel
run()
btNovoActionPerformed(java.awt.event.ActionEvent
display
-{ex)
do the
not
{ modify
formjavax.swing.JLabel
*/ ex);
Runnable() {
378
379
380
381
382
private private privatejavax.swing.JTextField
383
javax.swing.JTextField
jLabel4;
javax.swing.JLabel
private private
javax.swing.JTextField javax.swing
// End
tfTelefone;
tfNome;
tfId;
tfCnpj;
.JTextField
384
} of variables declaration
Produto
9 * To change this template, choose Tools | Templates 10 * and open the templ
ate in the editor.
11 */
12 /**
13 *
14 * @author Josiel 15 */
16 public class FormularioProduto extends javax.swing.JFrame { 17
18 private Produto produto = new Produto();
19 private List<Produto> produtos = new LinkedList<>();
20 private ProdutoDao produtoDao = new ProdutoDao();
21 private List<Fornecedor> fornecedores = new LinkedList<>();
22 private FornecedorDao fornecedorDao = new FornecedorDao();
23 int posicao; 24
25
/**
26
* Creates new form FormularioFornecedor 27
*/
28 public FormularioProduto() throws Exception{
29 initComponents();
30 listarFornecedores(); 31
}
32
33 private void listarFornecedores() throws Exception {
34 fornecedores = fornecedorDao.pesquisar("");
35 cbFornecedor.addItem("Selecione um Fornecedor");
36 for (int i = 0; i < fornecedores.size(); i++) {
37 cbFornecedor.addItem(fornecedores.get(i).getNome()); 38
}
39
40
41
42
43
44
45
46
48
}
private void limparCampos() {
tfId.setText(null);
tfCodigo.setText(null);
tfNome.setText(null);
tfValor.setText(null);
cbFornecedor.setSelectedIndex(0); 47 }
49
50
51
52
53
54
55
56
57
cbFornecedor.getSelectedItem().toString()).get(0));
58
60
61
62
63
64
65
66
281
280
btCadastrarActionPerformed(java.awt.event.ActionEvent
282
283
284
285
286
sucesso!");
287
288
289
290
292
291
evt)
293
294
void
private
296
295
59 { }dispose();
67
if
produto.setId(Integer.parseInt(tfId.getText()));
produto.setCodigo(tfCodigo.getText());
produto.setNome(tfNome.getText());
produto.setValor(Double.parseDouble(tfValor.getText()));
produto.setFornecedor(
tfId.setText(String.valueOf(p.getId()));
tfCodigo.setText(p.getCodigo());
tfNome.setText(p.getNome());
tfValor.setText(String.valueOf(p.getValor()));
cbFornecedor.setSelectedItem(p.getFornecedor().getNome());
try
getFornecedor();
produtoDao.cadastrar(produto);
limparCampos();
JOptionPane.showMessageDialog(null,
private
//Cdigo
catch
(!tfId.getText().equals(""))
{ void
(Exception
omitido
getFornecedor()
setFornecedor(Produto
btFecharActionPerformed(java.awt.event.ActionEvent
ex) {fornecedorDao.pesquisar(
throws
{ p)ex);
Exception{
"Cadastrado
{
evt)
com{
btPesquisarActionPerformed(java.awt.event.ActionEvent
try
produtos
298
299
301
300
JOptionPane.showMessageDialog(null,
302
303
306
310
309
posicao++;
308
313
if
317
316
posicao--;
315
320
323
322
325
evt)
}posicao
328
setFornecedor(produtos.get(posicao));
327
private
{330
3229
97catch
04
05
07
11
12
14
18
19
21
24
26
(posicao
{ void
=(Exception
=produtos.size()
0;produtoDao.pesquisar(tfNome.getText());
><btAlterarActionPerformed(java.awt.event.ActionEvent
btProximoActionPerformed(java.awt.event.ActionEvent
btAnteriorActionPerformed(java.awt.event.ActionEvent
btPrimeiroActionPerformed(java.awt.event.ActionEvent
btUltimoActionPerformed(java.awt.event.ActionEvent
produtos.size()
0)
{ ex) { - 1;- 1) {ex);
331 try {
332 getFornecedor();
333 produtoDao.alterar(produto);
334 JOptionPane.showMessageDialog(null, "Alterado com sucesso!");
335 limparCampos();
336 } catch (Exception ex) {
337 JOptionPane.showMessageDialog(null, ex);
338
}
339
} 340
341 private void btExcluirActionPerformed(java.awt.event.ActionEvent evt) {
342 try {
343 getFornecedor();
344 produtoDao.excluir(produto.getId());
345 JOptionPane.showMessageDialog(null, "Excluido com sucesso!");
346 limparCampos();
347 } catch (Exception ex) {
348 JOptionPane.showMessageDialog(null, ex);
349
}
350
} 351
352 private void btNovoActionPerformed(java.awt.event.ActionEvent evt) {
353 limparCampos();
354
} 355
356
//Cdigo omitido 382
383 /* Create and display the form */
384 java.awt.EventQueue.invokeLater(new Runnable() {
385 public void run() {
386 try {
387 new FormularioFornecedor().setVisible(true);
388 } catch (Exception ex) {
389 JOptionPane.showMessageDialog(null, ex);
390
}
391
}
392
});
393
394
395
396
397
398
399
400
401
402
403
404
}
// Variables declaration - do not modify
private javax.swing.JButton btAlterar;
private javax.swing.JButton btAnterior;
private javax.swing.JButton btCadastrar;
private javax.swing.JButton btExcluir;
private javax.swing.JButton btFechar;
private javax.swing.JButton btNovo;
private javax.swing.JButton btPesquisar;
private javax.swing.JButton btPrimeiro;
private javax.swing.JButton btProximo;
private javax.swing.JButton btUltimo;
405
406
407
408
409
410
411
412
413
414
415
416
Entrada
btBuscarActionPerformed(java.awt.event.ActionEvent
void
private
155
156
157
158
159
160
161
162
164
163
btRegistrarActionPerformed(java.awt.event.ActionEvent
165
166
167
168
171
movimento.setQuantidade(Double.parseDouble(tfQtde.getText()));
170
movimento.setProduto(produtoDao.pesquisar(tfProduto.getText()).get(0));
169
173
172
174
175
177
176
178
179
180
181
182
183
185
184
evt)
186
187
236
215
}{ }dispose();
trycatch
new
FormularioBuscar.formulario="Entrada";
try{
ProdutoDao
Movimento
movimento.setTipo(tfTipo.getText());
movimento.setData(tfData.getText());
MovimentoDao
movimentoDao.registarMovimento(movimento);
tfProduto.setText(null);
tfQtde.setText(null);
tfData.setText(null);
}catch(Exception
JOptionPane.showMessageDialog(null,
private
{ void
FormularioBuscar().show();
(Exception
movimento
produtoDao
movimentoDao
jButton1ActionPerformed(java.awt.event.ActionEvent
ex){
ex)
= new
={new=Movimento();
ProdutoDao();
new
MovimentoDao();
"Entrada registada!");
ex);
evt) {
Busca de produtos
141
142
dispose();
}
143
/*
172
171
java.awt.EventQueue.invokeLater(new
173
public
174
try
175
new
176
177
JOptionPane.showMessageDialog(null,
178
}179
182
});
184
185
186
187
private
188
//
189
190
191
183
44catch
80
81
Create
Variables
End
{ of
}FormularioBuscar().setVisible(true);
void
(Exception
javax.swing.JButton
javax.swing.JLabel
javax.swing.JScrollPane
javax.swing.JTable
javax.swing.JTextField
andrun()
variables
declaration
display
{ex)
declaration
the
{ -form
dojButton1;
jLabel1;
tbProduto;
not
*/jScrollPane1;
tfProduto;
modify
Runnable() {
ex);
Sada
154 { private
153
155
156
157
158
159
160
161
163
162
btRegistrarActionPerformed(java.awt.event.ActionEvent
164
165
166
167
170
movimento.setQuantidade(Double.parseDouble(tfQtde.getText()));
169
movimento.setProduto(produtoDao.pesquisar(tfProduto.getText()).get(0));
168
172
171
173
174
176
175
177
178
179
180
181
182
183
184
185
187
{186
evt)
newcatch
FormularioBuscar.formulario="Sada";
try
ProdutoDao
Movimento
movimento.setTipo(tfTipo.getText());
movimento.setData(tfData.getText());
MovimentoDao
if
tfProduto.setText(null);
tfQtde.setText(null);
tfData.setText(null);
}JOptionPane.showMessageDialog(null,
else
(movimentoDao.registarMovimento(movimento))
{FormularioBuscar().show();
{void
(Exception
movimento
produtoDao
btBuscarActionPerformed(java.awt.event.ActionEvent
movimentoDao
jButton1ActionPerformed(java.awt.event.ActionEvent
ex)
= new
={new=Movimento();
ProdutoDao();
new
MovimentoDao();
"Sada registada!");
"Estoque
ex);
insuficiente");
evt)
{ {
REFENRNCIAS BIBLIOGRFICAS
TEIXEIRA, Maria. Acesso a Banco de Dados com JDBC (Java Database Connectivity) e
o Padro de Projeto DAO (Data Access Object). Disponvel em http://www.ceunes.ufes.
br/downloads/2
/mariateixeira-Java.Introdu%C3%A7%C3%A3o%20a%20Banco% 20de%20Dados.pdf. Acessado
em 17 de outubro de 2012.
CARNEIRO, Davi Luan. Introduo ao pattern DAO. Disponvel em http://javafree.uol.com.
br/artigo/871452/Introducao-ao-pattern- DAO.html. Acessado em 17 de outubro de 2
012