Spring.Repaso01.ClienteDAO.java Source code

Java tutorial

Introduction

Here is the source code for Spring.Repaso01.ClienteDAO.java

Source

/*
 * 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.
 */

package Spring.Repaso01;

import java.sql.Types;
import java.util.ArrayList;
import java.util.List;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

/**
 *
 * @author stdeceiver
 */
public class ClienteDAO implements ClienteDAOinterface {

    private JdbcTemplate jdbcTemplate;

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

    @Override
    public void alta(Cliente c) {
        String inserQuery = "insert into Cliente (idCliente, Nombre, Ape1, Ape2, Saldo) values (?, ?, ?, ?, ?) ";
        Object[] params = new Object[] { c.getIdCliente(), c.getNombre(), c.getApe1(), c.getApe2(), c.getSaldo() };
        int[] types = new int[] { Types.INTEGER, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.DECIMAL };
        jdbcTemplate.update(inserQuery, params, types);
    }

    @Override
    public void baja(int idCliente) {
        String delQuery = "delete from Cliente where idCliente = ?";
        jdbcTemplate.update(delQuery, new Object[] { idCliente });
    }

    @Override
    public void modificacion(Cliente c) {
        String modQuery = "update Cliente set Nombre = ?, Ape1 = ?, Ape2 = ?, Saldo = ? where idCliente = ?";
        Object[] params = new Object[] { c.getNombre(), c.getApe1(), c.getApe2(), c.getSaldo(), c.getIdCliente() };
        int[] types = new int[] { Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.DECIMAL, Types.INTEGER };
        jdbcTemplate.update(modQuery, params, types);
    }

    @Override
    public Cliente consulta(int idCliente) {
        String selQuery = "select * from Cliente where idCliente = ?";
        Cliente cliente = (Cliente) jdbcTemplate.queryForObject(selQuery, new Object[] { idCliente },
                new BeanPropertyRowMapper(Cliente.class));
        return cliente;
    }

    @Override
    public ArrayList<Cliente> consultaTodos() {
        String selQuery = "select * from Cliente";
        List cliente = jdbcTemplate.query(selQuery, new BeanPropertyRowMapper(Cliente.class));
        return (ArrayList) cliente;
    }

}