public boolean Salvar(AlunoTarefa obj) {
    try {
      if (obj.getIdalunotarefa() == 0) {
        PreparedStatement comando =
            bd.getConexao()
                .prepareStatement("insert into alunosportarefa(idaluno, idtarefa) values(?,?)");
        comando.setInt(1, obj.getIdaluno());
        comando.setInt(2, obj.getIdtarefa());
        comando.executeUpdate();
      } else {
        PreparedStatement comando =
            bd.getConexao()
                .prepareStatement(
                    "update alunosportarefa set idaluno=?, idtarefa =? where idalunotarefa = ?");
        comando.setObject(1, obj.getIdaluno());
        comando.setObject(2, obj.getIdtarefa());
        comando.setInt(3, obj.getIdalunotarefa());
        comando.executeUpdate();
      }

      return true;
    } catch (SQLException ex) {
      Logger.getLogger(AlunoTarefaDAO.class.getName()).log(Level.SEVERE, null, ex);
      return false;
    }
  }
 public void ApagarTudo() {
   try {
     PreparedStatement comando = bd.getConexao().prepareStatement("delete from alunosportarefa");
     comando.executeUpdate();
   } catch (SQLException ex) {
     Logger.getLogger(AlunoTarefaDAO.class.getName()).log(Level.SEVERE, null, ex);
   }
 }
 public boolean Apagar(Curso obj) {
   try {
     PreparedStatement comando =
         bd.getConexao().prepareStatement("delete from cursos where idcurso = ?");
     comando.setInt(1, obj.getIdCurso());
     comando.executeUpdate();
     return true;
   } catch (SQLException ex) {
     Logger.getLogger(CursoDAO.class.getName()).log(Level.SEVERE, null, ex);
     return false;
   }
 }
  public boolean Salvar(Curso obj) {
    try {
      if (obj.getIdCurso() == 0) {
        PreparedStatement comando =
            bd.getConexao().prepareStatement("insert into cursos(nome) values(?)");
        comando.setString(1, obj.getNome());
        comando.executeUpdate();
      } else {
        PreparedStatement comando =
            bd.getConexao().prepareStatement("update cursos set nome = ? where idcurso = ?");
        comando.setString(1, obj.getNome());
        comando.setInt(2, obj.getIdCurso());
        comando.executeUpdate();
      }

      return true;
    } catch (SQLException ex) {
      Logger.getLogger(CursoDAO.class.getName()).log(Level.SEVERE, null, ex);
      return false;
    }
  }
 public boolean Apagar(AlunoTarefa obj) {
   try {
     PreparedStatement comando =
         bd.getConexao().prepareStatement("delete from alunosportarefa where idalunotarefa = ?");
     comando.setInt(1, obj.getIdalunotarefa());
     comando.executeUpdate();
     return true;
   } catch (SQLException ex) {
     Logger.getLogger(AlunoTarefaDAO.class.getName()).log(Level.SEVERE, null, ex);
     return false;
   }
 }
  public List<AlunoTarefa> buscar(AlunoTarefa filtro) {
    try {

      String sql = "select * from alunosportarefa ";
      String where = "";

      if (filtro.getIdtarefa() > 0) {
        where = "idtarefa =" + filtro.getIdtarefa() + "%'";
      }

      if (filtro.getIdaluno() > 0) {
        where = "idaluno =" + filtro.getIdaluno() + "%'";
      }

      if (filtro.getIdalunotarefa() > 0) {
        if (where.length() > 0) {
          where = where + " and ";
        }
        where = where + " idalunotarefa = " + filtro.getIdalunotarefa();
      }

      if (where.length() > 0) {
        sql = sql + " where " + where;
      }

      Statement comando = bd.getConexao().createStatement();

      ResultSet resultado = comando.executeQuery(sql);

      List<AlunoTarefa> alunotarefa = new LinkedList<>();
      while (resultado.next()) {

        AlunoTarefa temp = new AlunoTarefa();

        temp.setIdalunotarefa(resultado.getInt("idalunotarefa"));
        temp.setIdaluno(resultado.getInt("idaluno"));
        temp.setIdtarefa(resultado.getInt("idtarefa"));

        alunotarefa.add(temp);
      }
      return alunotarefa;
    } catch (SQLException ex) {
      Logger.getLogger(AlunoTarefaDAO.class.getName()).log(Level.SEVERE, null, ex);
      return null;
    }
  }
  public List<Curso> listarTodos() {
    try {
      PreparedStatement comando = bd.getConexao().prepareStatement("select * from cursos");
      ResultSet resultado = comando.executeQuery();

      List<Curso> curso = new LinkedList<>();
      while (resultado.next()) {
        Curso tmp = new Curso(0, "");

        tmp.setIdCurso(resultado.getInt("idcurso"));
        tmp.setNome(resultado.getString("nome"));

        curso.add(tmp);
      }
      return curso;
    } catch (SQLException ex) {
      Logger.getLogger(CursoDAO.class.getName()).log(Level.SEVERE, null, ex);
      return null;
    }
  }
  public List<Curso> buscar(Curso filtro) {
    try {

      String sql = "select * from cursos ";
      String where = "";

      if (filtro.getNome().length() > 0) {
        where = "nome like '%" + filtro.getNome() + "%'";
      }

      if (filtro.getIdCurso() > 0) {
        if (where.length() > 0) {
          where = where + " and ";
        }
        where = where + " idcurso = " + filtro.getIdCurso();
      }

      if (where.length() > 0) {
        sql = sql + " where " + where;
      }

      Statement comando = bd.getConexao().createStatement();

      ResultSet resultado = comando.executeQuery(sql);

      List<Curso> curso = new LinkedList<>();
      while (resultado.next()) {

        Curso temp = new Curso(0, "");

        temp.setIdCurso(resultado.getInt("idcurso"));
        temp.setNome(resultado.getString("nome"));

        curso.add(temp);
      }
      return curso;
    } catch (SQLException ex) {
      Logger.getLogger(CursoDAO.class.getName()).log(Level.SEVERE, null, ex);
      return null;
    }
  }
  public Curso Abrir(int codigo) {
    try {
      Curso curso = new Curso(0, "");

      PreparedStatement comando =
          bd.getConexao().prepareStatement("select * from cursos where idcurso = ?");
      comando.setInt(1, codigo);
      ResultSet resultado = comando.executeQuery();

      resultado.first();

      curso.setIdCurso(resultado.getInt("idcurso"));
      curso.setNome(resultado.getString("nome"));

      return curso;

    } catch (SQLException ex) {
      Logger.getLogger(CursoDAO.class.getName()).log(Level.SEVERE, null, ex);
      throw new RuntimeException();
    }
  }
  public List<AlunoTarefa> listarTodos() {
    try {
      PreparedStatement comando = bd.getConexao().prepareStatement("select * from alunosportarefa");
      ResultSet resultado = comando.executeQuery();

      List<AlunoTarefa> alunotarefa = new LinkedList<>();
      while (resultado.next()) {
        AlunoTarefa tmp = new AlunoTarefa();

        tmp.setIdalunotarefa(resultado.getInt("idalunotarefa"));
        tmp.setIdaluno(resultado.getInt("idaluno"));
        tmp.setIdtarefa(resultado.getInt("idtarefa"));

        alunotarefa.add(tmp);
      }
      return alunotarefa;
    } catch (SQLException ex) {
      Logger.getLogger(AlunoTarefaDAO.class.getName()).log(Level.SEVERE, null, ex);
      return null;
    }
  }
  public AlunoTarefa Abrir(int codigo) {
    try {
      AlunoTarefa alunotarefa = new AlunoTarefa();

      PreparedStatement comando =
          bd.getConexao().prepareStatement("select * from alunosportarefa where idalunotarefa = ?");
      comando.setInt(1, codigo);
      ResultSet resultado = comando.executeQuery();

      resultado.first();

      alunotarefa.setIdalunotarefa(resultado.getInt("idalunotarefa"));
      alunotarefa.setIdaluno(resultado.getInt("idaluno"));
      alunotarefa.setIdtarefa(resultado.getInt("idtarefa"));

      return alunotarefa;

    } catch (SQLException ex) {
      Logger.getLogger(AlunoTarefaDAO.class.getName()).log(Level.SEVERE, null, ex);
      return null;
    }
  }