Oracle pads values in char columns so if I insert "a" in a CHAR(2) column then I cannot get that record by comparing that column to "a", I should get it by comparing it to "a ". Right?
To solve this problem the Oracle jdbc driver has the property fixedString but I cannot make it work. (look for fixedString here)
I'm using ojdbc14.jar driver for Oracle 10gR2 and accessing an Oracle 10gR2 database.
This is my code:
try {
Properties props = new Properties();
props.put("user", "****");
props.put("password", "****");
props.put("fixedString", true);
Class.forName("oracle.jdbc.driver.OracleDriver");
String jdbcUrl = "jdbc:oracle:thin:@<host>:<port>:<sid>";
Connection connection = DriverManager.getConnection(jdbcUrl, props);
PreparedStatement ps = connection.prepareStatement(
"SELECT * FROM MY_TABLE WHERE MY_TABLE_ID = ?");
ps.setObject(1, "abc"); // (*)
// MY_TABLE_ID is CHAR(15)
ResultSet rs = ps.executeQuery();
while (rs.next())
{
System.out.print("data: ");
System.out.println(rs.getString("MY_TABLE_ID"));
}
rs.close();
ps.close();
connection.close();
} catch (SQLException ex) {
ex.printStackTrace();
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
The above code executes fine (no exceptions thrown) but the ResultSet is empty after executeQuery().
If I change the line (*) for
ps.setObject(1, "abc ");
Then I get the column I wanted. So it seems the driver is ignoring the fixedString option.
I've also tried changing the line (*) for
ps.setObject(1, "abc", java.sql.Types.CHAR);
But the ResultSet I get is empty again. What am I missing?
Thanks in advance