Search code examples
oracle-databasejdbccharprepared-statementresultset

No Data Returned in Query with Equal Predicate on a CHAR Type Column


This is my code and i have a column which is named as Product Id and data type as CHAR (6 byte) in oracle. I have used set string method for this column to retrieve the details from Database. Actually i got result set as false (It has empty) and there is no records fetched. But when am using 'PRODUCT_ID="+value+"' it works. How to fix this..

sSQL="SELECT * FROM CART_VIEW WHERE PRODUCT_ID=? AND  PRODUCT_NAME=? ";
ps=conn.prepareStatement(sSQL);
ps.setString(++i, sProductId);
ps.setString(++i, sProductName);
rs=ps.executeQuery();

Solution

  • The column format CHAR(6) means (contrary to the VARCHAR2 format) that the strings shorter than 6 bytes are blank padded. I.e. you must provide the string with added trailing blanks to the exact length of the column.

    Example if you insert 'X' in the column XX CHAR(6)

     -- returns nothing
     select * from test where xx = 'X'; 
    
     -- returns a row
     select * from test where xx = 'X     '; 
    

    I.e. you must pass in setString the full length string.