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;
}
}