Oracle JDBC and Oracle CHAR data type Oracle JDBC and Oracle CHAR data type oracle oracle

Oracle JDBC and Oracle CHAR data type


If you want

stmt.setString(1, "a");    // This won't return any records

to return a record, try

conn.prepareStatement("select * from x where c = cast(? as char(4))")


I don't see any reason to use CHAR datatype even if it is char(1) in Oracle. Can you change the datatype instead?


Gary's solution works well. Here's an alternative.

If you are using an Oracle JDBC driver, the call to prepareStatement() will actually return an OraclePreparedStatement, which has a setFixedCHAR() method that automatically pads your inputs with whitespace.

String sql = "select * from x where c = ?";OraclePreparedStatement stmt = (OraclePreparedStatement) conn.prepareStatement(sql);stmt.setFixedCHAR(1, "a");...

Obviously, the cast is only safe if you are using the Oracle driver.

The only reason I would suggest that you use this over Gary's answer is that you can change your column sizes without having to modify your JDBC code. The driver pads the correct number of spaces without the developer needing to know/manage the column size.