开发者

Why does JDBC driver pad some blank characterS other a queried field, from an Oracle Database?

开发者 https://www.devze.com 2022-12-13 23:45 出处:网络
So, here is the code which create the table in an Oracle 10g / UTF-8database : CREATE TABLE TEST_SEMANTIC

So, here is the code which create the table in an Oracle 10g / UTF-8 database :

CREATE TABLE TEST_SEMANTIC
(
SEMANTIC_COLBYTE char(2 byte) ,
SEMANTIC_COLCHAR char(2 char)
);

meaning, that I use two differents types of semantic for the two columns, byte and char.

I then insert inside the database these corresponding data :

insert into test_semantic(SEMANTIC_COLBYTE,SEMANTIC_COLCHAR)
values('é','é');

So when I use the JDBC driver to query the database in a java program, and display the result, I expect an output like this :

Byte>é<
Char>é<

Whereas I get this :

Byte>é<
Char>é      <

When I query the database like this :

select dump(semantic_colbyte,16),dump(semantic_colchar,16) from test_semantic;

I get this :

Typ=96 Len=2: c3,a9 Typ=96 Len=3: c3,a9,20

Here is the java code :

public static void main(String[] args){
        Connection con = null;
        try {
            Class.forName("oracle.jdbc.driver.Oracle开发者_运维知识库Driver");

        } catch (java.lang.ClassNotFoundException e) {
            System.err.print("ClassNotFoundException:");
            System.err.println(e.getMessage());
        }

        try {
            Properties props = new Properties();
            props.put("user", "XXX");
            props.put("password", "XXX");

            con = DriverManager.getConnection("jdbc:oracle:thin:@xxx:1521:xxx", props);
            Statement stmt = (Statement) con.createStatement();
            stmt.execute("SELECT SEMANTIC_COLBYTE,SEMANTIC_COLCHAR FROM TEST_SEMANTIC"); 
            ResultSet result = stmt.getResultSet();
            result.next();
            String output_byte = result.getString(1);
            String output_char = result.getString(2);

        System.out.println("Byte>"+output_byte+"<");                
        System.out.println("Char>"+output_char+"<");

        } catch (SQLException ex) {
            System.err.println("SQLException: " + ex.getMessage());
        }
    }


CHAR Datatype:

The CHAR datatype specifies a fixed-length character string. Oracle ensures that all values stored in a CHAR column have the length specified by size. If you insert a value that is shorter than the column length, then Oracle blank-pads the value to column length.


Don't forget to trim your values while using char. Or don't use char, use varchar2, until you are providing the exact sized value as the column size.

You might want to know why, so here it is.


Have you already read the Oracle documentation on Oracle length semantics for character datatypes?

http://download.oracle.com/docs/cd/B19306_01/server.102/b14220/datatype.htm#sthref3787


Which characterset is the database (and your session) actually in. Mine was in AL32UTF8 and wouldn't accept 'é' in a 2 byte CHAR field. In a 4 byte field, it went to Typ=96 Len=4: ef,bf,bd,20

A UTF-8 character can be four bytes and therefore the CHAR(2 char) can be up to eight bytes. So I could understand a string of length 8 coming out. Seven is a bit odd, almost like it was told the first character is three bytes and the second character can be up to four.

Can you play with ResultSetMetaData (eg getColumnDisplaySize, getColumnTypeName) and see what comes out.

0

精彩评论

暂无评论...
验证码 换一张
取 消