insert data from an array into the database - java

Asked

Viewed 1,217 times

1

I am creating a system that at a certain point should create an order and this request has several items that I put inside an arrayList. I have an order table in the database, in which way I will create my method in the DAO to save the order items in the requested table, being of the type arrayList.

Request 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);
}

Code of the Pedidodao:

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();        
    }
  • 1

    Josiane, in your database do you use an order table and a menu table? If yes, you can first enter the request, generate an ID for it, and use a 'for' to insert the menu into another table with the corresponding request ID. If this is not the case, post as is the structure of your tables.

  • In my database I have only the order and product table, I had not done the normalization of data creating another table Ex.: Product Pedido_product. I will make the project change by creating this other table and the methods. Then put here if it worked out. Thank you.

1 answer

1

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 stating the order ID and the item ID. If Order 001 has "silver1" and "silver2" items you will then enter 2 records in the order table.

Order 001 - ITEM 0001 Order 001 - ITEM 0003

When reading this table you can filter by request. select * from orders Where request.id = 001; and it handles all dishes/items that that order had.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.