Package com.comprainsumos.MPersistencia

Source Code of com.comprainsumos.MPersistencia.MPInsumos

package com.comprainsumos.MPersistencia;

import com.comprainsumos.modelo.Insumo;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

public class MPInsumos {

    public static ManejadorBaseDatos mbd = ManejadorBaseDatos.getInstancia();
    public static Connection con;

    public static Insumo getInsumos(String codigo) throws SQLException, Exception {
        mbd.conectar();
        con = mbd.getConexion();

        if (codigo == null) {
            throw new SQLException("No hay elemento clave de la clase insumos");
        }
        ResultSet rs = null;
        PreparedStatement pst = null;
        Insumo insumos = null;
        try {
            pst = con.prepareStatement("select * from insumos where codigo = ?");
            pst.setString(1, codigo.trim());
            rs = pst.executeQuery();
            while (rs.next()) {
                insumos = Insumo.load(rs);
            }
        } finally {
            if (rs != null) {
                rs.close();
            }
            if (pst != null) {
                pst.close();
            }
            return insumos;
        }
    }
    //Metodo que recibe como parametro un objeto de tipo Insumos para ingresarlo a la base de datos

    public static void crearInsumos(Insumo insumos) throws SQLException, Exception {
        mbd.conectar();
        con = mbd.getConexion();
        if (con == null) {
            throw new SQLException(" no hay conexion ");
        }
        PreparedStatement pst = null;
        try {
            pst = con.prepareStatement("Insert Into insumos values(?,?,?,?,?)");
            pst.setInt(1, insumos.getCodigo());
            pst.setString(2, insumos.getNombre());
            pst.setDouble(3, insumos.getCantidad());
            pst.setDouble(4, insumos.getStockMin());
            pst.setString(5, insumos.getUnidadmedia());
            pst.executeUpdate();
        } finally {
            if (pst != null) {
                pst.close();
            }
        }
    }

    public static void delInsumos(int codigo) throws SQLException, Exception {
        mbd.conectar();
        con = mbd.getConexion();

        if (codigo == 0) {
            throw new SQLException("No hay elemento clave de la clase insumos");
        }
        PreparedStatement pst = null;

        try {
            pst = con.prepareStatement("delete from insumos where codigo = ?");
            pst.setInt(1, codigo);
            pst.executeUpdate();

        } finally {
        }

    }
    //Metodo que recibe como parametro un objeto de tipo Insumos para acualizar un registro existente

    public static Insumo modInsumos(Insumo insumos) throws SQLException, Exception {
        mbd.conectar();
        con = mbd.getConexion();


        if (insumos == null) {
            throw new SQLException("No hay elemento clave de la clase insumos");
        }
        Insumo insu = null;
        PreparedStatement pst = null;

        try {
            //Construimos la sentencia SQL para actializar un registro existente
            pst = con.prepareStatement("UPDATE insumos SET  nombre = '" + insumos.getNombre() + "'"
                    + " ,cantidad = '" + insumos.getCantidad() + "', "
                    + "stockmin='" + insumos.getStockMin() + "',"
                    + " unidadmedia = '" + insumos.getUnidadmedia() + "' WHERE codigo = ?");
            pst.setInt(1, insumos.getCodigo());

            pst.executeUpdate();

        } finally {
        }

        return insu;

    }

    public static List<Insumo> listar() throws Exception {
        mbd.conectar();
        con = mbd.getConexion();
        ResultSet rs = null;
        PreparedStatement pst = null;
        List<Insumo> lista = new LinkedList();
        try {
            pst = con.prepareStatement("SELECT * FROM insumos");
            rs = pst.executeQuery();
            while (rs.next()) {
                lista.add(Insumo.load(rs));
            }
        } finally {
            if (rs != null) {
                rs.close();
            }
            if (pst != null) {
                pst.close();
            }
        }
        return lista;
    }

}
TOP

Related Classes of com.comprainsumos.MPersistencia.MPInsumos

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.