0

I need to get the integer[] value, from an column named "jogos" in PostgreSQL, and store it in a int[] attribute in Java, but the resultSet.getArray show me this error:

incompatible types: Array cannot be converted to int[]

And there's no resultSet.getIntegerArray or something like that.

My function:

public ResultSet LogarUsuario(Usuario usuario){
    String sql = "Select * FROM usuario WHERE email = '" + usuario.email + "' AND senha = '" + usuario.senha + "'";
    try {
        ResultSet rs = db.ExecutaBusca(sql); //search on DB and returns the result
        usuario.setId(rs.getInt("id"));
        usuario.setEmail(rs.getString("email"));
        usuario.setSenha(rs.getString("senha"));
        usuario.setJogos(rs.getArray("jogos")); //Line where is the problem
        return rs;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

My class I will instantiate and store those values from DB:

public class Usuario {
    public int id;
    public String email;
    public String senha;
    public int[] jogos;

}

So, how can I get this integer[] value and store in a int[] attribute?

There's more code I guess is not needed, but I can send a specific part if requested.

It's my first question here, sorry if something it's confusing. Also, sorry if my english looks weird or wrong, I am still learning.

3
  • 2
    Does this answer your question? How to query for an postgres integer array in spring Commented Jul 14, 2021 at 1:00
  • ResultSet.getArray() returns object of type interface java.sql.Array. You can use ResultSet.getArray().getArray() to get the java array Commented Jul 14, 2021 at 1:03
  • This post solution is similar to the answer Udayanga gave me, but that code is using List and I am using array. This ResultSet.getArray().getArray() give me this error: incompatible types: Object cannot be converted to int[] Commented Jul 14, 2021 at 1:57

2 Answers 2

0

The getArray method may be returning Array Objects, so you may need to use Integer wrapper for the variable to set the value into. You are using primitive int array in the class where you store the values. Try this , may work (not tested)

Modify this:

public int[] jogos;

To:

public Integer[] jogos;
public Integer[] getJogos() {
    return jogos;
}
public void setJogos(Integer[] jogos) {
    this.jogos = jogos;
}

Modify this :

usuario.setJogos(rs.getArray("jogos"));` //Line where is the problem

To:

Array jogosArray= rs.getArray("jogos");
Integer[] jogosIntArray = (Integer[]) jogosArray.getArray();
usuario.setJogos(jogosIntArray);
Sign up to request clarification or add additional context in comments.

Comments

0

Try something like this

 Array array = rs.getArray("jogos");
 usuario.setJogos((int[]) array.getArray());

3 Comments

The error is gone but now i got this exception: java.lang.ClassCastException: [Ljava.lang.Integer; cannot be cast to [I
Try by changing your "Usuario" class "jogos" from int[] to Integer[]. Then change above code also.
I did something like that as shown on the answer from Giri and it worked. But thanks for the help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.