E
I see that in your program you use the following "standards" : https://desciclopedia.org/wiki/Gambi_Design_Patterns#BCDR . https://desciclopedia.org/wiki/Gambi_Design_Patterns#Thunder_Mega_Zord . https://desciclopedia.org/wiki/Gambi_Design_Patterns#Um_array_vale_mais_que_mil_vari%C3%A1veis .A little https://desciclopedia.org/wiki/Gambi_Design_Patterns#Programa%C3%A7%C3%A3o_Orientada_a_Strings .I suggest abandoning these "standards" as soon as possible and abolishing things like String[], Object[], List<String[]> and List<Object[]>. That comes from the fact that what you are asking is a https://pt.meta.stackoverflow.com/q/499/132 .That is, the statement of the method should be something like this:public List<Peca> searchPecas(
int codigoProduto,
int codigoFornecedor,
BigInteger bkSubDivisionUidD)
{
// ...
}
Note that the result is List<Peça> and not List<Object[]>. After all, Java is an object-oriented language, and therefore data relating to a piece should be in a class Peça and not within one Object[]. Similarly, a method that provides a list of parts, must return List<Peça> and not one List<Object[]>.Working with a Object[] It is difficult because anything can be anywhere in any way and good luck to the poor programmer who has to deal with it, something quite different from the case of using a clearly defined class. Therefore it is better to use a class Peça instead of one Object[].As for the parameters, the golden rule is to separate things clearly. The simplest way to do this would be to leave the parameters being only simple types such as int, String, BigInteger, etc. Or else, create a class to encapsulate the meaning of the parameters, such as something like this:public final class BuscaPecasParametros {
private final int codigoProduto;
private final int codigoFornecedor;
public BuscaPecasParametros(int codigoProduto, int codigoFornecedor) {
this.codigoProduto = codigoProduto;
this.codigoFornecedor = codigoFornecedor;
}
public int getCodigoProduto() {
return codigoProduto;
}
public int getCodigoFornecedor() {
return codigoFornecedor;
}
}
And then, in the method you would put this:public List<Peca> searchPecas(BuscaPecasParametros params) {
// ...
}
Depending on the case (it can't be sure only with the incomplete code of the question), it could be so also:public List<Peca> searchPecas(
List<BuscaPecasParametros> params,
BigInteger bkSubDivisionUidD)
{
// ...
}
SQL would be built just like this:private static final String SQL_PECAS = ""
+ "\n BEGIN "
+ "\n SET NOCOUNT ON "
+ "\n DECLARE @CodigoProduto VARCHAR(10), "
+ "\n @CodigoFornecedor VARCHAR(10) "
+ "\n SET @subDivision = ? "
+ "\n SET @perfitSerial = ? ";
Yes, use the old operator + with Stringhere. Nothing to be creating StringBuilder, StringBuffer, append or any of these paraphernalias because the compiler is smart enough to make concatenation of constant strings in compilation time. If you look at the resulting bytecode, you will see the string already mounted by the compiler inside and the weight in the performance will be so close to zero that it doesn't even have to be measured easily, being the string loaded once in a lifetime when the class is loaded in memory by JVM. On the other hand, using the approach of appends as you are using, the strings will eventually be built and rebuilt from several pieces whenever the method is executed, which means that there will be impact on the performance and garbage collector.To see how to make the inside of the method, take a look https://pt.stackoverflow.com/q/172909/132 . You can’t mount exactly how it would be in this answer because there are many parts of the method missing in your question. Then I'll copy the example I did in https://pt.stackoverflow.com/a/172910/132 to serve you inspiration:private static final String SQL_ALUNOS_POR_TURMA =
"SELECT id, nome, telefone FROM alunos WHERE id_turma = ?";
public static void localizarAlunos(String turma) throws ConexaoFalhouException {
try (
Connection c = Conexao.obter();
PreparedStatement ps = c.prepareStatement(SQL_ALUNOS_POR_TURMA);
) {
ps.setString(1, turma);
try (ResultSet rs = ps.executeQuery()) {
List<Aluno> alunos = new ArrayList<>();
while (rs.next()) {
Aluno a = new Aluno();
a.setInt(rs.getInt(1));
a.setNome(rs.getString(2));
a.setTelefone(rs.getString(3));
alunos.add(a);
}
return alunos;
}
} catch (SQLException e) {
throw new ConexaoFalhouException(e);
}
}
And another detail:StringUtils.trim(String.valueOf(peças.getCodigoProduto()))
Huh? If the result peças.getCodigoProduto() need to be encapsulated in one String.valueOf(...), so that's probably not a string. If this is not a string, then the StringUtils.trim(...) It's unnecessary. And even if StringUtils.trim(...) is being necessary, the right would be peças.getCodigoProduto() return something that will never create spaces in the beginning or in the end.