Advertisement
JVFabia

actorDAO

Oct 9th, 2020
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.00 KB | None | 0 0
  1. package com.forge.project.jdbc.dao;
  2.  
  3. import com.forge.project.jdbc.ConnectionManager;
  4. import com.forge.project.jdbc.dto.Actor;
  5. import com.forge.project.jdbc.dto.actor;
  6.  
  7. import java.sql.Connection;
  8. import java.sql.PreparedStatement;
  9. import java.sql.ResultSet;
  10. import java.sql.SQLException;
  11. import java.util.LinkedList;
  12. import java.util.List;
  13.  
  14. public class ActorDAO {
  15.     private Connection connection;
  16.  
  17.     public ActorDAO() throws SQLException {
  18.         this.connection = ConnectionManager.obtenerConexion();
  19.     }
  20.  
  21.     public Actor obtenerActorPorNombre(String nombre) throws SQLException {
  22.         String sql = "select nombre, genero from actor where nombre=?";
  23.         PreparedStatement ps = connection.prepareStatement(sql);
  24.         ps.setString(1, nombre);
  25.         ResultSet rs = ps.executeQuery();
  26.         if(rs.next()) {
  27.             Actor a = new Actor(rs.getString("nombre"), rs.getString("genero"));
  28.             return a;
  29.         }
  30.         return new Actor("", " ");
  31.     }
  32.  
  33.  
  34.     public List<Actor> obtenerActorPorNombreLike(String nombre) throws SQLException {
  35.         String sql = "select nombre, genero from actor where nombre like ?";
  36.         PreparedStatement ps = connection.prepareStatement(sql);
  37.         ps.setString(1, nombre);
  38.         List<Actor> resultado = new LinkedList<>();
  39.         ResultSet rs = ps.executeQuery();
  40.         while(rs.next()){
  41.             Actor a = new Actor(rs.getString("nombre"), rs.getString("genero"));
  42.             resultado.add(a);
  43.         }
  44.         return resultado;
  45.     }
  46.  
  47.  
  48.  
  49.     public Actor obtenerActor(String nombre) throws SQLException{
  50.         String sql = "select nombre, genero from actor where nombre = ?";
  51.         PreparedStatement ps = connection.prepareStatement(sql);
  52.         ps.setString(1, nombre);
  53.         ResultSet rs = ps.executeQuery();
  54.         if(rs.next()){
  55.             Actor a  = new Actor(rs.getString("nombre"), rs.getString("genero"));
  56.             return a;
  57.         }
  58.         return new Actor("","");
  59.     }
  60.  
  61.  
  62.  
  63. }
  64.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement