insert data from an array into the database - java

0

I am creating a system that at some point must create a request and this request has several items that I put inside a ListList. I have a request table in the database, how will I create my method in DAO to save the order items in the order table, being they are of type arrayList.

Ordering code:

package br.com.pedidosCongelados.domain;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;

public class Pedido {

    private long numPedido;
    private Date dataPedido;
    private ArrayList<CardapioIngrediente> cardapio;
    private Cliente cliente;
    private BigDecimal valor;

    public long getNumPedido() {
        return numPedido;
    }

    public void setNumPedido(long numPedido) {
        this.numPedido = numPedido;
    }

    public Date getDataPedido() {
        return dataPedido;
    }

    public void setDataPedido(Date dataPedido) {
        this.dataPedido = dataPedido;
    }

    public Cliente getCliente() {
        return cliente;
    }

    public void setCliente(Cliente cliente) {
        this.cliente = cliente;
    }

    public BigDecimal getValor() {
        return valor;
    }

    public void setValor(BigDecimal valor) {
        this.valor = valor;
    }

    public ArrayList<CardapioIngrediente> getCardapio() {
        return cardapio;
    }

public void setCardapio(ArrayList<CardapioIngrediente> cardapio) {
    this.cardapio = cardapio;
}

public void calcularValorTotal() {
    BigDecimal total = BigDecimal.ZERO;

    for (CardapioIngrediente cardapio : this.getCardapio()) {
        if (cardapio.getIdCardapio() != 0 ) {
            total = total.add(cardapio.getValor());
        }
    }

    this.setValor(total);
}

Order Code:

package br.com.pedidosCongelados.dao;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import br.com.pedidosCongelados.domain.CardapioIngrediente;
import br.com.pedidosCongelados.domain.Pedido;
import br.com.pedidosCongelados.factory.ConexaoFactory;

public class PedidoDAO {

    public void salvar(Pedido p) throws SQLException{
        StringBuilder sql = new StringBuilder();
        sql.append("INSERT INTO pedido");
        sql.append("(dataPedido, cardapio_idcardapio, cliente_idcliente, valor)");
        sql.append("VALUES (?, ?, ?, ?)");

        Connection conexao = ConexaoFactory.conectar();

        PreparedStatement comando = conexao.prepareStatement(sql.toString());

        comando.setDate(1, (Date) p.getDataPedido());
        comando.setLong(2, p.getCardapio().getIdCardapio());//essa linha não consegui desenvolver.
        comando.setLong(3, p.getCliente().getIdcliente()); 
        comando.setBigDecimal(4, p.getValor());

        comando.executeUpdate();        
    }
    
asked by anonymous 31.05.2017 / 04:51

1 answer

0

You have the 1: N ratio, so create the menu table with all possible items. For each item you enter a record in the requested table, informing the order ID and item ID. If Order 001 has the items "plate1" and "plate2" then you will enter 2 records in the orders table.

Request 001 - ITEM 0001 Request 001 - ITEM 0003

When you read this table you can filter by request. select * from requests where request.id = 001; and he treats all the dishes / items that that order had.

    
02.06.2017 / 21:25