vendredi 29 juin 2018

Can not set int field to java.lang.Integer

I've seached for a solution but what I found was using Hybernate, but i'm trying to do it myself, because i can't use hybernate on class yet.

I try to set the int value I get from ResultSet by using

Field.set(Object value, Object obj)

but it doesn't seem to be working, here's my code:

public class GenericDao<T> {

public static void main(String[] args) {

    //Create instance of GenericDao
    GenericDao<Produtos> dao = new GenericDao<>();

    //select Products (Produtos) from Database
    ArrayList<Produtos> produtos = dao.select(Produtos.class, "");

    //Print all product names
    for (Produtos produto : produtos) {

        System.out.println(produto.getNomeProduto());

    }

}

//Insert creator method
public int insert(Object obj) {
    //Insert is working properly
}


//Select creation method
public ArrayList<T> select(Class<T> c, String whereStatement) {

    //Select statement creation, I know that the String whereStatement
    //is terrible here, i just need to get it working and i'll change it later
    String sql = "SELECT * FROM " + c.getSimpleName() + whereStatement;

    //getting connection
    Connection conexao = new ConnectionFactory("FastFood").obterConexao();

    //Creating ArrayList to return
    ArrayList<T> list = new ArrayList<T>();

    try {

        Statement pstmt = conexao.createStatement();

        ResultSet rs = pstmt.executeQuery(sql);

        while (rs.next()) {

            for (Field f : c.getDeclaredFields()) {

                T obje = c.newInstance();

                boolean wasAccesible = f.isAccessible();
                if (!wasAccesible) {
                    f.setAccessible(true);
                }

                String type = f.getType().getSimpleName();

                if (type.equalsIgnoreCase("String")) {
                    f.set(rs.getString(f.getName()), obje);
                } else if (type.equalsIgnoreCase("int")) {

                    int value = rs.getInt(f.getName());  

                    f.set(value, obje);  //THE ERROR IS HAPPENING HERE, you can ignore the rest if you want to

                } else if (type.equalsIgnoreCase("localdatetime")) {
                    f.set(LocalDateTimeConverter.DateToLocalDateTime(rs.getDate(f.getName())), obje);
                } else if (type.equalsIgnoreCase("double")) {
                    f.set(rs.getDouble(f.getName()), obje);
                } else if (type.equalsIgnoreCase("boolean")) {
                    f.set(rs.getBoolean(f.getName()), obje);
                }

                if (!wasAccesible) {
                    f.setAccessible(false);
                }

                list.add(obje);
            }
        }
    } catch (Exception e) {
        System.err.println("Erro : " + e.getMessage());
    }
    return list;
}

The error I'm getting is: Can not set int field br.com.gtff.bean.Produtos.idProduto to java.lang.Integer

Here is what being used from the Produtos Class

public class Produtos {

private int idProduto;
private String nomeProduto;
private Double valorProduto;
private int gramasProduto;
private Boolean ativo;

}





Aucun commentaire:

Enregistrer un commentaire