List by foreign key MySQL and Java

0

I'm trying to select the dvds that the member registered but I'm not getting it, I would like to know if someone can help me, the result of my code returns so Result:

1 - null - null - null - null - null - null - 2

And only one result appears, I need it to check the whole database and show the user only the dvds registered by him, to list all the dvds I got but this one is killing me.

Class Socio :

public class Socio {
    protected Long codigo;
    protected String nome;
    protected Integer telefone;
    protected Integer ddd;
    protected String email;
    protected String cpf;

    // getters e setters

    @Override
    public String toString() {
        String saida = cpf + " - " + nome + " - " + telefone + " - " + ddd
                + " - " + email + " - " + codigo;
        return saida;
    }

}

Class Dvd :

package dominio;

import java.util.Scanner;

public class Dvd {

    private Long codigo;
    protected String titulo;
    protected String genero;
    protected String descricaoGenero;
    protected Double duracao;
    protected String sinopse;
    protected String idioma;
    protected String legenda;
    protected Integer anoProducao;
    private Socio socio = new Socio();

    // getters e setters

    @Override
    public String toString() {
        Socio s = new Socio();
        String saida = titulo + " - " + genero + " - " + descricaoGenero
                + " - " + duracao + " - " + sinopse + " - " + idioma + " - "
                + anoProducao + " - " + codigo ;
        return saida;
    }

}

List method in DvdDao :

public Dvd listarDvdsPorCodigoSocio(Dvd f) throws SQLException{
    StringBuilder sql = new StringBuilder();
    sql.append("SELECT codigo, socio_codigo, titulo ");
    sql.append("FROM dvd ");
    sql.append("WHERE socio_codigo = ? ");

    Connection conexao = FabricaDeConexao.conectar();

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

    ResultSet resultado = comando.executeQuery();

    Dvd retorno = null;

    while(resultado.next()){
        retorno = new Dvd();
        retorno.setCodigo(resultado.getLong("codigo"));
        retorno.setTitulo(resultado.getString("titulo"));
        retorno.setTitulo(resultado.getString("socio_codigo"));
    } 
    return retorno;
}

And how I'm using it:

public void buscarDvdsPorSocio () throws SQLException{

    Dvd f2 = new Dvd();
    AdicionarSocioVisao e = new AdicionarSocioVisao();
    e.solicitarCodigoUsuario();
    f2.setSocio(e);

    try {

        DvdDAO fdao = new DvdDAO();
        Dvd f3 = fdao.listarDvdsPorCodigoSocio(f2);

        System.out.println("Resultado 1: "+f3);

    } catch (SQLException ex) {
        System.out.println("OCORREU UM ERRO..." + ex);
        ex.printStackTrace();
    }
}
    
asked by anonymous 31.05.2015 / 00:41

1 answer

-1

Now it looks like this in my DAO :

public List<Dvd> listarDvdsPorCodigoSocio( Dvd f) throws SQLException {
    StringBuilder sql = new StringBuilder();
    sql.append("SELECT codigo, titulo, genero, descricaogenero, duracao, sinopse, idiomas, legenda, anodeproducao, socio_codigo ");
    sql.append("FROM dvd ");
    sql.append("WHERE socio_codigo = ? ");

    Connection conexao = FabricaDeConexao.conectar();

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

    ResultSet resultado = comando.executeQuery();

    List<Dvd> retorno = new ArrayList<>();

    while(resultado.next()){
        final Dvd dvd = new Dvd();
        dvd.setCodigo(resultado.getLong("codigo"));
        dvd.setTitulo(resultado.getString("titulo"));
        dvd.setGenero(resultado.getString("genero"));
        dvd.setDescricaoGenero(resultado.getString("descricaogenero"));
        dvd.setDuracao(resultado.getDouble("duracao"));
        dvd.setSinopse(resultado.getString("sinopse"));
        dvd.setIdioma(resultado.getString("legenda"));
        dvd.setAnoProducao(resultado.getInt("anodeproducao"));
        dvd.setTitulo(resultado.getString("socio_codigo"));
        retorno.add(dvd);
    } 
    return retorno;
}

And so I'm using:

public void listarOsPropriosDvds(){
    AdicionarSocioVisao e = new AdicionarSocioVisao();
    e.solicitarCodigoUsuario();

    Dvd find = new Dvd();
    find.setSocio(e);

    try {
        DvdDAO fdao = new DvdDAO();
        List<Dvd> resultado = fdao.listarDvdsPorCodigoSocio(find);

        for(Dvd d: resultado) {

            System.out.println(String.format("Resultado "+ d));
        }
    } catch (SQLException ex) {
        System.out.println("OCORREU UM ERRO..." + ex);
    }
}
    
01.06.2015 / 17:57
Why do I need to run this code twice to rotate the 3d object? [closed] ___ ___ erkimt SQLSTATE [42703] Undefined column: 7 ERROR: alis.nome_coluna column does not exist LINE 1: [closed] ______ qstntxt ___

I have a page on my system that does a file transfer, it just does an update on the table. I copied the lines of this code and put it to work on another page, changing only the field and table names.

I'm using the Symfony 1.4 framework with MVC for PHP development. In the action of the original function there are two actions one that opens the page and another that makes the update code. In the action of the other page, I copied the action that makes the page open and the code action doing the update, changed the fields and table name.

My role, as I said, is to take the procedure number (original code page) and bring it along to whom the procedure was registered. In the database has a boolean column %code% that receives default %code% and if it is changed it becomes %code% - and if it is %code% I say in my code for it to invert the values.

I did the same thing for the other page. If I do not create the column %code% (new boolean column) my button that does the function of opening the page to transfer, everything works fine, however it does not change anything because it does not have a column in the table created to receive the value% with%. If I create the column %code% my system does not load the data and the error appears saying that the column does not exist.

My action code is this:

%pre%

I am not the creator of the system, I am only giving maintenance (I am a trainee). Can anyone help me?

    
______ azszpr57824 ___

try to put one in? as in the above line between the parameters:  -> set ('d.transferido_den', '?', 'NOT (d.transferido_den)')

It will probably work.

    
______ azszpr58125 ___

I was making a mistake because of the database I was calling. There were 2 with similar names, I was creating in db that was not being called in my file, so the error.

    
___