FacturaDAO.java Source code

Java tutorial

Introduction

Here is the source code for FacturaDAO.java

Source

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author luisjo
 */
public class FacturaDAO implements FacturaDAOInterface {

    private JdbcTemplate jdbcTemplate;

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    @Override
    public void alta(Factura f) {
        String inserQuery = "insert into TFacturas (idFactura, idCliente, Fecha, Importe) values (?, ?, ?, ?) ";
        Object[] params = new Object[] { f.getIdFactura(), f.getIdCliente(), f.getFecha(), f.getImporte() };
        int[] types = new int[] { Types.INTEGER, Types.INTEGER, Types.DATE, Types.DECIMAL };
        jdbcTemplate.update(inserQuery, params, types);
    }

    @Override
    public void baja(int idFactura) {
        String updateQuery = "delete from TFacturas where IdFactura = ?";
        Object[] params = new Object[] { idFactura };
        jdbcTemplate.update(updateQuery, params);
    }

    @Override
    public ArrayList<Factura> consulta(String idCliente) {
        String selQuery = "select * from TFacturas where IdFactura = ?";
        ArrayList<Factura> f = (ArrayList) jdbcTemplate.query(selQuery, new Object[] { idCliente },
                new FacturaMapper());
        return f;
    }

    private static class FacturaMapper implements RowMapper<Factura> {

        public FacturaMapper() {
        }

        @Override
        public Factura mapRow(ResultSet rs, int i) throws SQLException {

            Factura f = new Factura();

            f.setIdFactura(rs.getInt("idFactura"));
            f.setIdCliente(rs.getString("idCliente"));
            f.setFecha(rs.getDate("Fecha"));
            f.setImporte(rs.getDouble("Importe"));

            return f;
        }
    }
}