Search code examples
javapostgresqljdbcresultset

org.postgresql.util.PSQLException - 2019


I am trying to automate my API & DB testing(postgresql) via Cucumber(Java). How ever i have written my code and trying to execute below queries:

resultSet=statement.executeQuery("select * from rules_data.postcode_leadtime where postcode = "+postcodedistrict.toUpperCase()+"  and order_type = "+ordertype.toUpperCase()+"");

Now, if i run the direct query i.e. -

select * 
from rules_data.postcode_leadtime 
where postcode = 'MK9'  
  and order_type = 'ADSI' 

it runs perfectly fine but when I run the above query with parameters it gives the below error:

org.postgresql.util.PSQLException: ERROR: column "mk9" does not exist

I googled this up, found out that it has to be in Uppercase when executing the command, so then i made sure that its being converted into uppercase as you can see in the parameter in the above query, BUT STILL I AM GETTING THE SAME ISSUE. Happening for my other commands too.

Note: I have tried printing the values of parameters, they are coming in Upper cases.

Another issue i am getting is with the below query:

resultSet=statement.executeQuery("select day from rules_data.hub_workdays where hub_id = " + hub + " and available = 'N'");

Over here hub is an integer value, it is coming fine on printing but when executing via Java gives an error below:

org.postgresql.util.PSQLException: ERROR: operator does not exist: character varying = integer

But when i run with value it works fine.

select day 
from rules_data.hub_workdays 
where hub_id = '593' 
and available = 'N'

Already mentioned in my first explanation.


Solution

  • You should really use PreparedStatement as written in the comment, but your error can go away with the following fix:

    resultSet=statement.executeQuery("select * from rules_data.postcode_leadtime 
                                     where postcode = '"+postcodedistrict.toUpperCase()+"'   
                                     and order_type = '"+ordertype.toUpperCase()+"'");
    

    basically you need to add single quotes so that the parameters become strings