0
votes

I tried to get ResultSet from Vertica database with Java. But it returns null.

SQL query:

SELECT to_char(SUM(my_column)) FROM my_table
UNION ALL SELECT to_char(123) FROM dual

SQL result in DB client:

201198184560.13000
123

sql_result

Java code:

PreparedStatement ps;
ps = connection.prepareStatement(sqlQuery);
ResultSet rs = ps.executeQuery();

while (rs.next()) {
    System.out.println("Inside loop...");
    String str = rs.getString(1);
    Float f = rs.getFloat(1);
    Double d = rs.getDouble(1);
    Object obj = rs.getObject(1);
    System.out.println("String: " + str + ", float: " + f + ", double: " + d + ", obj: " + obj);
}

Result in java console:

Inside loop...
String: null, float: 0.0, double: 0.0, obj: null
Inside loop...
String: 123, float: 123.0, double: 123.0, obj: 123

What am I doing wrong? Why Result Set returns null? How can I print all my Result Sets in java console?

Update1: Debug before while rs.next():

rs = {S4ForwardResultSet@1015} 
 m_logger = {DSILogger@1028} 
 m_resultMetaData = {S4ResultSetMetaData@1045} 
 m_currentRow = 0
 m_currentStream = null
 m_isOpen = true
 m_resultSet = {VResultSet@1046} 
 m_resultSetColumns = {ArrayList@1038}  size = 1
 m_warningListener = {SWarningListener@1047} 
 m_wasLastValueNull = false
 m_cachedDataWrappers = {ArrayList@1048}  size = 1
 m_cursorPosition = {CursorPosition@1049} "BEFORE_FIRST"
 m_columnNameIndexes = {ArrayList@1050}  size = 1
 m_numColumns = 1
 m_parentStatement = {VerticaJdbc4PreparedStatementImpl@996} 
 m_jdbcVersion = {JDBCVersion@1030} "JDBC4"
 m_streamBufferSize = 32000

Update2: Debug while rs.next() first time:

rs = {S4ForwardResultSet@1013} 
 m_logger = {DSILogger@1014} 
 m_resultMetaData = {S4ResultSetMetaData@1015} 
 m_currentRow = 1
 m_currentStream = null
 m_isOpen = true
 m_resultSet = {VResultSet@1016} 
 m_resultSetColumns = {ArrayList@1017}  size = 1
 m_warningListener = {SWarningListener@1018} 
 m_wasLastValueNull = true
 m_cachedDataWrappers = {ArrayList@1019}  size = 1
 m_cursorPosition = {CursorPosition@1078} "AT_FIRST"
 m_columnNameIndexes = {ArrayList@1021}  size = 1
 m_numColumns = 1
 m_parentStatement = {VerticaJdbc4PreparedStatementImpl@996} 
 m_jdbcVersion = {JDBCVersion@1022} "JDBC4"
 m_streamBufferSize = 32000

Debug while rs.next() second time:

m_logger = {DSILogger@1014} 
m_resultMetaData = {S4ResultSetMetaData@1015} 
m_currentRow = 2
m_currentStream = null
m_isOpen = true
m_resultSet = {VResultSet@1016} 
m_resultSetColumns = {ArrayList@1017}  size = 1
m_warningListener = {SWarningListener@1018} 
m_wasLastValueNull = false
m_cachedDataWrappers = {ArrayList@1019}  size = 1
m_cursorPosition = {CursorPosition@1087} "AT_LAST"
m_columnNameIndexes = {ArrayList@1021}  size = 1
m_numColumns = 1
m_parentStatement = {VerticaJdbc4PreparedStatementImpl@996} 
m_jdbcVersion = {JDBCVersion@1022} "JDBC4"
m_streamBufferSize = 32000

Update3: SQL looks like this:

with prov as
 (select ...
    from ...
    join ...
      on ...               
   where ...)
,
debet as
 (select prov....
    from prov
   where ...
   group by prov....
  ),
kredit as
 (select prov....
    from prov
   where ...
   group by prov....
  )

select 
    TO_CHAR(SUM(debet.sum_debet_rur)) SSS
from
    kredit full join debet 
    on ...
where (debet...) and (debet....)    

UNION ALL SELECT TO_CHAR(123) SSS from dual
1
Could you debug into your code and print out the full state of rs for the first row?Dominik Sandjaja
what database engine do you use?Jan
Added Update1: Debug for the first row rsMaxim
That's not the first row. That would be before the first row. Can you post the debug result of rs afte the first call of rs.next()?Moh-Aw
Added Update2 rs.next() first and second timeMaxim

1 Answers

0
votes

The problem was in my SQL-query.

My JavaApp take SQL-query from file, that contains --comments. After my JavaApp convert it to String and send to DB.

In Vertica you can check your queries using query:

select * from query_requests
order by start_timestamp desc

Thanks!