Search code examples
javapostgresqlintbyteresultset

resultset contains int8 type column however neither getInt or getByte is able to fettch the value of column


I am connecting to a postgresql DB and am trying to fetch the count of specific employees

this is the query I am using

select count(*) from new_employees where joining_date between '10-01-2019' AND '11-01-2019'

this query is correctly giving count as 20

enter image description here

Now I tried connecting the same DB via my groovy script and

on resultset I fetched the values by this

int countOfEmployees =  rs.getInt("count");

and when I printed the value it gave out correctly as 20

However when I was trying to connect the same DB using Java the value is showing up as 0

I use the same code

int countOfEmployees =  rs.getInt("count");

but if is of no help

I also tried and checked if resultset is non empty and also found out that the count column is of type int8 by using resulsetmetadata

ResultsetMetaData rsmd = rs.getMetaData();
System.out.println("Type: " + rsmd.getColumnTypeName(1)); 

Even using getByte was of no use I tried capturing the value in a variable and then tried converting it to int still the value was zero.

Byte countOfEmp = rs.getByte("count");
int countOfEmpConverted = countOfEmp.intValue();

If anyone knows if I am doing something wrong, please help!


Solution

  • int8 maps to long type in Java. It may be confusing because in programming languages like C# we use int64 and int32 for long and int respectively.

    Source:
    1. this
    2. here