Scarpah600 / Filmes

0 stars 0 forks source link

FilmesDao.java #2

Open Scarpah600 opened 6 years ago

Scarpah600 commented 6 years ago
package br.com.Jc1.DAO;

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

import br.com.Jc1.domain.Filmes;
import br.com.Jc1.factory.ConexaoFactory;

public class FilmesDao {

    // UPDATE `dtb_filmes`.`db_filmes` SET `NOME_FILMES` = '2', `GENERO_FILMES`
    // = '2' WHERE (`ID` = '1');

    public void salvar(Filmes f) throws SQLException {
        StringBuilder sql = new StringBuilder();
        sql.append("INSERT INTO db_filmes ");
        sql.append("(NOME_FILMES, GENERO_FILMES, DATA_FILMES, DESCRICAO_FILMES) ");
        sql.append("VALUES (?,?,?,?)");

        Connection conexao = ConexaoFactory.conectar();

        PreparedStatement comando = conexao.prepareStatement(sql.toString());
        comando.setString(1, f.getNOME_FILME());
        comando.setString(2, f.getGENERO_FILME());
        comando.setString(3, f.getDATA_FILME());
        comando.setString(4, f.getDESCRICAO_FILME());

        comando.executeUpdate();

    }

    public void alterar(Filmes f) throws SQLException {
        StringBuilder sql = new StringBuilder();
        sql.append("UPDATE db_filmes ");
        sql.append("SET NOME_FILMES = ? , GENERO_FILMES = ? , DATA_FILMES = ? , DESCRICAO_FILMES = ? ");
        sql.append("WHERE ID = ? ");

        Connection conexao = ConexaoFactory.conectar();

        PreparedStatement comando = conexao.prepareStatement(sql.toString());
        comando.setLong(5, f.getID());
        comando.setString(1, f.getNOME_FILME());
        comando.setString(2, f.getGENERO_FILME());
        comando.setString(3, f.getDATA_FILME());
        comando.setString(4, f.getDESCRICAO_FILME());
        comando.executeUpdate();
    }

    // DELETE FROM `dtb_filmes`.`db_filmes` WHERE (`ID` = '5');

    public void deletar(Filmes f) throws SQLException {
        StringBuilder sql = new StringBuilder();
        sql.append("DELETE FROM  db_filmes ");
        sql.append("WHERE ID = ? ");

        Connection conexao = ConexaoFactory.conectar();

        PreparedStatement comando = conexao.prepareStatement(sql.toString());
        comando.setLong(1, f.getID());

        comando.executeUpdate();
    }

    public Filmes BuscarFilmes(Filmes f) throws SQLException {
        StringBuilder sql = new StringBuilder();
        sql.append("SELECT ID, NOME_FILMES ");
        sql.append("FROM db_filmes ");
        sql.append("WHERE ID = ? ");

        Connection conexao = ConexaoFactory.conectar();

        PreparedStatement comando = conexao.prepareStatement(sql.toString());
        comando.setLong(1, f.getID());

        ResultSet resultado = comando.executeQuery();
        Filmes retorno = null;

        if (resultado.next()) {
            retorno = new Filmes();
            retorno.setID(resultado.getLong("ID"));
            retorno.setNOME_FILME(resultado.getString("NOME_FILMES"));
        }

        return retorno;

    }

    public ArrayList<Filmes>buscarPorNome(Filmes f) throws SQLException
    {
        StringBuilder sql = new StringBuilder();
        sql.append("SELECT ID, NOME_FILMES ");
        sql.append("FROM db_filmes ");
        sql.append("WHERE NOME_FILMES LIKE ? ");
        sql.append("ORDER BY NOME_FILMES ASC ");

        Connection conexao = ConexaoFactory.conectar();

        PreparedStatement comando = conexao.prepareStatement(sql.toString());

        comando.setString(1, "%" + f.getNOME_FILME() + "%");
        ResultSet resultado = comando.executeQuery();
        ArrayList<Filmes> lista = new ArrayList<Filmes>();

        while (resultado.next()) {
            Filmes filme = new Filmes();
            filme.setID(resultado.getLong("ID"));
            filme.setNOME_FILME(resultado.getString("NOME_FILMES"));

            lista.add(filme);
        }
       return lista;
    }

    public ArrayList<Filmes> listar() throws SQLException {
        StringBuilder sql = new StringBuilder();
        sql.append("SELECT ID, NOME_FILMES ");
        sql.append("FROM db_filmes ");
        sql.append("ORDER BY NOME_FILMES ASC ");

        Connection conexao = ConexaoFactory.conectar();

        PreparedStatement comando = conexao.prepareStatement(sql.toString());

        ResultSet resultado = comando.executeQuery();

        ArrayList<Filmes> lista = new ArrayList<Filmes>();

        while (resultado.next()) {
            Filmes f = new Filmes();
            f.setID(resultado.getLong("ID"));
            f.setNOME_FILME(resultado.getString("NOME_FILMES"));

            lista.add(f);
        }
        return lista;
    }
}