Advertisement
Guest User

Untitled

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