-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathControl_Banco.java
117 lines (99 loc) · 3.02 KB
/
Control_Banco.java
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
import java.sql.*;
import java.util.*;
public class Control_Banco {
private Connection conexao;
public Control_Banco(){
conexao = new FabricaDeConexoes().solicitaConexao("localhost","Concessionaria","root","");
}
public void cadastrarVenda(Venda v){
String sql = "insert into Venda "+
"(codvenda, forma_de_pagamento, codcarro, codcliente, codatendente) "+
"values (?,?,?,?,?)";
try{
PreparedStatement ps = conexao.prepareStatement(sql);
ps.setString(1,v.getCodvenda());
ps.setString(2,v.getFormaPagamento());
ps.setString(3,v.getCodcarro());
ps.setString(4,v.getCodcliente());
ps.setString(5,v.getCodatendente());
ps.execute();
ps.close();
}catch (SQLException e){
throw new RuntimeException(e);
}
}
//Apartir daqui somente será possível consultar, alterar, listar ou excluir a venda e não o cliente.
// Para entendermos que o cadastro do cliente é único! mas a venda pode mudar
public Venda pesquisaVenda(String pe){
Venda v = new Venda();
try{
String sql = "select * from Venda where codvenda like ?";
PreparedStatement ps = conexao.prepareStatement(sql);
ps.setString(1,"%"+pe+"%");
ResultSet rs = ps.executeQuery();
v.setCodvenda("0");
while(rs.next()){
if(rs.getString("codvenda").equals(pe)){
v.setCodvenda(rs.getString("codvenda"));
v.setFormaPagamento(rs.getString("forma_de_pagamento"));
v.setCodcarro(rs.getString("codcarro"));
v.setCodcliente(rs.getString("codcliente"));
v.setCodatendente(rs.getString("codatendente"));
}
}
rs.close();
ps.close();
return v;
}catch (SQLException e){
throw new RuntimeException(e);
}
}
public void altera(Venda v){
String sql ="update Venda set "+
"forma_de_pagamento=?, codcarro=?"+
" where codvenda=?";
try{
PreparedStatement ps=conexao.prepareStatement(sql);
ps.setString(1,v.getFormaPagamento());
ps.setString(2,v.getCodcarro());
ps.setString(3,v.getCodvenda());
ps.execute();
ps.close();
}catch (SQLException e){
throw new RuntimeException (e);
}
}
public List<Venda> listar(){
try{
List<Venda> vendas = new ArrayList<Venda>();
PreparedStatement ps = conexao.prepareStatement("Select * from Venda");
ResultSet res = ps.executeQuery();
while(res.next()){
Venda v = new Venda();
v.setCodvenda(res.getString("codvenda"));
v.setCodatendente(res.getString("codatendente"));
v.setCodcarro(res.getString("codcarro"));
v.setCodcliente(res.getString("codcliente"));
v.setFormaPagamento(res.getString("forma_de_pagamento"));
vendas.add(v);
}
res.close();
ps.close();
return vendas;
}
catch (SQLException e){
throw new RuntimeException(e);
}
}
public void apaga(String a){
String sql = "delete from Venda "+"where codvenda=?";
try{
PreparedStatement ps = conexao.prepareStatement(sql);
ps.setString(1,a);
ps.execute();
ps.close();
}catch (SQLException e){
throw new RuntimeException (e);
}
}
}