Como faço para inserir varios produtos em uma mesma venda no banco de dados mysql, pois estou desenvolvendo um programa desktop no netbeans e quando realizo uma venda com varios produtos, insere apenas um produto no bando de dados e sendo que a venda consta de varios produtos. Aqui segue o codigo:
private void btCancelarActionPerformed(java.awt.event.ActionEvent evt) {
cancelarVenda();
}
private void cancelarVenda(){
while (tmVendas.getRowCount() > 0){
tmVendas.removeRow(0);
}
total = 0;
ftfTotal.setValue(0);
venda.clear();
this.dispose();
}
private void btFinalizarActionPerformed(java.awt.event.ActionEvent evt) {
cadastrarVenda();
}
private void cadastrarVenda(){
if (venda.size() == 0){
JOptionPane.showMessageDialog(this, "Inclua pelo menos um produto!");
}else{
try {
VendasControl vc = new VendasControl();
SimpleDateFormat formato = new SimpleDateFormat("yyyy-MM-dd");
java.util.Date d = new java.util.Date();
Date data = Date.valueOf(formato.format(d));
int codigo = vc.buscarCodigoUltimaVenda() + 1;
for (int i = 0; i < venda.size(); i++){
venda.get(i).setClientes_codigo(clientes.get(cbClientes.getSelectedIndex()).getCodigo());
venda.get(i).setUsuarios_codigo(usuarios.get(cbUsuarios.getSelectedIndex()).getCodigo());
venda.get(i).setProdutos_nome(produtos.get(cbProdutos.getSelectedIndex()).getNome());
venda.get(i).setData_venda(data);
venda.get(i).setValor_total(total);
venda.get(i).setCodigo(codigo);
vc.cadastrarVenda(venda.get(i));
}
JOptionPane.showMessageDialog(this, "Venda cadastrada com sucesso!");
} catch (Exception ex) {
JOptionPane.showMessageDialog(this, "Erro ao cadastrar Venda!");
}
venda.clear();
cleartmVendas();
}
}
private void btIncluirActionPerformed(java.awt.event.ActionEvent evt) {
incluirProduto();
}
private void incluirProduto(){
if (verificarQuantidade()){
VendasBean vb = new VendasBean();
vb.setClientes_codigo(clientes.get(cbClientes.getSelectedIndex()).getCodigo());
vb.setUsuarios_codigo(usuarios.get(cbUsuarios.getSelectedIndex()).getCodigo());
vb.setProdutos_nome(produtos.get(cbProdutos.getSelectedIndex()).getNome());
vb.setQuantidade(Integer.parseInt(String.valueOf(ftfQuantidade.getValue())));
venda.add(vb);
String produto = produtos.get(cbProdutos.getSelectedIndex()).getNome();
String cliente = clientes.get(cbClientes.getSelectedIndex()).getNome();
String usuario = usuarios.get(cbUsuarios.getSelectedIndex()).getLogin();
String quantidade = String.valueOf(ftfQuantidade.getValue());
String valor = String.valueOf(produtos.get(cbProdutos.getSelectedIndex()).getValor() * Integer.parseInt(quantidade));
String [] campos = new String [] {usuario, produto, quantidade, valor};
tmVendas.addRow(campos);
total += Double.parseDouble(valor);
ftfTotal.setValue(total);
} else {
JOptionPane.showMessageDialog(this, "Quantidade inválida!");
ftfQuantidade.requestFocus();
}
}
private void cleartmVendas(){
int rowsCount = tmVendas.getRowCount();
for(int i = 0; i < rowsCount; i++){
tmVendas.removeRow(0);
}
ftfTotal.setText("");
}
private boolean verificarQuantidade(){
try {
if (Integer.parseInt(String.valueOf(ftfQuantidade.getValue())) > 0){
return true;
}
} catch (NumberFormatException ex) {
ex.printStackTrace();
}
return false;
}
a classe que insere dados no banco é essa:
public void cadastrarVenda(VendasBean venda){
try {
Connection ExConn = banco.conecta();
String sSql = "INSERT INTO VENDAS (CODIGO,USUARIOS_CODIGO, PRODUTOS_NOME, CLIENTES_CODIGO, DATA_VENDA, VALOR_TOTAL, QUANTIDADE) VALUES(?,?,?,?,?,?,?)";
PreparedStatement stm = ExConn.prepareStatement(sSql);
stm.setInt(1, venda.getCodigo());
stm.setInt(2, venda.getUsuarios_codigo());
stm.setString(3, venda.getProdutos_nome());
stm.setInt(4, venda.getClientes_codigo());
stm.setDate(5, venda.getData_venda());
stm.setDouble(6, venda.getValor_total());
stm.setInt(7, venda.getQuantidade());
stm.executeUpdate();
banco.desconecta();
} catch (Exception ex) {
ex.printStackTrace();
}
}
por favor me ajudem…