Why does the JDBC driver put a space on another blank name for another requested field from an Oracle database?

So, here is the code that creates the table in the Oracle 10g / UTF-8 database:

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

means that I use two different types of semantics for two columns, bytes and char.

Then I insert the following data into the database:

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

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

 Byte>é< Char>é< 

Considering the following:

 Byte>é< Char>é < 

When I query the database as follows:

 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.OracleDriver"); } 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()); } } 
+4
source share
4 answers

Remember to trim your values ​​when using char . Or don't use char , use varchar2 until you specify the exact value of the column size.

You might want to find out why this is so .

+4
source

CHAR Data Type :

The CHAR data type indicates a character string of fixed length. Oracle guarantees that all values ​​stored in the CHAR column have a length specified by size. If you insert a value that is less than the length of the column, then Oracle will empty the value for the length of the column.

+5
source

Have you read the Oracle documentation on Oracle length semantics for character data types?

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

+2
source

What character set is the database (and your session). The mine was in AL32UTF8 and did not accept 'é' in the 2 byte CHAR field. In a field with 4 bytes, it went to Typ = 96 Len = 4: ef, bf, bd, 20

A UTF-8 character can be four bytes, and therefore CHAR (2 char) can contain up to eight bytes. Thus, I realized that a string of length 8 was output. Seven is a little strange, almost as it was said, the first character is three bytes, and the second character can be up to four.

You can play with ResultSetMetaData (e.g. getColumnDisplaySize, getColumnTypeName) and see what happens.

0
source

All Articles