Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * 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 com.dao;
- import com.model.Articulo;
- import com.utils.Conexion;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.ArrayList;
- import java.util.List;
- /**
- *
- * @author forense
- */
- public class ArticulosDAO {
- public int save(Articulo a) throws SQLException{
- String sql = "Insert into inv_articulos ( "
- + " id_articulo"
- + ", nombre"
- + ", descripcion"
- + " )"
- + " values "
- + " ( ?, ?, ? ? )";
- Connection conn = Conexion.getConnection();
- PreparedStatement stmt = conn.prepareStatement(sql);
- stmt.setInt(1, a.getIdArticulo());
- stmt.setString(2, a.getNombre());
- stmt.setString(3, a.getDescripcion());
- int registros = stmt.executeUpdate();
- stmt.close();
- return registros;
- }
- public int update(Articulo a) throws SQLException{
- String sql = "UPDATE inv_articulos " +
- " SET nombre = ?," +
- " descripcion = ?," +
- " WHERE id_articulo =?";
- Connection conn = Conexion.getConnection();
- PreparedStatement stmt = conn.prepareStatement(sql);
- stmt.setString(2, a.getNombre());
- stmt.setString(3, a.getDescripcion());
- stmt.setInt(1, a.getIdArticulo());
- int registros = stmt.executeUpdate();
- stmt.close();
- return registros;
- }
- public int delete(Articulo a) throws SQLException{
- String sql = "DELETE FROM inv_articulos WHERE id_articulo=?";
- Connection conn = Conexion.getConnection();
- PreparedStatement stmt = conn.prepareStatement(sql);
- stmt.setInt(1, a.getIdArticulo());
- int registros = stmt.executeUpdate();
- return registros;
- }
- public static Articulo getArticuloById(int idArticulo) throws SQLException{
- String sql = "SELECT * FROM inv_articulos WHERE id= ? ";
- Connection conn = Conexion.getConnection();
- PreparedStatement stmt = conn.prepareStatement(sql);
- stmt.setInt(1, idArticulo);
- ResultSet res = stmt.executeQuery();
- Articulo a = null;
- if (res.next()) {
- a = new Articulo();
- a.setIdArticulo(idArticulo);
- a.setNombre(res.getString("nombre"));
- a.setNombre(res.getString("descripcion"));
- }
- return a;
- }
- public static List<Articulo> getAllArticulos() throws SQLException{
- System.out.println( ":1:getAllArticulos::");
- List <Articulo> list = new ArrayList<Articulo>();
- String sql = "SELECT * FROM inv_articulos";
- Connection conn = Conexion.getConnection();
- System.out.println( ":2:getAllArticulos::");
- PreparedStatement stmt = conn.prepareStatement(sql);
- ResultSet res = stmt.executeQuery();
- System.out.println( ":3:getAllArticulos::");
- if (res.next()) {
- Articulo a = new Articulo();
- a.setIdArticulo(res.getInt("id_articulo"));
- a.setNombre(res.getString("nombre"));
- a.setNombre(res.getString("descripcion"));
- System.out.println( " :4:getAllArticulos---");
- list.add(a);
- }
- System.out.println( " :5:getAllArticulos---<");
- return list;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement