Oracle JDBC and Oracle CHAR data type -


I have a difficult issue with the Oracle jdbc driver's code CHAR handling data types. Take simple table:

  make table x (c char (4)); Insert in X (C) values ​​('A'); - Inserted 'A'   

So when I put something in CHAR (4) , the string always gets filled with white space. Is when I execute a query like this:

 select  from x where c = 'a'; - 1 selection of records * from x where c = 'a'; - 1 selection of records * from x where c = 'a'; - 1 selects the record   

Here, the constant 'a' is also full of whitespace, which is why the record always comes back. This is true when these questions are executed simultaneously with a JDBC created location Now the hard thing is that when I want to use a bind variable: < Pre class = "lang-java prettyprint-override"> created statement stmt = conn.prepareStatement ("x * from * select c =?"); Stmt.setString (1, "A"); // It will not return any record stmt.setString (1, "A"); // This will return a record stmt.executeQuery ();

This is a workaround:

  created condition stmt = conn.prepareStatement ("From X Choose * where trim (c) = trim (?) "); Stmt.setString (1, "A"); // This will return a record stmt.setString (1, "A"); // This will return a record stmt.executeQuery ();  Edit : These barriers are now:  
  • The above solution is not required because it modifies the content of both C and ? , and it's hard to use indexed on c .
  • Columns CHAR <

    edit : The reason for these obstacles is that it is not possible

    Edit Because I ask about the developer's approach to this question, a database intangible library So my needs are to provide a very common solution that does not break anything in Juke's client code, this is the reason why I am not really a big fan of the solution. And that's why I do not have access to that CHAR column announcement. But still, I want to be able to handle this issue.

    What would you do instead?

    If you want

      stmt.setString (1, "a"); // This will not return a record   

    , please try

      conn.prepareStatement ("Select * from x * c = cast (? Four (four)) ")    

Comments