Package com.comprainsumos.MPersistencia

Source Code of com.comprainsumos.MPersistencia.MProveedor

package com.comprainsumos.MPersistencia;


import com.comprainsumos.modelo.Insumo;
import com.comprainsumos.modelo.Proveedor;
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 MProveedor {
   
    public static ManejadorBaseDatos mbd=ManejadorBaseDatos.getInstancia();
    public static Connection con;
   
    public static Proveedor getProveedor(String nit) throws SQLException, Exception {
            mbd.conectar();
            con=mbd.getConexion();

            if (nit == null) {
                throw new SQLException("No hay elemento clave de la clase proveedor");
            }
                ResultSet rs = null;
                PreparedStatement pst = null;
                Proveedor proveedor = null;
                try {
                pst = con.prepareStatement("select * from proveedor where nit = ?");
                pst.setString(1, nit.trim());
                rs = pst.executeQuery();
            while(rs.next()) {
                proveedor = Proveedor.load(rs);
            }
            } finally {
            if (rs != null) {
                rs.close();
            }
            if (pst != null) {
                pst.close();
            }
            return proveedor;
        }
    }
   
    //Metodo que recibe como parametro un objeto de tipo Proveedor para ingresarlo a la base de datos
    public static void crearProveedor(Proveedor proveedor) 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 proveedor values(?,?,?,?,?)");
            pst.setInt(1,proveedor.getCodigo());
            pst.setString(2,proveedor.getNit());
            pst.setString(3,proveedor.getNombre());
            pst.setString(4,proveedor.getDireccion());
            pst.setString(5,proveedor.getEmail());
          
            pst.executeUpdate();
        }finally {
            if (pst != null) {
                pst.close();
            }
        }
    } 
   
    public static void delProveedores(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 proveedor 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 Proveedor modProveedores(Proveedor proveedor) throws SQLException, Exception {
        mbd.conectar();
        con=mbd.getConexion();
       
             
        if (proveedor == null) {
                throw new SQLException("No hay elemento clave de la clase Proveedor");
            }
                PreparedStatement pst = null;
               
            try {
                //Construimos la sentencia SQL para actializar un registro existente
                pst = con.prepareStatement(" update proveedor set  nit = "+"'"+proveedor.getNit()+"'"+
                                           ",nombre ="+"'"+proveedor.getNombre()+"'"+
                                           ",direccion ="+"'"+proveedor.getDireccion()+"'"+
                                           ",email ="+"'"+proveedor.getEmail()+"'"+" where codigo = ?");
               
                pst.setInt(1,proveedor.getCodigo());
              
                pst.executeUpdate();
          
            } finally {
            }
           
            return proveedor;
           
        }
   
    public static List<Proveedor> listar() throws Exception {
        mbd.conectar();
        con = mbd.getConexion();
        ResultSet rs = null;
        PreparedStatement pst = null;
        List<Proveedor> lista = new LinkedList();
        try {
            pst = con.prepareStatement("SELECT * FROM proveedor");
            rs = pst.executeQuery();
            while (rs.next()) {
                lista.add(Proveedor.load(rs));
            }
        } finally {
            if (rs != null) {
                rs.close();
            }
            if (pst != null) {
                pst.close();
            }
        }
        return lista;
    }
   
}
TOP

Related Classes of com.comprainsumos.MPersistencia.MProveedor

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.