0
I am developing a system with Java 8 and Spring Boot with JDBC accessing an Oracle base. When I do a query, using Namedparameterjdbctemplate, it does not return any record, but if I copy this same query and do a direct search in the Oracle client, the records return normally. Follow the search method:
public Optional<Objeto> busca(String campo4, String campo3, String campo2) {
StringBuilder sql = new StringBuilder();
sql.append("SELECT CAMPO_1, ");
sql.append(" CAMPO_2, ");
sql.append(" CAMPO_3, ");
sql.append(" CAMPO_4, ");
sql.append(" CAMPO_5, ");
sql.append(" CAMPO_6 ");
sql.append("FROM TABELA_DO_BANCO ");
sql.append(" WHERE CAMPO_4 = :pCampo4 ");
sql.append(" AND CAMPO_2 = :pCampo2 ");
sql.append(" AND CAMPO_3 = :pCampo3 ");
return this.namedParameterJdbcTemplate.query(sql.toString(), new MapSqlParameterSource()
.addValue("pCampo4", campo4, Types.INTEGER)
.addValue("pCampo2", campo2)
.addValue("pCampo3", campo3)
, new ObjetoRowMapper())
.stream().findFirst();
}
Follows the description of the database:
I believe the problem lies in WHERE with the countryside CAMPO_2, because when I remove it from the query, the system returns the expected records. Does anyone have any idea what it is? Has anyone gone through it?
Hugs.
Where is the
CAMPO_8
?– Dherik
Actually it is CAMPO_2, already corrected the post. Thank you.
– Wellington Gonçalves
Inspects the value that is in the field variable 2. Also, put the query you ran on the outside, because if the problem is in the Java query, it is probably relative to the value of the fields you injected in the query.
– Giuliana Bezerra
@Giulianabezerra inspected and the value is normal.
– Wellington Gonçalves
Include the query you are performing outside the application, as requested by @Giulianabezerra.
– Weslley Tavares
Follow sql taken directly from code: SELECT CAMPO_1, CAMPO_2, CAMPO_3, CAMPO_4, CAMPO_5, CAMPO_6 FROM SDE_PARAM_VALID_CRM WHERE CAMPO_4 = 112 AND CAMPO_2 = 337941320001 AND CAMPO_3 = 'AC';
– Wellington Gonçalves
Are you sure this query is running? Note that the column
CAMPO_2
is the typeVARCHAR2
and you’re comparing it to a numerical value.– Weslley Tavares
So I put the Types.VARCHAR in the addValue of CAMPO_2, but did not resolve.
– Wellington Gonçalves
I believe that
campo2
,campo3
andcampo4
should all be treated asTypes.VARCHAR2
orTypes.CHAR
as appropriate.– Piovezan
@Piovezan I couldn’t find the
Types.VARCHAR2
and treated asTypes.CHAR
but it didn’t work.– Wellington Gonçalves
@Wellingtongonçalves They were just suggestions, I don’t know the framework.
– Piovezan
@Piovezan Aah yes, thank you very much kkkk, but I tried to put all the
Types
but none worked :(– Wellington Gonçalves