Error between java - jdbc oralce, resultset null

0

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.

    
asked by Fernando Guevara 09.04.2018 в 17:28
source

1 answer

1

As indicated by Alfabravo, there was an erratic behavior in the way that java through the JDBC executed the query, this query was of the form:

  Select * from (
   select
    (select...) as col1,
    (select ...) as col2 
   from tabla where condiciones
  )

The query was modified to eliminate the internal selects and the problem disappeared.

    
answered by 23.04.2018 / 21:40
source