Problem with ResultSet in Java

0

I am working with MySQL in Java and when I do querys I formulate them in a general way, I prepare them and execute them.

When I get the ResultSet I give it to a method that processes it (Command Pattern), but when the response is obtained, an exception is thrown.

Finally, it should be noted that with the query I am working with, there are no null results.

This function prepares the query:

public void selectAsResultSetWhitList(IQuery query, ArrayList<String> lista,IResult result) throws SQLException {
    String qr = query.getQuery();
    PreparedStatement ps = conexion.prepareStatement(qr);
    ResultSet r;
    int i =1 ;
    for (String st : lista) {
        ps.setString(i++, st);
    }
    r= ps.executeQuery();
    result.setResult(r);
}

This class is the one that processes the ResultSet:

import java.sql.ResultSet;
import java.sql.SQLException;

public class ResultadosCategoria implements IResult {
    private int categoria;

    @Override
    public void setResult(ResultSet r) throws SQLException {
        categoria = 5 ;
        while (r.next()){// Si es que el resultado es vacio hay que asegurarse.
        categoria =  r.getInt("categoria");}
    }

    public int result() {
        return categoria;
    }
}
    
asked by Carlos Contreras 20.01.2017 в 19:51
source

1 answer

0

The function is well done. The problem was in the way to give the parameters to the Query. It was due to a bad obtaining of the parameters from a JSON.

    
answered by 23.01.2017 в 14:47