Guest User

Untitled

a guest
Jan 15th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.77 KB | None | 0 0
  1. package com.everis.biblioteca.dao;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.util.ArrayList;
  9. import java.util.List;
  10.  
  11. import com.everis.biblioteca.modelo.Seccion;
  12. import com.everis.biblioteca.modelo.Estanteria;
  13. import com.everis.biblioteca.modelo.Libro;
  14.  
  15. public class BibliotecaDaoSQL implements BibliotecaDao{
  16.  
  17. @Override
  18. public void darDeAltaNuevoLibro(Libro libro, Estanteria estanteria) {
  19.  
  20. Connection cx = null;
  21.  
  22. try {
  23. cx = DriverManager.getConnection("jdbc:mysql://localhost:3306/biblioteca","root","root");
  24. PreparedStatement pst = cx.prepareStatement("insert into libro (titulo,autor,editorial,numero_paginas,seccion) values (?,?,?,?,?)");
  25. pst.setString(1,libro.getTitulo());
  26. pst.setString(2,libro.getAutor());
  27. pst.setString(3,libro.getEditorial());
  28. pst.setInt(4, libro.getNumeroDePaginas());
  29.  
  30. //NO HEMOS TENIDO EN CUENTA LA SECCION! FALLO!
  31. pst.setString(5, null); //pst.setString(5, libro.getSeccion().toString());
  32.  
  33. pst.executeUpdate();
  34.  
  35. } catch (SQLException e) {
  36. e.printStackTrace();
  37. } finally {
  38. try {
  39. cx.close();
  40. } catch (SQLException e) {
  41. e.printStackTrace();
  42. }
  43. }
  44.  
  45.  
  46. }
  47.  
  48. @Override
  49. public void borrarLibro(Integer idLibro) {
  50. // TODO Auto-generated method stub
  51.  
  52. }
  53.  
  54. @Override
  55. public void prestarLibro(Integer idLibro) {
  56. // TODO Auto-generated method stub
  57.  
  58. }
  59.  
  60. @Override
  61. public void devolverLibro(Integer idLibro) {
  62. // TODO Auto-generated method stub
  63.  
  64. }
  65.  
  66. @Override
  67. public List<Libro> buscarLibroPorTitulo(String titulo) {
  68. // TODO Auto-generated method stub
  69. return null;
  70. }
  71.  
  72. @Override
  73. public List<Libro> buscarLibroPorAutor(String autor) {
  74. List<Libro> libros = new ArrayList<Libro>();
  75.  
  76. Connection cx = null;
  77.  
  78. try {
  79. cx = DriverManager.getConnection("jdbc:mysql://localhost:3306/biblioteca","root","root");
  80. PreparedStatement pst = cx.prepareStatement("select * from libro where autor=?");
  81. pst.setString(1,autor);
  82.  
  83. ResultSet rs = pst.executeQuery();
  84. while(rs.next()){
  85. Libro lAux = new Libro( rs.getInt("ID_LIBRO"),
  86. rs.getString("TITULO"),
  87. rs.getString("AUTOR"),
  88. rs.getString("EDITORIAL"),
  89. rs.getInt("NUMERO_PAGINAS"),
  90. null, //SE NOS OLVIDÓ LA SECCION EN INSERTAR
  91. null);
  92.  
  93. libros.add(lAux);
  94. }
  95.  
  96. } catch (SQLException e) {
  97. e.printStackTrace();
  98. } finally {
  99. try {
  100. cx.close();
  101. } catch (SQLException e) {
  102. e.printStackTrace();
  103. }
  104. }
  105.  
  106.  
  107. return libros;
  108. }
  109.  
  110. @Override
  111. public Libro buscarLibroPorId(Integer idLibro) {
  112. // TODO Auto-generated method stub
  113. return null;
  114. }
  115.  
  116. }
Add Comment
Please, Sign In to add comment