Currently I have a code that executes a query to a database in oracle which returns about 200,000 results and then I run the resultset to build a csv.
The problem I have is that sometimes, some values of a column of the resultset reach null
, but when I execute the query separately those values are not null
. The strangest thing is that in the row where the null values appear, the information of other columns is also modified (that is, if the information had to be output hola mundo
comes out mundohola
).
If I filter the query to bring less results between them what is coming out null
, this time they do not reach null
said values, it has already become a headache this problem.
I enclose the code that is creating the csv:
private ReporteBuilderVO generarCsvBySql(String sql) throws Exception {
StringBuilder buffer = new StringBuilder();
ReporteBuilderVO builderVO = new ReporteBuilderVO();
Integer rowSize = 0;
try (ByteArrayOutputStream write = new ByteArrayOutputStream()) {
try (Connection connection = getJDBCConnection()) {
try (Statement statement = connection.createStatement()) {
ResultSet resultSet = statement.executeQuery(sql);
//System.out.println("size
resultSet.getFetchSize());
resultSet.setFetchSize(300);
ResultSetMetaData dataModel = resultSet.getMetaData();
int columnCount = dataModel.getColumnCount();
for (int i = 1; i <= columnCount; i++) {
buffer.append(dataModel.getColumnName(i));
if (i < columnCount) {
buffer.append(";");
}
}
buffer.append("\n");
//System.out.println("Fila de títulos construida");
write.write(buffer.toString().getBytes());
while (resultSet.next()) {
rowSize++;
for (int i = 1; i <= columnCount; i++) {
write.write(resultSet.getString(i) != null ?
resultSet.getString(i).replaceAll("[\n\r]", " ").getBytes() : "".getBytes());
if (i < columnCount) {
write.write(";".getBytes("UTF-8"));
}
}
write.write("\n".getBytes("UTF-8"));
}
}
}
builderVO.setResultadoOutput(write);
builderVO.setTotalFilas(rowSize);
//System.out.println("rowSize "+rowSize);
}
//System.out.println("Saliendo de generarCsvBySql");
return builderVO;
}
If I print the resultset.getString(i)
, null
appears, but when executing the query, that data is not null
. I very much appreciate any help you can give me in this regard, or if there is any improvement that can be made to this method to prevent this from happening.
PD. Something that seems pertinent to clarify is that the data of the column that is coming out null, in fact it is a mandatory field, therefore, it is never null and this was the reason why we found out about the inconvenience.