Advertisement
Guest User

Untitled

a guest
Dec 7th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.79 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package com.dao;
  7.  
  8. import com.model.Articulo;
  9. import com.utils.Conexion;
  10. import java.sql.Connection;
  11. import java.sql.PreparedStatement;
  12. import java.sql.ResultSet;
  13. import java.sql.SQLException;
  14. import java.util.ArrayList;
  15. import java.util.List;
  16.  
  17. /**
  18. *
  19. * @author forense
  20. */
  21. public class ArticulosDAO {
  22. public int save(Articulo a) throws SQLException{
  23. String sql = "Insert into inv_articulos ( "
  24. + " id_articulo"
  25. + ", nombre"
  26. + ", descripcion"
  27. + " )"
  28. + " values "
  29. + " ( ?, ?, ? ? )";
  30.  
  31. Connection conn = Conexion.getConnection();
  32. PreparedStatement stmt = conn.prepareStatement(sql);
  33. stmt.setInt(1, a.getIdArticulo());
  34. stmt.setString(2, a.getNombre());
  35. stmt.setString(3, a.getDescripcion());
  36. int registros = stmt.executeUpdate();
  37. stmt.close();
  38. return registros;
  39. }
  40.  
  41. public int update(Articulo a) throws SQLException{
  42. String sql = "UPDATE inv_articulos " +
  43. " SET nombre = ?," +
  44. " descripcion = ?," +
  45. " WHERE id_articulo =?";
  46. Connection conn = Conexion.getConnection();
  47. PreparedStatement stmt = conn.prepareStatement(sql);
  48. stmt.setString(2, a.getNombre());
  49. stmt.setString(3, a.getDescripcion());
  50. stmt.setInt(1, a.getIdArticulo());
  51.  
  52. int registros = stmt.executeUpdate();
  53. stmt.close();
  54. return registros;
  55. }
  56.  
  57. public int delete(Articulo a) throws SQLException{
  58. String sql = "DELETE FROM inv_articulos WHERE id_articulo=?";
  59. Connection conn = Conexion.getConnection();
  60. PreparedStatement stmt = conn.prepareStatement(sql);
  61. stmt.setInt(1, a.getIdArticulo());
  62. int registros = stmt.executeUpdate();
  63. return registros;
  64. }
  65.  
  66. public static Articulo getArticuloById(int idArticulo) throws SQLException{
  67. String sql = "SELECT * FROM inv_articulos WHERE id= ? ";
  68. Connection conn = Conexion.getConnection();
  69. PreparedStatement stmt = conn.prepareStatement(sql);
  70. stmt.setInt(1, idArticulo);
  71. ResultSet res = stmt.executeQuery();
  72.  
  73. Articulo a = null;
  74. if (res.next()) {
  75. a = new Articulo();
  76. a.setIdArticulo(idArticulo);
  77. a.setNombre(res.getString("nombre"));
  78. a.setNombre(res.getString("descripcion"));
  79. }
  80. return a;
  81. }
  82.  
  83. public static List<Articulo> getAllArticulos() throws SQLException{
  84.  
  85. System.out.println( ":1:getAllArticulos::");
  86.  
  87. List <Articulo> list = new ArrayList<Articulo>();
  88. String sql = "SELECT * FROM inv_articulos";
  89. Connection conn = Conexion.getConnection();
  90. System.out.println( ":2:getAllArticulos::");
  91. PreparedStatement stmt = conn.prepareStatement(sql);
  92. ResultSet res = stmt.executeQuery();
  93. System.out.println( ":3:getAllArticulos::");
  94. if (res.next()) {
  95. Articulo a = new Articulo();
  96. a.setIdArticulo(res.getInt("id_articulo"));
  97. a.setNombre(res.getString("nombre"));
  98. a.setNombre(res.getString("descripcion"));
  99. System.out.println( " :4:getAllArticulos---");
  100. list.add(a);
  101. }
  102. System.out.println( " :5:getAllArticulos---<");
  103. return list;
  104. }
  105.  
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement