• <small id='esWn6'></small><noframes id='esWn6'>

  • <i id='esWn6'><tr id='esWn6'><dt id='esWn6'><q id='esWn6'><span id='esWn6'><b id='esWn6'><form id='esWn6'><ins id='esWn6'></ins><ul id='esWn6'></ul><sub id='esWn6'></sub></form><legend id='esWn6'></legend><bdo id='esWn6'><pre id='esWn6'><center id='esWn6'></center></pre></bdo></b><th id='esWn6'></th></span></q></dt></tr></i><div id='esWn6'><tfoot id='esWn6'></tfoot><dl id='esWn6'><fieldset id='esWn6'></fieldset></dl></div>

        <tfoot id='esWn6'></tfoot>
        <legend id='esWn6'><style id='esWn6'><dir id='esWn6'><q id='esWn6'></q></dir></style></legend>
          <bdo id='esWn6'></bdo><ul id='esWn6'></ul>

        Oracle JDBC 和 Oracle CHAR 数据类型

        时间:2023-09-20
        • <i id='GHEtB'><tr id='GHEtB'><dt id='GHEtB'><q id='GHEtB'><span id='GHEtB'><b id='GHEtB'><form id='GHEtB'><ins id='GHEtB'></ins><ul id='GHEtB'></ul><sub id='GHEtB'></sub></form><legend id='GHEtB'></legend><bdo id='GHEtB'><pre id='GHEtB'><center id='GHEtB'></center></pre></bdo></b><th id='GHEtB'></th></span></q></dt></tr></i><div id='GHEtB'><tfoot id='GHEtB'></tfoot><dl id='GHEtB'><fieldset id='GHEtB'></fieldset></dl></div>

            <legend id='GHEtB'><style id='GHEtB'><dir id='GHEtB'><q id='GHEtB'></q></dir></style></legend>
              <tbody id='GHEtB'></tbody>

                <bdo id='GHEtB'></bdo><ul id='GHEtB'></ul>

                <small id='GHEtB'></small><noframes id='GHEtB'>

              • <tfoot id='GHEtB'></tfoot>

                  本文介绍了Oracle JDBC 和 Oracle CHAR 数据类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我在 Oracle JDBC 驱动程序处理 CHAR 数据类型时遇到了一个棘手的问题.让我们以这个简单的表格为例:

                  I have a tricky issue with the Oracle JDBC driver's handling of CHAR data types. Let's take this simple table:

                  create table x (c char(4));
                  insert into x (c) values ('a');  -- inserts 'a   '
                  

                  所以当我在 CHAR(4) 中插入一些东西时,字符串总是用空格填充.当我执行这样的查询时也会这样做:

                  So when I insert something into CHAR(4), the string is always filled with whitespace. This is also done when I execute queries like this:

                  select * from x where c = 'a';    -- selects 1 record
                  select * from x where c = 'a ';   -- selects 1 record
                  select * from x where c = 'a   '; -- selects 1 record
                  

                  这里,常量 'a' 也用空格填充.这就是为什么总是返回记录的原因.当使用 JDBC PreparedStatement 执行这些查询时也是如此.现在棘手的是当我想使用绑定变量时:

                  Here, the constant 'a' is filled with whitespace as well. That's why the record is always returned. This holds true when these queries are executed using a JDBC PreparedStatement as well. Now the tricky thing is when I want to use a bind variable:

                  PreparedStatement stmt = 
                    conn.prepareStatement("select * from x where c = ?");
                  stmt.setString(1, "a");    // This won't return any records
                  stmt.setString(1, "a   "); // This will return a record
                  stmt.executeQuery();
                  

                  这是一种解决方法:

                  PreparedStatement stmt = 
                    conn.prepareStatement("select * from x where trim(c) = trim(?)");
                  stmt.setString(1, "a");    // This will return a record
                  stmt.setString(1, "a   "); // This will return a record
                  stmt.executeQuery();
                  

                  编辑:现在这些是限制:

                  • 上述解决方法是不可取的,因为它修改了 c? 的内容,并且使得在 c 上使用索引变得非常困难.
                  • 将列从 CHAR 移动到 VARCHAR(当然应该是)是不可能的
                  • The above workaround is not desireable as it modifies both the contents of c and ?, AND it makes using indexes on c quite hard.
                  • Moving the column from CHAR to VARCHAR (which it should be, of course) is not possible

                  EDIT:这些限制的原因是因为我是从jOOQ,一个数据库抽象库.所以我的要求是提供一个非常通用的解决方案,不会破坏 jOOQ 的客户端代码中的任何内容.这就是为什么我不是这种解决方法的忠实粉丝.这就是为什么我无权访问该 CHAR 列的声明.但是,我仍然希望能够处理这个案子.

                  EDIT: The reasons for these constraints is because I ask this question from the point of view of the developer of jOOQ, a database abstraction library. So my requirements are to provide a very generic solution that doesn't break anything in jOOQ's client code. That is why I'm not really a big fan of the workaround. And that's why I don't have access to that CHAR column's declaration. But still, I want to be able to handle this case.

                  你会怎么做?当我想忽略尾随空格时,处理 CHAR 数据类型的好做法是什么?

                  What would you do instead? What's a good practice for handling CHAR data types when I want to ignore trailing whitespace?

                  推荐答案

                  如果你愿意

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

                  要返回记录,请尝试

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

                  这篇关于Oracle JDBC 和 Oracle CHAR 数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:将 Unixtime 转换为日期时间 SQL (Oracle) 下一篇:Oracle SQL - 确定连续值范围

                  相关文章

                1. <i id='7gDqw'><tr id='7gDqw'><dt id='7gDqw'><q id='7gDqw'><span id='7gDqw'><b id='7gDqw'><form id='7gDqw'><ins id='7gDqw'></ins><ul id='7gDqw'></ul><sub id='7gDqw'></sub></form><legend id='7gDqw'></legend><bdo id='7gDqw'><pre id='7gDqw'><center id='7gDqw'></center></pre></bdo></b><th id='7gDqw'></th></span></q></dt></tr></i><div id='7gDqw'><tfoot id='7gDqw'></tfoot><dl id='7gDqw'><fieldset id='7gDqw'></fieldset></dl></div>
                  <tfoot id='7gDqw'></tfoot>
                2. <small id='7gDqw'></small><noframes id='7gDqw'>

                  1. <legend id='7gDqw'><style id='7gDqw'><dir id='7gDqw'><q id='7gDqw'></q></dir></style></legend>

                        <bdo id='7gDqw'></bdo><ul id='7gDqw'></ul>