Solución Actor

package com.trifulcas.DAO;

public class Actor {
	private int actor_id;
	private String first_name;
	private String last_name;
	public Actor(int actor_id, String first_name, String last_name) {
		super();
		this.actor_id = actor_id;
		this.first_name = first_name;
		this.last_name = last_name;
	}
	public int getActor_id() {
		return actor_id;
	}
	public void setActor_id(int actor_id) {
		this.actor_id = actor_id;
	}
	public String getFirst_name() {
		return first_name;
	}
	public void setFirst_name(String first_name) {
		this.first_name = first_name;
	}
	public String getLast_name() {
		return last_name;
	}
	public void setLast_name(String last_name) {
		this.last_name = last_name;
	}
	@Override
	public String toString() {
		return "Actor [actor_id=" + actor_id + ", first_name=" + first_name + ", last_name=" + last_name + "]";
	}
	
	
	
}

package com.trifulcas.DAO;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ActorDAO {
	private Connection con;
	private PreparedStatement st;
	private ResultSet rs;

	public ActorDAO() {
		try {
			// Nos conectamos en el constructor, la variable con estará disponible
			// para todas las funciones de la clase DAO
			Class.forName("com.mysql.cj.jdbc.Driver");
			con = DriverManager.getConnection("jdbc:mysql://localhost:3306/sakila", "root", "");

		} catch (Exception ex) {
			System.out.println(ex.getMessage());
		}
	}

	public void close() {
		try {
			con.close();
			st.close();
			rs.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	public Actor getActor(int id) {
		try {
			// Lo hago igual que hasta ahora, SQL, STATEMENT, RESULTSET
			String sql = "select * from actor where actor_id=?";
			st = con.prepareStatement(sql);
			st.setInt(1, id);
			rs = st.executeQuery();
			// Si hay resultado construyo un país con los datos que me devuelve la consulta
			if (rs.next()) {
				return new Actor(rs.getInt("actor_id"), rs.getString("first_name"), rs.getString("last_name"));
			}
		} catch (Exception ex) {
			System.out.println(ex.getMessage());
		}
		return null;
	}
	public List<Actor> getActors(String texto){
		try {
			List<Actor> actores = new ArrayList<>();
			// Lo hago igual que hasta ahora, SQL, STATEMENT, RESULTSET
			String sql = "select * from actor where first_name like ? or last_name like ?";
			st = con.prepareStatement(sql);
			st.setString(1, "%" + texto + "%");
			st.setString(2, "%" + texto + "%");
			rs = st.executeQuery();
			// Si hay resultado construyo un país con los datos que me devuelve la consulta
			while (rs.next()) {
				actores.add(new Actor(rs.getInt("actor_id"), rs.getString("first_name"), rs.getString("last_name")));
			}
			return actores;
		} catch (Exception ex) {
			System.out.println(ex.getMessage());
		}
		return null;
	}
	public List<Actor> getActors(){
		try {
			
			return getActors("");
		} catch (Exception ex) {
			System.out.println(ex.getMessage());
		}
		return null;
	}
	
	public int addActor(String first, String last) {
		try {
			// Creo el sql
			String sql = "insert into actor(first_name, last_name) values (?,?)";
			st = con.prepareStatement(sql);
			// Añado el parámetro
			st.setString(1, first);
			st.setString(2, last);
			// Ejecuto
			return st.executeUpdate();
		} catch (Exception ex) {
			System.out.println(ex.getMessage());
		}
		return 0;
	}
	public int addActor(Actor actor) {
		try {
			
			// Ejecuto
			return addActor(actor.getFirst_name(),actor.getLast_name());
		} catch (Exception ex) {
			System.out.println(ex.getMessage());
		}
		return 0;
	}
	public int updateActor(Actor actor) {
		try {
			String sql = "update actor set first_name=?,last_name=? where actor_id=?";
			st = con.prepareStatement(sql);
			// Añado el parámetro
			st.setString(1, actor.getFirst_name());
			st.setString(2, actor.getLast_name());
			st.setInt(3, actor.getActor_id());
			// Ejeceuto
			return st.executeUpdate();

		} catch (Exception ex) {
			System.out.println(ex.getMessage());
		}
		return 0;
	}
	public int deleteActor(int id) {
		try {
			String sql = "delete from actor  where actor_id=?";
			st = con.prepareStatement(sql);
			// Añado el parámetro
			st.setInt(1, id);
			// Ejecuto
			return st.executeUpdate();

		} catch (Exception ex) {
			System.out.println(ex.getMessage());
		}
		return 0;
	}
}

package com.trifulcas.DAO;

public class TestActorDao {

	public static void main(String[] args) {
		ActorDAO actorDAO = new ActorDAO();
		System.out.println(actorDAO.getActor(1));
		System.out.println(actorDAO.getActors());
		actorDAO.addActor("aa", "bb");
		Actor actor=new Actor(0,"aa","bb");
		actorDAO.addActor(actor);
		
		Actor penelope=actorDAO.getActor(1);
		penelope.setFirst_name("Paquita");
		penelope.setLast_name("Salas");
		actorDAO.updateActor(penelope);
		System.out.println(actorDAO.getActor(1));
		actorDAO.deleteActor(56982);
	}

}

Publicado por

Avatar del usuario

Juan Pablo Fuentes

Formador de programación y bases de datos